2021-01-30 21:36:38 +01:00
#### [Examples](../README.md)
2021-03-01 12:59:06 +01:00
## Bashbot webhook example
2021-01-30 21:36:38 +01:00
2021-03-01 08:30:11 +01:00
### Webhook
2021-01-30 21:36:38 +01:00
2021-03-01 08:30:11 +01:00
Bashbot default mode is to poll Telegram server for updates but Telegram offers webhook as a more efficient method to deliver updates.
2021-03-01 12:59:06 +01:00
If your server is reachable from the Internet its possible to use the method described here.
2021-02-20 08:34:42 +01:00
2021-03-01 19:40:53 +01:00
Prerequisite for receiving Telegram updates with webhook is a valid SSL certificate, a self signed certificate will not be sufficient.
2021-03-01 08:30:11 +01:00
2021-03-08 08:06:35 +01:00
Webhook processing require special setup on server and Telegram side, therefore it's implemented as separate scripts and you need at least sudo rights to setup.
2021-01-30 21:36:38 +01:00
2021-01-31 21:18:40 +01:00
#### Setup Apache webhook
2021-01-30 21:36:38 +01:00
2021-03-01 13:22:49 +01:00
Prerequisite: An Apache webserver with a valid SLL certificate chain and php enabled.\
This should work with other webservers also but it's not testet.
2021-01-30 21:36:38 +01:00
2021-03-01 13:22:49 +01:00
Setup webhook with Apache:
2021-01-30 21:36:38 +01:00
2021-01-31 21:18:40 +01:00
- install bashbot as described in [Bashbot Installation ](../../doc/0_install.md )
2021-03-01 08:30:11 +01:00
- create file `data-bot-bash/webhook-fifo-<botname>` (_\<botname\> as in `botconfig.jssh` _)
2021-03-05 11:48:06 +01:00
- run `sudo bashbot.sh init` to setup bashbot to run as same user as web server (_e.g. www_)
2021-03-01 19:40:53 +01:00
- create a directory in web root: `telegram/<your_bot_token>` (_< your_bot_token > as `botconfig.jssh` _)
2021-03-05 11:48:06 +01:00
- give web server access to directory (_e.g.`chown www:www -R telegram` _)
2021-03-01 13:22:49 +01:00
- go into the new directory and copy all files from `examples/webhook` to it
- edit file `BASHBOT_HOME` to contain ithe Bashbot installation directory as first line (_other lines are ignored_)
2021-03-03 08:49:50 +01:00
- execute `php index.php` with user id of web server to test write access to `data-bot-bash/webhook-fifo-< botname >
2021-03-01 08:30:11 +01:00
2021-03-01 13:22:49 +01:00
Calling `https://<yourservername>/telegram/<your_bot_token>/` will execute `index.php`
thus append received data to the file `data-bot-bash/webhook-fifo-<botname>` .
E.g. `https://<yourservername>/telegram/<your_bot_token>/?json={"test":"me"}` will append `{"test":"me"}` .
2021-01-30 21:36:38 +01:00
2021-03-01 08:30:11 +01:00
Now your Server is ready to receive updates from Telegram.
2021-01-30 21:36:38 +01:00
2021-02-28 17:13:25 +01:00
#### Default webhook processing
2021-01-30 21:36:38 +01:00
2021-03-01 19:40:53 +01:00
This is the testet and supported default method for processing Telegram updates over webhook.
2021-01-31 21:30:28 +01:00
2021-03-01 19:40:53 +01:00
To enable update processing delete the file `data-bot-bash/webhook-fifo-<botname>` if webhook is working as described above.
2021-03-01 08:30:11 +01:00
Incoming Telegram updates are now forwarded to the script `bin/process_update.sh` for processing.
2021-01-31 21:30:28 +01:00
2021-03-01 19:40:53 +01:00
On incoming Telegram updates the script is executed, it sources bashbot.sh and forward the update to Bashbot for processing.
2021-03-01 13:22:49 +01:00
Even it seems overhead to source Bashbot for every update, it's more responsive and create less load than Bashbot polling mode.
2021-01-31 21:30:28 +01:00
2021-03-01 13:22:49 +01:00
Nevertheles there are some limitations compared to polling mode:
2021-02-24 16:33:03 +01:00
- no startup actions
2021-02-25 19:20:09 +01:00
- `addons` and `TIMER_EVENTS` are not working
2021-02-24 16:33:03 +01:00
2021-06-03 14:12:47 +02:00
Interactive and background jobs are working as of Bashbot Version 1.51.
2021-02-24 16:33:03 +01:00
2021-02-28 17:13:25 +01:00
#### Full webhook processing
2021-01-31 21:30:28 +01:00
2021-03-01 12:59:06 +01:00
Full webhook processing use an external script to imitate Bashbot polling mode with webhook.
2021-02-28 17:13:25 +01:00
1. Default webook method must work first!
2021-03-01 13:22:49 +01:00
2. run `bashbot.sh init` to setup bashbot to run with your user id
2. Create a named pipe: `mkfifo data-bot-bash/webhook-fifo-botname` and give the web server write access to it
2021-03-03 08:49:50 +01:00
3. execute `php index.php` with user id of web server to test write access to `data-bot-bash/webhook-fifo-< botname >
2021-03-05 11:48:06 +01:00
4. Start script for Bashbot webhook polling mode:\
2021-03-01 08:30:11 +01:00
`bin/process-batch.sh --startbot --watch data-bot-bash/webhook-fifo-<botname>`
2021-02-28 17:13:25 +01:00
2021-03-01 13:22:49 +01:00
The script read updates from given file line by line and forward updates to Bashbot update processing. `--startbot` will run the startup actions
(_e.g. load addons, start TIMER, trigger first run_) and `--watch` will wait for new updates instead of exit on end of file.
2021-03-01 19:40:53 +01:00
Short form: 'bin/process-batch.sh -s -w'
2021-02-28 17:13:25 +01:00
2021-03-05 11:48:06 +01:00
If script works as expected, you may run Bashbot webook polling in background by using `./bachbot.rc starthook/stophook` .
2021-03-03 09:00:41 +01:00
2021-03-05 11:48:06 +01:00
To switch back to default processing delete fifo `data-bot-bash/webhook-fifo-<botname>` and stop `bin/process-batch.sh` .
2021-02-01 10:30:47 +01:00
2021-02-20 08:34:42 +01:00
#### Enable webhook on Telegram side
2021-01-31 21:30:28 +01:00
To get updates via webhook your server must be reachable from the internet and you must
instruct Telegram where to deliver updates, this is done by calling bashbot function `set_webhook` .
*Example:*
```bash
bin/any_command.sh set_webhook "https://myserver.com/telegram"
```
instruct Telegram to use the URL `https://myserver.com/telegram/<your_bot_token>/` to deliver updates.
After you enable webhook to deliver Telegram updates it's no more possible to poll updates with `bashbot.sh start` .
To stop delivering of Telegram updates via webhook run `bin/any_command.sh delete_webhook` .
2021-02-20 08:34:42 +01:00
**Important**: Telegram will refuse to deliver updates if your webhook has no valid SSL certificate chain.
#### Bash webhook
2021-03-01 13:22:49 +01:00
A pure bash webhook implementation is not possible without extra software because Telegram delivers
webhook updates only over secure TLS connections with a valid SSL certificate chain.
2021-02-20 08:34:42 +01:00
2021-03-01 13:22:49 +01:00
`socat` looks like a tool to listen for Telegram updates from bash scripts, let's see ...
2021-02-20 08:34:42 +01:00
2021-06-03 14:21:40 +02:00
#### $$VERSION$$ v1.51-0-g6e66a28
2021-01-30 21:36:38 +01:00