From 4833712246757cfec66493014ea159d1b8a5a687 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Sun, 31 Jan 2021 10:00:15 +0100 Subject: [PATCH] doc: webhook functions bin: do not show JSON.sh error --- bin/process_update.sh | 4 +- doc/6_reference.md | 80 +++++++++++++++++++++++++++++++++++++- examples/webhook/index.php | 18 ++++++--- 3 files changed, 94 insertions(+), 8 deletions(-) diff --git a/bin/process_update.sh b/bin/process_update.sh index c9e8bb4..34385fb 100755 --- a/bin/process_update.sh +++ b/bin/process_update.sh @@ -15,7 +15,7 @@ USAGE='process_update.sh [-h|--help] [debug] [/dev/null)" # assign to bashbot ARRAY Json2Array 'UPD' <<<"${UPDATE}" diff --git a/doc/6_reference.md b/doc/6_reference.md index 0c8894e..e540a35 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -1222,6 +1222,84 @@ https://linuxhint.com/associative_array_bash/ https://linuxconfig.org/how-to-use-arrays-in-bash-script +---- + +### Manage Webhooks +Bashbot default mode is to poll Telegram server for updates. Telegram offers the more efficient webhook method to deliver updates. +I recommend to use webhook with `bin/any_command.sh` to test and setup functionality, see also [webhook example](../example/webhook) + +##### get_webhook_info +`get_webhook_info` get current webhook status for your bot, e.g. url, waiting updates, last error. + +*usage:* get_webhook_info + +*example:* +```bash +bin/any_command.sh get_webhook_info + +["URL"] "" +["OK"] "true" +["LASTERR"] "" +["COUNT"] "0" +["CERT"] "false" +["result","pending_update_count"] "0" +["ok"] "true" +["result","has_custom_certificate"] "false" +``` + + +##### delete_webhook +`delete_webhook` deletes current set webhook, deletes outstanding updates if second arg is `true` + +*usage:* delete_webhook [true|false] + +*example:* + +```bash +bin/any_command.sh delete_webhook false + +["RESULT"] "true" +["OK"] "true" +["result"] "true" +["ok"] "true" +["description"] "Webhook was deleted" +``` + + +##### set_webhook +`set_webhook` deletes current set webhook, deletes outstanding updates if second arg is `true` + +*usage:* set_webhook "https://host.dom[:port][/path]" [max_conn] + +First arg is URL to send updates to, port and path are optional. If port is given itmust be on of 443, 80, 88 or 8443. +For security reasons your TOKEN will be added to URL, e.g. https://myhost.com -> https://myhost.com/12345678:azndfhbgdfbbbdsfg +Second arg is max connection rate in the range 1-100, bashbot default is 1. + +*example:* + +```bash +bin/any_command.sh set_webhook https://myhost.com/telegram 2 + +["OK"] "true" +["RESULT"] "true" +["ok"] "true" +["result"] "true" +["description"] "Webhook is set" + +bin/any_command.sh get_webhook_info + +["OK"] "true" +["URL"] "https://myhost.com/telegram/12345678:AABBCCDDEE...aabbccee124567890/" +["COUNT"] "0" +["CERT"] "false" +["ok"] "true" +["result","ip_address"] "1.2.3.4" +["result","url"] "https://myhost.com/telegram/12345678:AABBCCDDEE...aabbccee124567890/" +["result","pending_update_count"] "0" +["result","max_connections"] "2" +["result","has_custom_certificate"] "false" +``` + ---- ### Aliases - shortcuts for often used functions @@ -1493,5 +1571,5 @@ The name of your bot is available as bash variable "$ME", there is no need to ca #### [Prev Best Practice](5_practice.md) #### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v1.40-dev-1-g41e2d09 +#### $$VERSION$$ v1.40-dev-13-g2a3663a diff --git a/examples/webhook/index.php b/examples/webhook/index.php index f07c97b..51157ef 100644 --- a/examples/webhook/index.php +++ b/examples/webhook/index.php @@ -7,7 +7,7 @@ * @license http://www.wtfpl.net/txt/copying/ WTFPLv2 * @since 30.01.2021 20:24 * -#### $$VERSION$$ v1.40-dev-12-ga289cb8 +#### $$VERSION$$ v1.40-dev-13-g2a3663a ***********************************************************/ // bashbot home dir @@ -15,17 +15,25 @@ // webhook endpoint $cmd=$BASHBOT_HOME.'/bin/process_update.sh'; - // read request data + // prepeare read, e.g. run from CLI $data=''; $input="php://input"; - if (php_sapi_name() == "cli") { $input="php://stdin"; } + $json_file="json.txt"; + if (php_sapi_name() == "cli") { + if(is_readable($json_file)) { + $input=$json_file; + } else { + $input="php://stdin"; + } + } + // read request data if($json = file_get_contents($input)) { $data = $json; } else { $data = implode(" ",$_POST); } // file_put_contents('server.txt', print_r($_SERVER, TRUE)); - // file_put_contents('json.txt', $data); + // file_put_contents($json_file, $data); // process telegram update if ($data == '') { @@ -55,6 +63,6 @@ $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'; header($protocol.' '.$code.' '.$msg); } - exit('Error '.$code.': '.$msg."\n"); + exit('Error '.$code.': '.$msg); } ?>