123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856 |
- <?php
- class WolfDispatcher {
-
- protected $botToken = '';
-
- protected $commands = array();
-
- protected $groupChatCommands = array();
-
- protected $adminCommands = array();
-
- protected $textReacts = array();
-
- protected $ignoredChatIds = array();
-
- protected $adminChatIds = array();
-
- protected $allowedChatIds = array();
-
- protected $telegram = '';
-
- protected $receivedData = array();
-
- protected $chatId = 0;
-
- protected $messageId = 0;
-
- protected $chatType = '';
-
- protected $photoHandleMethod = '';
-
- protected $chatMemberAppearMethod = '';
-
- protected $chatMemberLeftMethod = '';
-
- protected $debugFlag = false;
-
- protected $calledActions = array();
-
- protected $botImplementation = '';
-
- protected $hookAutoSetup = false;
-
- const LOG_PATH = 'exports/';
-
- const HOOK_PID_PATH = 'exports/';
-
- public function __construct($token) {
- if (!empty($token)) {
- $this->botToken = $token;
- }
- $this->initTelegram();
- $this->setBotName();
- }
-
- protected function setBotName() {
- $this->botImplementation = get_class($this);
- }
-
- public function setDebug($state) {
- if ($state) {
- $this->debugFlag = true;
- }
- }
-
- protected function initTelegram() {
- if (!empty($this->botToken)) {
- if (class_exists('UbillingTelegram')) {
- $this->telegram = new UbillingTelegram($this->botToken);
- } else {
- if (class_exists('WolfGram')) {
- $this->telegram = new WolfGram($this->botToken);
- }
- }
- if (empty($this->telegram)) {
- throw new Exception('EX_NO_TELEGRAM_LIB');
- }
- } else {
- throw new Exception('EX_EMPTY_TOKEN');
- }
- }
-
- public function setActions($commands) {
- if (!empty($commands)) {
- if (is_array($commands)) {
- $this->commands = $commands;
- }
- }
- }
-
- public function setGroupActions($groupCommands) {
- $this->groupChatCommands = $groupCommands;
- }
-
- public function setAdminActions($adminCommands) {
- $this->adminCommands = $adminCommands;
- }
-
- public function setAdminChatId($chatIds) {
- if (!empty($chatIds)) {
- $chatIds = array_flip($chatIds);
- $this->adminChatIds = $chatIds;
- }
- }
-
- public function setTextReactions($commands) {
- if (!empty($commands)) {
- if (is_array($commands)) {
- $this->textReacts = $commands;
- }
- }
- }
-
- public function setPhotoHandler($name) {
- if (!empty($name)) {
- $this->photoHandleMethod = $name;
- }
- }
-
- public function setOnChatMemberActions($methodAppear = '', $methodLeft = '') {
- if (!empty($methodAppear)) {
- $this->chatMemberAppearMethod = $methodAppear;
- }
- if (!empty($methodLeft)) {
- $this->chatMemberLeftMethod = $methodLeft;
- }
- }
-
- public function setAllowedChatIds($chatIds) {
- if (!empty($chatIds)) {
- if (is_array($chatIds)) {
- $this->allowedChatIds = array_flip($chatIds);
- }
- }
- }
-
- public function setIgnoredChatIds($chatIds) {
- if (!empty($chatIds)) {
- if (is_array($chatIds)) {
- $this->ignoredChatIds = array_flip($chatIds);
- }
- }
- }
-
- protected function getTextAction() {
- $result = '';
-
- if (!empty($this->commands)) {
- foreach ($this->commands as $eachCommand => $eachAction) {
- if (mb_stripos($this->receivedData['text'], $eachCommand, 0, 'UTF-8') !== false) {
- $result = $eachAction;
- }
- }
- }
-
- if (!empty($this->adminCommands)) {
- if ($this->isAdmin()) {
- foreach ($this->adminCommands as $eachCommand => $eachAction) {
- if (mb_stripos($this->receivedData['text'], $eachCommand, 0, 'UTF-8') !== false) {
- $result = $eachAction;
- }
- }
- }
- }
-
- if (empty($result)) {
- if (!empty($this->textReacts)) {
- foreach ($this->textReacts as $eachTextReact => $eachAction) {
- if (mb_stripos($this->receivedData['text'], $eachTextReact, 0, 'UTF-8') !== false) {
- $result = $eachAction;
- }
- }
- }
- }
- return($result);
- }
-
- protected function runAction($actionName) {
- if (!empty($actionName)) {
- if (method_exists($this, $actionName)) {
-
- $this->$actionName();
- if ($this->debugFlag) {
- $this->calledActions[] = 'METHOD: ' . $actionName;
- }
- } else {
- if (function_exists($actionName)) {
- $actionName($this->receivedData);
- if ($this->debugFlag) {
- $this->calledActions[] = 'FUNC: ' . $actionName;
- }
- } else {
- if ($this->debugFlag) {
-
- $chatId = $this->receivedData['chat']['id'];
- $message = __('Any existing function on method named') . ' `' . $actionName . '` ' . __('not found by dispatcher');
- $this->telegram->directPushMessage($chatId, $message);
- if ($this->debugFlag) {
- $this->calledActions[] = 'FAILED: ' . $actionName;
- }
- }
- }
- }
- }
- }
-
- protected function reactInput() {
- $currentInputAction = '';
- if (!empty($this->receivedData)) {
- if (isset($this->receivedData['from']) AND isset($this->receivedData['chat'])) {
- $chatId = $this->receivedData['chat']['id'];
- $interractionAllowed = true;
-
- if (!empty($this->allowedChatIds)) {
- if (!isset($this->allowedChatIds[$chatId])) {
- $interractionAllowed = false;
- }
- }
-
- if (isset($this->ignoredChatIds[$chatId])) {
- $interractionAllowed = false;
- }
- if ($interractionAllowed) {
-
- if (!empty($this->receivedData['text'])) {
- $currentInputAction = $this->getTextAction();
- if (!empty($currentInputAction)) {
- $this->runAction($currentInputAction);
- } else {
-
- $this->handleEmptyAction();
- }
- } else {
-
- $this->handleEmptyText();
- }
-
- if (!empty($this->photoHandleMethod)) {
- if ($this->isPhotoReceived()) {
- $this->runAction($this->photoHandleMethod);
- }
- }
-
- if (!empty($this->chatMemberAppearMethod)) {
- if ($this->isNewChatMemberAppear()) {
- $this->runAction($this->chatMemberAppearMethod);
- }
- }
- if (!empty($this->chatMemberLeftMethod)) {
- if ($this->isChatMemberLeft()) {
- $this->runAction($this->chatMemberLeftMethod);
- }
- }
-
- if ($this->isPhotoReceived()) {
- $this->handlePhotoReceived();
- }
- }
- }
-
- $this->handleAnyWay();
- }
- }
-
- protected function handleEmptyAction() {
-
- }
-
- protected function handleEmptyText() {
-
- }
-
- protected function handleAnyWay() {
-
- }
-
- protected function handlePhotoReceived() {
-
- }
-
- protected function writeDebugLog() {
- global $starttime, $query_counter;
- if ($this->debugFlag) {
- $nowmtime = explode(' ', microtime());
- $wtotaltime = $nowmtime[0] + $nowmtime[1] - $starttime;
- $logData = $this->botImplementation . ': ' . curdatetime() . PHP_EOL;
- $logData .= print_r($this->receivedData, true) . PHP_EOL;
- $logData .= 'GT: ' . round($wtotaltime, 4) . ' QC: ' . $query_counter . PHP_EOL;
- if (!empty($this->calledActions)) {
- $logData .= PHP_EOL . 'Called actions: ' . print_r($this->calledActions, true) . PHP_EOL;
- } else {
- $logData .= PHP_EOL . 'Called actions: NONE' . PHP_EOL;
- }
- $logData .= '==================' . PHP_EOL;
- $logFileName = strtolower($this->botImplementation) . '_debug.log';
- file_put_contents(self::LOG_PATH . $logFileName, $logData, FILE_APPEND);
- }
- }
-
- public function listen() {
-
- $this->installWebHook();
-
- $this->receivedData = $this->telegram->getHookData();
- if (!empty($this->receivedData)) {
- @$this->chatId = $this->receivedData['chat']['id'];
- @$this->messageId = $this->receivedData['message_id'];
- @$this->chatType = $this->receivedData['chat']['type'];
-
- if (!empty($this->groupChatCommands)) {
- if ($this->chatType != 'private') {
-
- $this->setActions($this->groupChatCommands);
- }
- }
- $this->reactInput();
- }
- $this->writeDebugLog();
- return($this->receivedData);
- }
-
- protected function isPhotoReceived() {
- $result = false;
- if ($this->receivedData['photo'] OR $this->receivedData['document']) {
- $imageMimeTypes = array('image/png', 'image/jpeg');
- if ($this->receivedData['photo']) {
- $result = true;
- } else {
- if ($this->receivedData['document']) {
- $imageMimeTypes = array_flip($imageMimeTypes);
- if (isset($imageMimeTypes[$this->receivedData['document']['mime_type']])) {
- $result = true;
- }
- }
- }
- }
- return($result);
- }
-
- protected function isNewChatMemberAppear() {
- $result = false;
- if ($this->receivedData['new_chat_member']) {
- $result = $this->receivedData['new_chat_member'];
- }
- return($result);
- }
-
- protected function isChatMemberLeft() {
- $result = false;
- if ($this->receivedData['left_chat_member']) {
- $result = $this->receivedData['left_chat_member'];
- }
- return($result);
- }
-
- protected function isAdmin() {
- $result = false;
- if (!empty($this->adminChatIds)) {
-
- if (isset($this->adminChatIds[$this->chatId])) {
- $result = true;
- } else {
-
- if (isset($this->adminChatIds[$this->receivedData['from']['id']])) {
- $result = true;
- }
- }
- }
- return($result);
- }
-
- protected function message() {
- return($this->receivedData);
- }
-
- protected function getPhoto() {
- $result = '';
- $filePath = '';
- $fileId = '';
- $imageMimeTypes = array('image/png', 'image/jpeg');
-
- if ($this->receivedData['photo']) {
- $maxSizeFile = end($this->receivedData['photo']);
- $fileId = $maxSizeFile['file_id'];
- } else {
-
- $imageMimeTypes = array_flip($imageMimeTypes);
- if ($this->receivedData['document']) {
- if (isset($imageMimeTypes[$this->receivedData['document']['mime_type']])) {
- $fileId = $this->receivedData['document']['file_id'];
- }
- }
- }
-
- if ($fileId) {
- $filePath = $this->telegram->getFilePath($fileId);
- if ($filePath) {
- $result = $this->telegram->downloadFile($filePath);
- }
- }
- return($result);
- }
-
- protected function savePhoto($savePath) {
- $result = '';
- if (!empty($savePath)) {
- if ($this->isPhotoReceived()) {
- $receivedPhoto = $this->getPhoto();
- if ($receivedPhoto) {
- file_put_contents($savePath, $receivedPhoto);
- if (file_exists($savePath)) {
- $result = $savePath;
- }
- }
- }
- }
- return($result);
- }
-
- protected function reply($message = '', $keyboard = array()) {
- $result = '';
- if (!empty($message)) {
- $replyResult = $this->telegram->directPushMessage($this->chatId, $message, $keyboard);
- if ($replyResult) {
- $result = json_decode($replyResult, true);
- }
- }
- return($result);
- }
-
- protected function replyTo($message = '', $keyboard = array(), $replyToMsg = '') {
- $result = '';
- if (!empty($message)) {
- if (empty($replyToMsg)) {
- $replyToMsg = $this->messageId;
- }
- $replyResult = $this->telegram->directPushMessage($this->chatId, $message, $keyboard, false, $replyToMsg);
- if ($replyResult) {
- $result = json_decode($replyResult, true);
- }
- }
- return($result);
- }
-
- protected function castKeyboard($buttons, $text = '⌨️') {
- if (!empty($buttons)) {
- $keyboard = $this->telegram->makeKeyboard($buttons);
- $this->reply($text, $keyboard);
- }
- }
-
- public function hookAutosetup($enabled = true) {
- $this->hookAutoSetup = $enabled;
- }
-
- protected function installWebHook() {
- if ($this->hookAutoSetup) {
- $listenerUrl = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
- $tokenHash = md5($this->botToken . $listenerUrl);
- $hookPidName = self::HOOK_PID_PATH . $this->botImplementation . $tokenHash . '.hook';
-
- if (!file_exists($hookPidName)) {
- $hookInfo = json_decode($this->telegram->getWebHookInfo(), true);
- if ($hookInfo['result']['url'] != $listenerUrl) {
-
- $this->telegram->setWebHook($listenerUrl, 100);
- if (function_exists('show_success')) {
- show_success($this->botImplementation . ' web-hook URL: ' . $hookInfo['result']['url']);
- }
- } else {
-
- if (function_exists('show_warning')) {
- show_warning($this->botImplementation . ' web-hook URL: ' . $hookInfo['result']['url']);
- }
- }
-
- file_put_contents($hookPidName, $listenerUrl);
-
- if ($this->debugFlag) {
- $logFileName = strtolower($this->botImplementation) . '_debug.log';
- $logData = $this->botImplementation . ': ' . curdatetime() . PHP_EOL;
- $logData .= 'INSTALLED WEB HOOK: ' . $listenerUrl . PHP_EOL;
- $logData .= 'HOOK PID: ' . $hookPidName . PHP_EOL;
- file_put_contents(self::LOG_PATH . $logFileName, $logData, FILE_APPEND);
- }
- } else {
-
- $currentHookUrl = file_get_contents($hookPidName);
- if (function_exists('show_info')) {
- show_info($this->botImplementation . ' web-hook URL: ' . $currentHookUrl);
- }
- }
- }
- }
- }
|