Commit 392f49e8 authored by Francesco Iannuzzelli's avatar Francesco Iannuzzelli

#30 Telegram bot

parent 6563d34b
<?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
......@@ -32,6 +27,14 @@ use function GuzzleHttp\Psr7\str;
********************************************************************/
use Formapro\TelegramBot\Bot;
use Formapro\TelegramBot\Update;
use Formapro\TelegramBot\SendMessage;
use Formapro\TelegramBot\AnswerCallbackQuery;
use Formapro\TelegramBot\InlineKeyboardButton;
use Formapro\TelegramBot\InlineKeyboardMarkup;
use Formapro\TelegramBot\SetWebhook;
use function GuzzleHttp\Psr7\str;
include_once(SERVER_ROOT."/../classes/config.php");
include_once(SERVER_ROOT."/../classes/db.php");
require_once(SERVER_ROOT."/../others/vendor/autoload.php");
......@@ -64,7 +67,9 @@ class TelegramBot
*
* @var array
*/
private $actions;
private $chat;
private $inline_buttons = array();
/**
* Initialize local properties
......@@ -76,7 +81,7 @@ class TelegramBot
$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);
$this->chat = json_decode(file_get_contents(SERVER_ROOT."/../custom/telegram.json"),true);
} else {
http_response_code(404);
exit;
......@@ -94,45 +99,88 @@ class TelegramBot
}
private function ActionLookup($action) {
$action = strtolower(trim(trim($action),'/'));
$response = false;
foreach($this->actions as $a) {
$this->menu_items = array();
foreach($this->chat['actions'] as $a) {
if($a['action'] == 'boh') {
$boh = $a['response'];
}
if($a['action'] == $action) {
$response = $a['response'];
if(isset($a['menu'])) {
$response .= PHP_EOL . PHP_EOL . $this->MenuLookup($a['menu']);
}
}
}
return $response? $response : $boh;
}
private function MenuLookup($menu) {
$response = '';
foreach($this->chat['menus'] as $m) {
if($m['menu'] == $menu) {
$response = $m['description'] . PHP_EOL;
$this->menu_items = $m['items'];
foreach($m['items'] as $item) {
$response .= strtoupper($item['label']) . ' - ' . $item['description'] . PHP_EOL;
}
}
}
return $response;
}
public function ProcessRequest($req) {
public function ProcessRequest($data) {
$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";
$update = Update::create($data);
if ($callbackQuery = $update->getCallbackQuery()) {
$answer = new AnswerCallbackQuery($callbackQuery->getId());
// should do something with the answer
$answer->setText('Grazie!');
$bot->answerCallbackQuery($answer);
} else {
$req = $update->getMessage();
$id = $req->getChat()->getId();
$from = $req->getFrom();
// get action
$text = $update->getMessage()->getText();
$action = trim(trim($text),'/');
// lookup
$response = $this->ActionLookup($action);
if(!$response) {
$response = $this->ActionLookup('boh');
$text = $req->getText();
// check it's not a bot
if(!$from->isBot()) {
$firstname = $from->getFirstName();
$lastname = $from->getLastName();
$username = $from->getUsername();
$fullname = "$firstname $lastname";
// lookup
$response = $this->ActionLookup($text);
// variables replacements
$response = str_replace('%NAME%', $fullname, $response);
//$msg = new SendMessage($id,"Hi there $fullname ($id - $username)!");
//$bot->sendMessage($msg);
$msg = new SendMessage($id,$response);
// request phone
/*
$button = new KeyboardButton('Telefono');
$button->setRequestContact(true);
$keyboard = new ReplyKeyboardMarkup([[$button]]);
$keyboard->setOneTimeKeyboard(true);
*/
// Inline buttons
if(count($this->inline_buttons)>0) {
$buttons = [];
foreach($this->inline_buttons as $item) {
$buttons[] = InlineKeyboardButton::withTextAsCallbackData(strtoupper($item));
}
$keyboard = new InlineKeyboardMarkup([$buttons]);
$msg->setReplyMarkup($keyboard);
}
$bot->sendMessage($msg);
http_response_code(200);
} else {
$bot->sendMessage($msg);
$msg = new SendMessage($id,"Sorry, mama told me not to talk to bots");
http_response_code(403);
}
// 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);
}
}
......
......@@ -41,5 +41,7 @@ if ($tb->KeyCheck($key)) {
$requestBody = file_get_contents('php://input');
$req = json_decode($requestBody, true);
$tb->ProcessRequest($req);
} else {
http_response_code(401);
}
?>
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