examples: webhook: make home and file configurable

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2021-01-31 18:08:18 +01:00
parent a7c98d750c
commit 5b0b121ba5
4 changed files with 46 additions and 17 deletions

View File

@ -0,0 +1,2 @@
/usr/local/github/telegram-bot-bash-develop/DIST/telegram-bot-bash
/usr/local/telegram-bot-bash

View File

@ -32,13 +32,13 @@ To stop delivering of updates with webhook run `bin/any_command.sh delete_webhoo
If you have an Apache webserver with a valid SLL certificate chain and php running you can use it as webhook endpoint:
- setup bashbot to run as the same user as your web server (_`bashbot.sh init`_)
- create the directory `telegram/<your_bot_token>` in webserver root
- copy `index.php` into new directory
- edit `index.php` to point to your bashbot installation
- 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
*Example index.php*, see [index.php](index.php) for a more complete implementation.
*Example minimal index.php*, see [index.php](index.php) for complete implementation.
```php
<?php
@ -57,6 +57,6 @@ pclose($handle);
?>
```
#### $$VERSION$$ v1.40-dev-12-ga289cb8
#### $$VERSION$$ v1.40-dev-20-ga7c98d7

View File

@ -2,18 +2,34 @@
/************************************************************
* @file examples/webhook/index.php
* @description example webhook implementation for apache
* write to fifo/file if writeable, else pipe to command
*
* first line of BASHBOT_HOME is used as bot home (optional)
* must start with /, not contain /. and min 20 characters long
*
* @author KayM (gnadelwartz), kay@rrr.de
* @license http://www.wtfpl.net/txt/copying/ WTFPLv2
* @since 30.01.2021 20:24
*
#### $$VERSION$$ v1.40-dev-13-g2a3663a
#### $$VERSION$$ v1.40-dev-20-ga7c98d7
***********************************************************/
// bashbot home dir
$CONFIG_HOME='BASHBOT_HOME';
$BASHBOT_HOME='/usr/local/telegram-bot-bash';
// webhook endpoint
// read from config file
if (file_exists($CONFIG_HOME)) {
$tmp = trim(fgets(fopen($CONFIG_HOME, 'r')));
// start with '/', not '/.', min 20 chars
if (substr($tmp,0,1) == '/' && strlen($tmp) >= 20 && strpos($tmp, '/.') === false) {
$BASHBOT_HOME=$tmp;
}
}
// script endpoint
$cmd=$BASHBOT_HOME.'/bin/process_update.sh';
// fifo endpoint
$fifo=$BASHBOT_HOME.'/data-bot-bash/webhook-fifo';
// prepeare read, e.g. run from CLI
$data='';
@ -33,27 +49,38 @@
$data = implode(" ",$_POST);
}
// file_put_contents('server.txt', print_r($_SERVER, TRUE));
// file_put_contents($json_file, $data);
file_put_contents($json_file, $data);
// process telegram update
// prepare for writing
if ($data == '') {
error_response(400, "No data received");
}
if (! chdir($BASHBOT_HOME)) {
error_response(403, "No route to bot home");
}
if (! is_executable($cmd)) {
error_response(502, "Webhook endpoint not found");
}
if (! $handle = popen( $cmd.' debug', 'w' )) {
error_response(503, "Can't open webhook endpoint");
// fifo or command?
if (! is_writeable($fifo)) {
// pipe to command
if (! file_exists($cmd)) {
error_response(502, "Webhook endpoint not found");
}
if (! $handle = popen( $cmd.' debug', 'w' )) {
error_response(503, "Can't open webhook command endpoint");
}
} else {
// write to fifo
if (! $handle = fopen( $fifo, 'a' )) {
error_response(503, "Can't open webhook file endpoint");
}
flock($handle, LOCK_EX);
}
if (fwrite( $handle, $data.'\n') === false) {
if (fwrite( $handle, str_replace(array("\n", "\r"), '',$data). PHP_EOL) === false) {
error_response(504, "Write to webhook failed");
}
flock($handle, LOCK_UN);
pclose($handle);
/**/
function error_response($code, $msg) {
$api = substr(php_sapi_name(), 0, 3);
@ -63,6 +90,6 @@
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
header($protocol.' '.$code.' '.$msg);
}
exit('Error '.$code.': '.$msg);
exit('Error '.$code.': '.$msg. PHP_EOL);
}
?>