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

113 lines
3.2 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
* 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
2021-01-31 07:58:24 +00:00
*
* @author KayM (gnadelwartz), kay@rrr.de
* @license http://www.wtfpl.net/txt/copying/ WTFPLv2
* @since 30.01.2021 20:24
*
2022-06-27 18:18:58 +00:00
#### $$VERSION$$ v1.52-1-g0dae2db
2021-01-31 07:58:24 +00:00
***********************************************************/
2021-01-30 20:36:38 +00:00
2021-01-31 07:58:24 +00:00
// bashbot home dir
$CONFIG_HOME='BASHBOT_HOME';
2021-01-31 07:58:24 +00:00
$BASHBOT_HOME='/usr/local/telegram-bot-bash';
// 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;
}
}
2021-02-15 14:17:50 +00:00
// bashbot config file
$CONFIG=$BASHBOT_HOME.'/botconfig.jssh';
// set botname here or read botname from config file if unknown
$botname="unknown";
if ($botname == "unknown" && file_exists($CONFIG)) {
$prefix='["botname"] "';
$len=strlen($prefix);
$arr = file($CONFIG, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($arr as $line) {
if(substr($line, 0, $len) == $prefix) {
$botname=substr($line, $len, strlen($line)-$len-1);
}
}
}
// script endpoint
2021-01-31 07:58:24 +00:00
$cmd=$BASHBOT_HOME.'/bin/process_update.sh';
2021-02-15 14:17:50 +00:00
// default fifo endpoint
$fifo=$BASHBOT_HOME.'/data-bot-bash/webhook-fifo-'.$botname;
2021-01-31 07:58:24 +00:00
// prepeare read, e.g. run from CLI
2021-01-31 07:58:24 +00:00
$data='';
$input="php://input";
$json_file="json.txt";
if (php_sapi_name() == "cli") {
if(is_readable($json_file)) {
$input=$json_file;
} else {
$input="php://stdin";
}
}
// read request data
2021-01-31 07:58:24 +00:00
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);
2021-01-31 20:18:40 +00:00
if ($data == '') { $data = implode(" ",$_GET); }
2021-01-31 07:58:24 +00:00
}
2021-01-31 20:18:40 +00:00
// uncomment to save last received JSON
2021-02-27 15:46:04 +00:00
// file_put_contents($json_file, str_replace(array("\n", "\r"), '',$data). PHP_EOL));
2021-01-31 07:58:24 +00:00
// prepare for writing
2021-01-31 07:58:24 +00:00
if ($data == '') {
error_response(400, "No data received");
}
if (! chdir($BASHBOT_HOME)) {
error_response(403, "No route to bot home");
}
2021-01-30 20:36:38 +00:00
// 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);
2021-01-31 07:58:24 +00:00
}
if (fwrite( $handle, str_replace(array("\n", "\r"), '',$data). PHP_EOL) === false) {
2021-01-31 07:58:24 +00:00
error_response(504, "Write to webhook failed");
}
flock($handle, LOCK_UN);
2021-01-31 07:58:24 +00:00
pclose($handle);
/**/
2021-01-31 07:58:24 +00:00
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. PHP_EOL);
2021-01-31 07:58:24 +00:00
}
2021-01-30 20:36:38 +00:00
?>