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)
## 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.
If your server is reachable from the Internet, you can use the webhook method described here (experimental), instead of running bashbot
with `bashbot.sh start`
#### 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`
Bashbot default mode is to poll Telegram server for updates but Telegram offers also webhook
as a more efficient method to deliver updates.
If your server is reachable from the Internet you can use the webhook method described here.
**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`_)
- create the directory `telegram/<your_bot_token>` in apache web root
- copy files all files form here into new directory and change to it
- edit `BASHBOT_HOME` to point to your bashbot installation directory
- setup webhook for your server (_e.g. `bin/any_command.sh set_webhook "https://myserver.com/telegram`_)
- send a command to your bot (_e.g. `/start`_) to check correct setup
- install bashbot as described in [Bashbot Installation](../../doc/0_install.md)
- create file `data-bot-bash/webhook-fifo`
- run `bashbot.sh init` to setup bashbot to run as same user as Apache (_e.g. www_)
- go to apache web root and create directory `telegram/<your_bot_token>`
- copy all files from `examples/webhook` to new directory and change to it
- 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
<?php
// bashbot home
$BASHBOT_HOME='/usr/local/telegram-bot-bash';
$cmd=$BASHBOT_HOME.'/bin/process_update.sh';
// save server context and webhook JSON
$json = file_get_contents("php://input");
// process teegram update
chdir($BASHBOT_HOME);
$handle = popen( $cmd, 'w' );
fwrite( $handle, $json.'\n' );
pclose($handle);
?>
```
#### $$VERSION$$ v1.40-dev-20-ga7c98d7
Now your Apache is ready to forward data to Bashbot.
#### Simple update processing
To configure simple update processing delete file `data-bot-bash/webhook-fifo` after your webhook is working.
Every webhook call now forwards incoming Telegram updates to the named pipe `data-bot-bash/webhook-fifo`
and Bashbot poll them like polling Telegram server. This is much more efficient than polling Telegram server.
To switch from `Simple processing` to `High traffic processing` start bashbot as `bashbot.sh start-hook`.
Stop bashbot with `bashbot.sh stop` to switch back to `Simple processing`
#### $$VERSION$$ v1.40-dev-21-g5b0b121

View File

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