webhook: final index.php

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2021-01-31 08:58:24 +01:00
parent a289cb8e5b
commit 2a3663a463
2 changed files with 74 additions and 14 deletions

View File

@ -38,6 +38,25 @@ If you have an Apache webserver with a valid SLL certificate chain and php runni
- 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
#### $$VERSION$$ v1.40-dev-11-g9316caa
*Example index.php*, see [index.php](index.php) for a more complete implementation.
```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-12-ga289cb8

View File

@ -1,19 +1,60 @@
<?php
// where bashbot is installed
$BASHBOT_HOME='/usr/local/telegram-bot-bash';
$cmd=$BASHBOT_HOME.'/bin/process_update.sh';
/************************************************************
* @file examples/webhook/index.php
* @description example webhook implementation for apache
*
* @author KayM (gnadelwartz), kay@rrr.de
* @license http://www.wtfpl.net/txt/copying/ WTFPLv2
* @since 30.01.2021 20:24
*
#### $$VERSION$$ v1.40-dev-12-ga289cb8
***********************************************************/
// save server context and webhook JSON
file_put_contents('server.txt', print_r($_SERVER, TRUE));
if($json = file_get_contents("php://input")) {
$data = $json;
// bashbot home dir
$BASHBOT_HOME='/usr/local/telegram-bot-bash';
// webhook endpoint
$cmd=$BASHBOT_HOME.'/bin/process_update.sh';
// read request data
$data='';
$input="php://input";
if (php_sapi_name() == "cli") { $input="php://stdin"; }
if($json = file_get_contents($input)) {
$data = $json;
} else {
$data = $_POST;
$data = implode(" ",$_POST);
}
file_put_contents('json.txt', $data);
// file_put_contents('server.txt', print_r($_SERVER, TRUE));
// file_put_contents('json.txt', $data);
// process teegram update
chdir($BASHBOT_HOME);
$handle = popen( $cmd.' debug', 'w' );
fwrite( $handle, $data.'\n' );
// process telegram update
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");
}
if (fwrite( $handle, $data.'\n') === false) {
error_response(504, "Write to webhook failed");
}
pclose($handle);
function error_response($code, $msg) {
$api = substr(php_sapi_name(), 0, 3);
if ($api == 'cgi' || $api == 'fpm') {
header('Status: '.$code.' '.$msg);
} else {
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
header($protocol.' '.$code.' '.$msg);
}
exit('Error '.$code.': '.$msg."\n");
}
?>