example: webhook: rewrite setup

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2021-01-31 21:18:40 +01:00
parent 5b0b121ba5
commit 6754273c6e
2 changed files with 36 additions and 52 deletions

View File

@ -1,62 +1,45 @@
#### [Examples](../README.md) #### [Examples](../README.md)
## Bashtbot webhook examples ## Bashtbot webhook example
### webhooks ### Webhooks
Bashbot default mode is to poll Telegram server for updates. Telegram offers the more efficient webhook method to deliver updates. Bashbot default mode is to poll Telegram server for updates but Telegram offers also webhook
If your server is reachable from the Internet, you can use the webhook method described here (experimental), instead of running bashbot as a more efficient method to deliver updates.
with `bashbot.sh start` If your server is reachable from the Internet you can use the webhook method described here.
#### Setup webhook
To get updates with webhooks your server must be reachable from the internet and you must inform Telegram about where to deliver updates,
this will be done by calling `set_webhook URL`. For security reasons bashbot adds you bottoken to the URL.
*Example:*
```bash
bin/any_command.sh set_webhook "https://myserver.com/telegram"
```
will instruct Telegram to use the URL `https://myserver.com/telegram/<your_bot_token>/` to deliver updates.
After you setup webhook to deliver updates it's no more possible to poll updates with `bashbot.sh start`.
To stop delivering of updates with webhook run `bin/any_command.sh delete_webhook`
**Important**: Only https connections with a valid certificate chain are allowed as endpoint for webhook. #### Setup Apache webhook
#### Using Apache with php enabled Prerequisite: An Apache webserver with a valid SLL certificate chain and php enabled.
If you have an Apache webserver with a valid SLL certificate chain and php running you can use it as webhook endpoint: Prepare Apache to forward webhook to Bashbot:
- setup bashbot to run as the same user as your web server (_`bashbot.sh init`_) - install bashbot as described in [Bashbot Installation](../../doc/0_install.md)
- create the directory `telegram/<your_bot_token>` in apache web root - create file `data-bot-bash/webhook-fifo`
- copy files all files form here into new directory and change to it - run `bashbot.sh init` to setup bashbot to run as same user as Apache (_e.g. www_)
- edit `BASHBOT_HOME` to point to your bashbot installation directory - go to apache web root and create directory `telegram/<your_bot_token>`
- setup webhook for your server (_e.g. `bin/any_command.sh set_webhook "https://myserver.com/telegram`_) - copy all files from `examples/webhook` to new directory and change to it
- send a command to your bot (_e.g. `/start`_) to check correct setup - write bashbot installation directory as first line to file `BASHBOT_HOME`
- execute `php index.php`
*Example minimal index.php*, see [index.php](index.php) for complete implementation. Every call to webhook `https://<yourservername>/telegram/<your_bot_token>/` will execute
`index.php` and write received JSON to file `data-bot-bash/webhook-fifo`.
E.g. the URL `https://<yourservername>/telegram/<your_bot_token>/?json={"test":"me"}`
will append `{"test":"me"}` to the file `data-bot-bash/webhook-fifo`.
```php Now your Apache is ready to forward data to Bashbot.
<?php
// bashbot home
$BASHBOT_HOME='/usr/local/telegram-bot-bash'; #### Simple update processing
$cmd=$BASHBOT_HOME.'/bin/process_update.sh';
To configure simple update processing delete file `data-bot-bash/webhook-fifo` after your webhook is working.
// save server context and webhook JSON Every webhook call now forwards incoming Telegram updates to the named pipe `data-bot-bash/webhook-fifo`
$json = file_get_contents("php://input"); and Bashbot poll them like polling Telegram server. This is much more efficient than polling Telegram server.
// process teegram update To switch from `Simple processing` to `High traffic processing` start bashbot as `bashbot.sh start-hook`.
chdir($BASHBOT_HOME); Stop bashbot with `bashbot.sh stop` to switch back to `Simple processing`
$handle = popen( $cmd, 'w' );
fwrite( $handle, $json.'\n' ); #### $$VERSION$$ v1.40-dev-21-g5b0b121
pclose($handle);
?>
```
#### $$VERSION$$ v1.40-dev-20-ga7c98d7

View File

@ -11,7 +11,7 @@
* @license http://www.wtfpl.net/txt/copying/ WTFPLv2 * @license http://www.wtfpl.net/txt/copying/ WTFPLv2
* @since 30.01.2021 20:24 * @since 30.01.2021 20:24
* *
#### $$VERSION$$ v1.40-dev-20-ga7c98d7 #### $$VERSION$$ v1.40-dev-21-g5b0b121
***********************************************************/ ***********************************************************/
// bashbot home dir // bashbot home dir
@ -47,9 +47,10 @@
$data = $json; $data = $json;
} else { } else {
$data = implode(" ",$_POST); $data = implode(" ",$_POST);
if ($data == '') { $data = implode(" ",$_GET); }
} }
// file_put_contents('server.txt', print_r($_SERVER, TRUE)); // uncomment to save last received JSON
file_put_contents($json_file, $data); // file_put_contents($json_file, $data);
// prepare for writing // prepare for writing
if ($data == '') { if ($data == '') {