Commit 12dbe907 authored by Francesco Iannuzzelli's avatar Francesco Iannuzzelli

#30 Telegram bot plumbing

parent 84c59ba5
......@@ -195,6 +195,8 @@ class Configuration
$values['recaptcha_public_key'] = (isset($conf->recaptcha_public_key) && $conf->recaptcha_public_key != "")? $conf->recaptcha_public_key : "";
$values['recaptcha_private_key'] = (isset($conf->recaptcha_private_key) && $conf->recaptcha_private_key != "")? $conf->recaptcha_private_key : "";
$values['fb_app_id'] = (isset($conf->fb_app_id) && $conf->fb_app_id != "")? $conf->fb_app_id : "";
$values['telegram_bot_api_key'] = (isset($conf->telegram_bot_api_key) && $conf->telegram_bot_api_key != "")? $conf->telegram_bot_api_key : "";
$values['telegram_callback_key'] = (isset($conf->telegram_callback_key) && $conf->telegram_callback_key != "")? $conf->telegram_callback_key : "";
$values['dev'] = (isset($conf->dev) && is_bool($conf->dev))? $conf->dev : false;
$values['track'] = (isset($conf->track) && is_bool($conf->track))? $conf->track: false;
$values['track_exclude'] = (isset($conf->track_exclude) && is_array($conf->track_exclude))? $conf->track_exclude : array("127.0.0.1");
......
<?php
use Formapro\TelegramBot\Bot;
use Formapro\TelegramBot\Update;
use Formapro\TelegramBot\SendMessage;
use Formapro\TelegramBot\SetWebhook;
use function GuzzleHttp\Psr7\str;
/********************************************************************
PhPeace - Portal Management System
Copyright notice
(C) 2003-2019 Francesco Iannuzzelli <francesco@phpeace.org>
All rights reserved
This script is part of PhPeace.
PhPeace is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
PhPeace is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
The GNU General Public License (GPL) is available at
http://www.gnu.org/copyleft/gpl.html.
A copy can be found in the file COPYING distributed with
these scripts.
This copyright notice MUST APPEAR in all copies of the script!
********************************************************************/
include_once(SERVER_ROOT."/../classes/config.php");
include_once(SERVER_ROOT."/../classes/db.php");
require_once(SERVER_ROOT."/../others/vendor/autoload.php");
/**
* Telegram bot
*
* @package PhPeace
* @author Francesco Iannuzzelli <francesco@phpeace.org>
*/
class TelegramBot
{
/**
* Telegram Bot API key
*
* @var string
*/
private $telegram_bot_api_key;
/**
* Telegram callback key
* (specified in the webook)
*
* @var string
*/
private $telegram_callback_key;
/**
* Chat actions & responses
*
* @var array
*/
private $actions;
/**
* Initialize local properties
*/
function __construct()
{
$conf = new Configuration();
$this->telegram_bot_api_key = $conf->Get("telegram_bot_api_key");
$this->telegram_callback_key = $conf->Get("telegram_callback_key");
$filename = SERVER_ROOT."/../custom/telegram.json";
if(file_exists($filename)) {
$this->actions = json_decode(file_get_contents(SERVER_ROOT."/../custom/telegram.json"),true);
} else {
http_response_code(404);
exit;
}
}
public function KeyCheck($key) {
return $key !='' && $key == $this->telegram_callback_key;
}
public function WebhookSet($url) {
$bot = new Bot($this->telegram_bot_api_key);
$response = $bot->setWebhook( new SetWebhook($url) );
return str($response).PHP_EOL;
}
private function ActionLookup($action) {
$response = false;
foreach($this->actions as $a) {
if($a['action'] == $action) {
$response = $a['response'];
}
}
return $response;
}
public function ProcessRequest($req) {
$bot = new Bot($this->telegram_bot_api_key);
$update = Update::create($req);
$id = $update->getMessage()->getChat()->getId();
$from = $update->getMessage()->getFrom();
// check it's not a bot
if(!$from->isBot()) {
$firstname = $from->getFirstName();
$lastname = $from->getLastName();
$username = $from->getUsername();
$fullname = "$firstname $lastname";
// get action
$text = $update->getMessage()->getText();
$action = trim(trim($text),'/');
// lookup
$response = $this->ActionLookup($action);
if(!$response) {
$response = $this->ActionLookup('boh');
}
// variables replacements
$response = str_replace('%NAME%', $fullname, $response);
// send message
$msg = new SendMessage($id,"Hi there $fullname ($id - $username)!");
$bot->sendMessage($msg);
$msg = new SendMessage($id,$response);
} else {
$msg = new SendMessage($id,"Sorry, mama told me not to speak to bots");
}
$bot->sendMessage($msg);
http_response_code(200);
}
}
?>
......@@ -1341,6 +1341,14 @@ class Config
*/
public $fb_app_id = "";
/**
* TELEGRAM BOT API KEY
* Default: ""
*
* @var string
*/
public $telegram_bot_api_key = "";
/**
* RECAPTCHA
* Default: false
......
<?php
/********************************************************************
PhPeace - Portal Management System
Copyright notice
(C) 2003-2019 Francesco Iannuzzelli <francesco@phpeace.org>
All rights reserved
This script is part of PhPeace.
PhPeace is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
PhPeace is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
The GNU General Public License (GPL) is available at
http://www.gnu.org/copyleft/gpl.html.
A copy can be found in the file COPYING distributed with
these scripts.
This copyright notice MUST APPEAR in all copies of the script!
********************************************************************/
if (!defined('SERVER_ROOT'))
define('SERVER_ROOT',$_SERVER['DOCUMENT_ROOT']);
include_once(SERVER_ROOT."/../classes/formhelper.php");
include_once(SERVER_ROOT."/../classes/telegram.php");
$fh = new FormHelper(false,0,false);
$get = $fh->HttpGet();
$key = $get['key'];
$tb = new TelegramBot();
if ($tb->KeyCheck($key)) {
$requestBody = file_get_contents('php://input');
$req = json_decode($requestBody, true);
$tb->ProcessRequest($req);
}
?>
<?php
/********************************************************************
PhPeace - Portal Management System
Copyright notice
(C) 2003-2019 Francesco Iannuzzelli <francesco@phpeace.org>
All rights reserved
This script is part of PhPeace.
PhPeace is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
PhPeace is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
The GNU General Public License (GPL) is available at
http://www.gnu.org/copyleft/gpl.html.
A copy can be found in the file COPYING distributed with
these scripts.
This copyright notice MUST APPEAR in all copies of the script!
********************************************************************/
$_SERVER['DOCUMENT_ROOT'] = realpath("../");
define('SERVER_ROOT',$_SERVER['DOCUMENT_ROOT']);
$options = getopt("h:");
if(is_array($options) && isset($options['h']))
{
$webhook = $options['h'];
include_once(SERVER_ROOT."/../classes/telegram.php");
$tb = new TelegramBot();
echo $tb->WebhookSet($webhook);
}
?>
#! /bin/sh
CURRENTDIR=`dirname $0`
cd $CURRENTDIR
php $CURRENTDIR/telegram_webhook.php -h $1
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment