telegram-bot-bash/examples/webhook/index.php

61 lines
1.7 KiB
PHP
Raw Normal View History

2021-01-30 20:36:38 +00:00
<?php
2021-01-31 07:58:24 +00:00
/************************************************************
* @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
***********************************************************/
2021-01-30 20:36:38 +00:00
2021-01-31 07:58:24 +00:00
// 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;
2021-01-30 20:36:38 +00:00
} else {
2021-01-31 07:58:24 +00:00
$data = implode(" ",$_POST);
}
// file_put_contents('server.txt', print_r($_SERVER, TRUE));
// file_put_contents('json.txt', $data);
// 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");
2021-01-30 20:36:38 +00:00
}
2021-01-31 07:58:24 +00:00
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");
}
2021-01-30 20:36:38 +00:00
?>