From 6754273c6ee628049947a11c7597112b4d8cca85 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 31 Jan 2021 21:18:40 +0100 Subject: [PATCH] example: webhook: rewrite setup --- examples/webhook/README.md | 81 +++++++++++++++----------------------- examples/webhook/index.php | 7 ++-- 2 files changed, 36 insertions(+), 52 deletions(-) diff --git a/examples/webhook/README.md b/examples/webhook/README.md index 01b5eb8..1ed8924 100644 --- a/examples/webhook/README.md +++ b/examples/webhook/README.md @@ -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//` 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/` 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/` +- 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:///telegram//` will execute +`index.php` and write received JSON to file `data-bot-bash/webhook-fifo`. +E.g. the URL `https:///telegram//?json={"test":"me"}` +will append `{"test":"me"}` to the file `data-bot-bash/webhook-fifo`. -```php - -``` - -#### $$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 diff --git a/examples/webhook/index.php b/examples/webhook/index.php index 2635e2a..86fc1d9 100644 --- a/examples/webhook/index.php +++ b/examples/webhook/index.php @@ -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 == '') {