|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * TaskTimeTerminate Telegram Bot |
| 5 | + * https://github.com/KIMB-technologies/TaskTimeTerminate-Bot |
| 6 | + * |
| 7 | + * (c) 2020 KIMB-technologies |
| 8 | + * https://github.com/KIMB-technologies/ |
| 9 | + * |
| 10 | + * released under the terms of GNU Public License Version 3 |
| 11 | + * https://www.gnu.org/licenses/gpl-3.0.txt |
| 12 | + * |
| 13 | + * The project is based on |
| 14 | + * https://github.com/php-telegram-bot/example-bot/ |
| 15 | + * (c) PHP Telegram Bot Team |
| 16 | + * released under the terms of MIT License |
| 17 | + * https://github.com/php-telegram-bot/example-bot/blob/master/LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +namespace Longman\TelegramBot\Commands\SystemCommands; |
| 21 | + |
| 22 | +use Longman\TelegramBot\Commands\SystemCommand; |
| 23 | +use Longman\TelegramBot\Entities\ServerResponse; |
| 24 | +use Longman\TelegramBot\Request; |
| 25 | + |
| 26 | +class GenericmessageCommand extends SystemCommand |
| 27 | +{ |
| 28 | + |
| 29 | + protected $name = 'genericmessage'; |
| 30 | + protected $description = 'Generic fallback message'; |
| 31 | + protected $version = '1.0.0'; |
| 32 | + |
| 33 | + public function execute(): ServerResponse |
| 34 | + { |
| 35 | + /** |
| 36 | + * Authenticate |
| 37 | + */ |
| 38 | + $chat = $this->getMessage()->getChat(); |
| 39 | + if(!$chat->isPrivateChat()){ |
| 40 | + return $this->replyToUser('This bot is for usage in private Chats only!'); |
| 41 | + } |
| 42 | + $session = new \TTTBot\Session($chat->getId()); |
| 43 | + if(!$session->getData('registered')){ |
| 44 | + return $this->replyToUser('Please start the bot via /start!'); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Handle message |
| 49 | + */ |
| 50 | + $handler = $session->getTemp('messageHandler'); |
| 51 | + if($handler === false){ |
| 52 | + return $this->replyToUser('Please use the commands /ttt and /task!'); |
| 53 | + } |
| 54 | + else{ |
| 55 | + $text = $this->getMessage()->getText(true); |
| 56 | + |
| 57 | + switch($handler) { |
| 58 | + case "categoryAdd": |
| 59 | + if(\TTTBot\InputParser::checkCategoryInput($text)){ |
| 60 | + \TTTBot\TTTLoader::addReadlineValue($session->getTemp('readlineName'), $text); |
| 61 | + $command = array('c','cats','add'); |
| 62 | + } |
| 63 | + else{ |
| 64 | + return $this->replyToChat('Invalid category name!'); |
| 65 | + } |
| 66 | + break; |
| 67 | + case "categoryDel": |
| 68 | + if(is_numeric($text)){ |
| 69 | + \TTTBot\TTTLoader::addReadlineValue($session->getTemp('readlineName'), $text); |
| 70 | + $command = array('c','cats','del'); |
| 71 | + } |
| 72 | + else{ |
| 73 | + return $this->replyToChat('Invalid category ID!'); |
| 74 | + } |
| 75 | + break; |
| 76 | + case "editSync": |
| 77 | + $r = \TTTBot\SettingsHelper::syncStep($session, $text); |
| 78 | + if($r === true){ |
| 79 | + $command = array('c','sync','server'); |
| 80 | + } |
| 81 | + else{ |
| 82 | + return $this->replyToChat($r); |
| 83 | + } |
| 84 | + break; |
| 85 | + case "editData": |
| 86 | + $r = \TTTBot\SettingsHelper::editStep($session, $text); |
| 87 | + if($r === true){ |
| 88 | + $command = array('c','e', $session->getTemp('editDay')); |
| 89 | + } |
| 90 | + else{ |
| 91 | + return $this->replyToChat($r); |
| 92 | + } |
| 93 | + break; |
| 94 | + case "newTask": |
| 95 | + $th = new \TTTBot\AddTaskHelper($session); |
| 96 | + $th->messageCommand($text); |
| 97 | + $opts = array('parse_mode' => 'Markdown'); |
| 98 | + if(!is_null($th->getReplyKeyboard())){ |
| 99 | + $opts['reply_markup'] = $th->getReplyKeyboard(); |
| 100 | + } |
| 101 | + return $this->replyToChat($th->getReplyText(), $opts); |
| 102 | + default: |
| 103 | + return $this->replyToChat('Unknown message handler – this should not have happened!'); |
| 104 | + break; |
| 105 | + } |
| 106 | + |
| 107 | + $session->setTemp('messageHandler', false); |
| 108 | + \TTTBot\TTTLoader::loadTTT('/code/data/' . $session->getKey()); |
| 109 | + $output = \TTTBot\TTTLoader::runTTTCommand($command); |
| 110 | + return $this->replyToUser( |
| 111 | + $output, |
| 112 | + array('parse_mode' => 'Markdown') |
| 113 | + ); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments