<?php
class Tamasbabot extends FrontDesk
{
public $text;
public function init(){
$this->db = new TamasbabotDatabase("@tamasbabot");
}
public function processCallback($update)
{
$callbacksData = explode(":",$this->callback_data);
$userID = $callbacksData[2];
$actionKey = $callbacksData[0];
$callbackHandler = $this->getCallbackHandler($actionKey);
if(isset($callbacksData[3])){
$otherData = $callbacksData[3];
$callbackHandler->setData($otherData);
}
$callbackHandler->setUserID($userID);
$callbackHandler->execute();
}
public function getCallbackHandler($actionKey): CallBackhandler{
$answers[TextAnswerCallBackHandler::$key]= new TextAnswerCallBackHandler($this);
$answers[ShowButtonsCallBackHandler::$key]= new ShowButtonsCallBackHandler($this);
$answers[CustomAnswerCallbackHandler::$key]= new CustomAnswerCallbackHandler($this);
$answers[EnumFrontdesk::SHOW_USER]= new ShowUserCallBackHandler($this);
$answers[EnumFrontdesk::DELETE_ME]= new DeleteMessageCallbackHandler($this);
return $answers[$actionKey];
}
public function processText()
{
$this->text = $this->message['text'];
$this->handleTextMessage();
}
function getDB(): TamasbabotDatabase {
return $this->db;
}
function isStart(){
return strpos($this->text, "/start") === 0;
}
function navigateHome($chatID, $messageID){
$markups = $this->buildKeyboardMarkup($chatID, $messageID);
$this->editMessageText($this->message_id,$this->chat_id,$this->message['text'], $markups);
}
function handleUserMessages(){
if($this->isStart()){
$this->handleStart();
}
else {
if(!isset($this->text)){
$this->forwardMessage($this->adminId);
}
$markups = $this->buildKeyboardMarkup($this->chat_id, $this->message_id);
$this->sendMarkedupMessageToAdmin($this->text, $markups);
}
}
function handleTextMessage()
{
// $intent = $this->getIntentService()->findIntent($text);
if ($this->isAdmin()) {
$this->handleAdminMessages();
} else {
$this->handleUserMessages();
}
}
function buildKeyboardMarkup($chatID, $messageID){
$markup = "";
$keyboard['inline_keyboard']=[];
$buttons = $this->getDB()->getButtons($chatID, $messageID);
foreach($buttons as $button){
$keyboard['inline_keyboard'][] = $button;
}
$markup = json_encode($keyboard, true);
return $markup;
}
function handleAdminMessages()
{
if(!isset($this->message['reply_to_message'])){
return false;
}
$repliedTomessage = $this->message['reply_to_message'];
if ($this->isMessageReplied()) {
$userID = $this->extractUserIDFromMessage($this->message);
if (isset($userID)) {
$this->sendMessage($this->message['text'], $userID);
} else if (!empty($repliedTomessage['photo'])) {
$this->sendMessage($this->message['text'], $userID);
} else if (!empty($repliedTomessage['voice'])) {
$this->sendMessage($this->message['text'], $userID);
}
}
}
function extractUserIDFromMessage(){
$repliedTomessage = $this->message['reply_to_message'];
if(!$this->isUserPrivate()){
$userid = $repliedTomessage['forward_from']['id'];
}
else if(strstr($repliedTomessage['text'],"userid") == 0) {
$userid = str_replace("userid:" , "" , $repliedTomessage['text']);
}
return $userid;
}
function isUserPrivate(){
$repliedTomessage = $this->message['reply_to_message'];
return !isset($repliedTomessage['forward_from']);
}
function isMessageReplied(){
$repliedTomessage = $this->message['reply_to_message'];
return isset($repliedTomessage);
}
function handleStart()
{
if (strpos($this->text, "/start") === 0) {
// $this->apiRequestJson("sendMessage", array(
// 'chat_id' => $this->chat_id,
// "text" => 'Hello',
// 'reply_markup' => array(
// 'keyboard' => array(
// array(
// 'Hallo',
// 'salam'
// )
// ),
// 'one_time_keyboard' => true,
// 'resize_keyboard' => true
// )
// ));
$this->apiRequest("sendMessage", array(
'chat_id' => $this->chat_id,
"text" => "سلام، خوبین ؟ اگه سوالی راجع به اپ بایادیک یا ربات مترجم دارین اینجا بنویسید" . "😊"
));
$this->apiRequest("sendMessage", array(
'chat_id' => $this->adminId,
"text" => $this->text . $this->chat_id
));
}
}
}