/** * Send command * * @todo Fake response doesn't need json encoding? * * @param string $action * @param array $data * * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public static function send($action, array $data = []) { self::ensureValidAction($action); $bot_name = self::$telegram->getBotName(); if (defined('PHPUNIT_TESTSUITE')) { $fake_response = self::generateGeneralFakeServerResponse($data); return new ServerResponse($fake_response, $bot_name); } self::ensureNonEmptyData($data); $response = json_decode(self::execute($action, $data), true); if (null === $response) { throw new TelegramException('Telegram returned an invalid response! Please review your bot name and API key.'); } return new ServerResponse($response, $bot_name); }
/** * @test */ public function execute() { $result = $this->telegram->handle(); $this->assertEquals('4', $result['text']); $this->assertEquals($this->message_id, $result['reply_to_message_id']); $this->assertEquals($this->chat_id, $result['chat_id']); }
/** * setUp */ protected function setUp() { $credentials = ['host' => PHPUNIT_DB_HOST, 'database' => PHPUNIT_DB_NAME, 'user' => PHPUNIT_DB_USER, 'password' => PHPUNIT_DB_PASS]; $this->telegram = new Telegram('apikey', 'testbot'); $this->telegram->enableMySql($credentials); //Make sure we start with an empty DB for each test. TestHelpers::emptyDB($credentials); }
/** * setUp */ protected function setUp() { $credentials = ['host' => '127.0.0.1', 'user' => 'travis', 'password' => '', 'database' => 'telegrambot']; $this->telegram = new Telegram('testapikey', 'testbotname'); $this->telegram->enableMySQL($credentials); //Make sure we start with an empty DB for each test. TestHelpers::emptyDB($credentials); }
/** * @test */ public function testChatType() { $this->chat = new Chat(json_decode('{"id":123,"title":null,"first_name":"john","last_name":null,"username":"******"}', true)); $this->assertEquals('private', $this->chat->getType()); $this->chat = new Chat(json_decode('{"id":-123,"title":"ChatTitle","first_name":null,"last_name":null,"username":"******"}', true)); $this->assertEquals('group', $this->chat->getType()); $this->chat = new Chat(json_decode('{"id":-123,"type":"channel","title":"ChatTitle","first_name":null,"last_name":null,"username":"******"}', true)); $this->assertEquals('channel', $this->chat->getType()); }
/** * @test */ public function testChatType() { $json = ' {"update_id":137809335, "message":{"message_id":4479,"from":{"id":123,"first_name":"John","username":"******"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092987,"reply_to_message":{"message_id":11,"from":{"id":121,"first_name":"Myname","username":"******"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092984,"text":"type some text"},"text":"some text"}} '; $struct = json_decode($json, true); $update = new Update($struct, 'mybot'); $this->message = $update->getMessage(); $this->reply_to_message = $this->message->getReplyToMessage(); $this->assertNull($this->reply_to_message->getReplyToMessage()); }
public function setUp() { //Default command object $this->telegram = new Telegram('apikey', 'testbot'); $this->command_stub = $this->getMockForAbstractClass($this->command_namespace, [$this->telegram]); //Create separate command object that contain a command config $this->telegram_with_config = new Telegram('apikey', 'testbot'); $this->telegram_with_config->setCommandConfig('command_name', ['config_key' => 'config_value']); $this->command_stub_with_config = $this->getMockBuilder($this->command_namespace)->disableOriginalConstructor()->getMockForAbstractClass(); //Set a name for the object property so that the constructor can set the config correctly TestHelpers::setObjectProperty($this->command_stub_with_config, 'name', 'command_name'); $this->command_stub_with_config->__construct($this->telegram_with_config); }
/** * @test */ public function testSetGeneralTestFakeResponse() { //setWebhook ok $fake_response = Request::generateGeneralFakeServerResponse(); $this->server = new ServerResponse($fake_response, 'testbot'); $this->assertTrue($this->server->isOk()); $this->assertTrue($this->server->getResult()); $this->assertNull($this->server->getErrorCode()); $this->assertEquals('', $this->server->getDescription()); //sendMessage ok $fake_response = Request::generateGeneralFakeServerResponse(['chat_id' => 123456789, 'text' => 'hello']); $this->server = new ServerResponse($fake_response, 'testbot'); $this->assertTrue($this->server->isOk()); $this->assertInstanceOf('\\Longman\\TelegramBot\\Entities\\Message', $this->server->getResult()); $this->assertNull($this->server->getErrorCode()); $this->assertNull($this->server->getDescription()); //Message $this->assertEquals('1234', $this->server->getResult()->getMessageId()); $this->assertEquals('1441378360', $this->server->getResult()->getDate()); $this->assertEquals('hello', $this->server->getResult()->getText()); //Message //User $this->assertEquals('123456789', $this->server->getResult()->getFrom()->getId()); $this->assertEquals('botname', $this->server->getResult()->getFrom()->getFirstName()); $this->assertEquals('namebot', $this->server->getResult()->getFrom()->getUserName()); //Message //Chat $this->assertEquals('123456789', $this->server->getResult()->getChat()->getId()); $this->assertEquals('', $this->server->getResult()->getChat()->getFirstName()); $this->assertEquals('', $this->server->getResult()->getChat()->getUserName()); //... they are not finished... }
/** * Pre-execute command * * @return \Longman\TelegramBot\Entities\ServerResponse * @throws \Longman\TelegramBot\Exception\TelegramException */ public function preExecute() { if ($this->need_mysql && !($this->telegram->isDbEnabled() && DB::isDbConnected())) { return $this->executeNoDb(); } return $this->execute(); }
/** * Send command * * @todo Fake response doesn't need json encoding? * @todo Rename "methods" to "actions" * * @param string $action * @param array|null $data * * @return Entities\ServerResponse */ public static function send($action, array $data = null) { if (!in_array($action, self::$methods)) { throw new TelegramException('This method doesn\'t exist!'); } $bot_name = self::$telegram->getBotName(); if (defined('PHPUNIT_TESTSUITE')) { $fake_response = self::generateGeneralFakeServerResponse($data); return new ServerResponse($fake_response, $bot_name); } $response = json_decode(self::executeCurl($action, $data), true); if (is_null($response)) { throw new TelegramException('Telegram returned an invalid response! Please your bot name and api token.'); } return new ServerResponse($response, $bot_name); }
public function __construct(Telegram $telegram) { $this->telegram = $telegram; $this->config = $telegram->getCommandConfig($this->name); }
/** * @test */ public function testTextAndCommandRecognise() { // /command $this->message = new Message($this->generateMessage('/help'), 'testbot'); $this->assertEquals('/help', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals('/help', $this->message->getText()); $this->assertEquals('', $this->message->getText(true)); // text $this->message = new Message($this->generateMessage('some text'), 'testbot'); $this->assertEquals('', $this->message->getFullCommand()); $this->assertEquals('', $this->message->getCommand()); $this->assertEquals('some text', $this->message->getText()); $this->assertEquals('some text', $this->message->getText(true)); // /command@bot $this->message = new Message($this->generateMessage('/help@testbot'), 'testbot'); $this->assertEquals('/help@testbot', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals('/help@testbot', $this->message->getText()); $this->assertEquals('', $this->message->getText(true)); // /commmad text $this->message = new Message($this->generateMessage('/help some text'), 'testbot'); $this->assertEquals('/help', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals('/help some text', $this->message->getText()); $this->assertEquals('some text', $this->message->getText(true)); // /command@bot some text $this->message = new Message($this->generateMessage('/help@testbot some text'), 'testbot'); $this->assertEquals('/help@testbot', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals('/help@testbot some text', $this->message->getText()); $this->assertEquals('some text', $this->message->getText(true)); }
$errors = array(); try { // Connect to Zabbix API. $api = new ZabbixApi($ZABBIX_HOST . 'api_jsonrpc.php', $ZABBIX_USER, $ZABBIX_PASSWORD); $api->setDefaultParams(array('output' => 'extend')); $triggers = $api->triggerGet(array('filter' => array('value' => 1))); foreach ($triggers as $trigger) { $errors[] = $trigger->description; } } catch (Exception $e) { // Exception in ZabbixApi catched. echo $e->getMessage(); } try { // create Telegram API object $telegram = new Telegram($API_KEY, $BOT_NAME); $telegram->enableMySQL($credentials); $telegram->addCommandsPath($COMMANDS_FOLDER); $telegram->setLogRequests(true); $telegram->setLogPath('logs/' . $BOT_NAME . '.log'); $telegram->setLogVerbosity(3); if (!empty($errors)) { $results = Request::sendToActiveChats('sendMessage', array('text' => "[Zabbix]\nWe have a problem\n" . implode(', ', $errors)), false, true, null, null); } $ServerResponse = $telegram->handleGetUpdates(); if ($ServerResponse->isOk()) { $n_update = count($ServerResponse->getResult()); print date('Y-m-d H:i:s', time()) . ' - Processed ' . $n_update . " updates\n"; } else { print date('Y-m-d H:i:s', time()) . " - Fail fetch updates\n"; print $ServerResponse->printError() . "\n";
public function testGetHelpCommandObject() { $command = $this->telegram->getCommandObject('help'); $this->assertInstanceOf('Longman\\TelegramBot\\Commands\\UserCommands\\HelpCommand', $command); }
/** * @test */ public function testTextAndCommandRecognise() { // /command $this->message = new Message($this->generateMessage('/help'), 'testbot'); $this->assertEquals('/help', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals('/help', $this->message->getText()); $this->assertEquals('', $this->message->getText(true)); // text $this->message = new Message($this->generateMessage('some text'), 'testbot'); $this->assertEquals('', $this->message->getFullCommand()); $this->assertEquals('', $this->message->getCommand()); $this->assertEquals('some text', $this->message->getText()); $this->assertEquals('some text', $this->message->getText(true)); // /command@bot $this->message = new Message($this->generateMessage('/help@testbot'), 'testbot'); $this->assertEquals('/help@testbot', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals('/help@testbot', $this->message->getText()); $this->assertEquals('', $this->message->getText(true)); // /commmad text $this->message = new Message($this->generateMessage('/help some text'), 'testbot'); $this->assertEquals('/help', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals('/help some text', $this->message->getText()); $this->assertEquals('some text', $this->message->getText(true)); // /command@bot some text $this->message = new Message($this->generateMessage('/help@testbot some text'), 'testbot'); $this->assertEquals('/help@testbot', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals('/help@testbot some text', $this->message->getText()); $this->assertEquals('some text', $this->message->getText(true)); // /commmad\n text //$array = $this->generateMessage("/help\n some text"); ////print_r($this->generateMessage('/help@testbot')); //echo 'value:'; //print_r($array); $this->message = new Message($this->generateMessage("/help\n some text"), 'testbot'); $this->assertEquals('/help', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals("/help\n some text", $this->message->getText()); $this->assertEquals(' some text', $this->message->getText(true)); // /command@bot\nsome text $this->message = new Message($this->generateMessage("/help@testbot\nsome text"), 'testbot'); $this->assertEquals('/help@testbot', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals("/help@testbot\nsome text", $this->message->getText()); $this->assertEquals('some text', $this->message->getText(true)); // /command@bot \nsome text $this->message = new Message($this->generateMessage("/help@testbot \nsome text"), 'testbot'); $this->assertEquals('/help@testbot', $this->message->getFullCommand()); $this->assertEquals('help', $this->message->getCommand()); $this->assertEquals("/help@testbot \nsome text", $this->message->getText()); $this->assertEquals("\nsome text", $this->message->getText(true)); }
/** * Insert request in db * * @return bool */ public static function insertRequest(Update $update) { if (!self::isDbConnected()) { return false; } $message = $update->getMessage(); $from = $message->getFrom(); $chat = $message->getChat(); $chat_id = $chat->getId(); $date = self::getTimestamp($message->getDate()); $forward_from = $message->getForwardFrom(); $forward_date = self::getTimestamp($message->getForwardDate()); $photo = $message->getPhoto(); $new_chat_participant = $message->getNewChatParticipant(); $new_chat_photo = $message->getNewChatPhoto(); $left_chat_participant = $message->getLeftChatParticipant(); $migrate_from_chat_id = $message->getMigrateFromChatId(); if (is_null($migrate_from_chat_id)) { $migrate_from_chat_id = 0; } try { //chats table $sth2 = self::$pdo->prepare('INSERT INTO `' . TB_CHATS . '` (`id`, `type`, `title`, `created_at` ,`updated_at`, `old_id`) VALUES (:id, :type, :title, :date, :date, :oldid) ON DUPLICATE KEY UPDATE `type`=:type, `title`=:title, `updated_at`=:date'); $chat_title = $chat->getTitle(); $type = $chat->getType(); $sth2->bindParam(':id', $chat_id, \PDO::PARAM_INT); $sth2->bindParam(':type', $type, \PDO::PARAM_INT); $sth2->bindParam(':title', $chat_title, \PDO::PARAM_STR, 255); $sth2->bindParam(':date', $date, \PDO::PARAM_STR); $sth2->bindParam(':oldid', $migrate_from_chat_id, \PDO::PARAM_INT); $status = $sth2->execute(); } catch (PDOException $e) { throw new TelegramException($e->getMessage()); } //insert user and the relation with the chat self::insertUser($from, $date, $chat); //Insert the forwarded message user in users table if (is_object($forward_from)) { self::insertUser($forward_from, $forward_date); $forward_from = $forward_from->getId(); } else { $forward_from = ''; } //Insert the new chat user if (is_object($new_chat_participant)) { self::insertUser($new_chat_participant, $date, $chat); $new_chat_participant = $new_chat_participant->getId(); } else { $new_chat_participant = ''; } //Insert the left chat user if (is_object($left_chat_participant)) { self::insertUser($left_chat_participant, $date, $chat); $left_chat_participant = $left_chat_participant->getId(); } else { $left_chat_participant = ''; } try { //Messages Table $sth = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_MESSAGES . '` ( `update_id`, `message_id`, `user_id`, `date`, `chat_id`, `forward_from`, `forward_date`, `reply_to_message`, `text`, `audio`, `document`, `photo`, `sticker`, `video`, `voice`, `caption`, `contact`, `location`, `new_chat_participant`, `left_chat_participant`, `new_chat_title`,`new_chat_photo`, `delete_chat_photo`, `group_chat_created`, `supergroup_chat_created`, `channel_chat_created`, `migrate_from_chat_id`, `migrate_to_chat_id` ) VALUES (:update_id, :message_id, :user_id, :date, :chat_id, :forward_from, :forward_date, :reply_to_message, :text, :audio, :document, :photo, :sticker, :video, :voice, :caption, :contact, :location, :new_chat_participant, :left_chat_participant, :new_chat_title, :new_chat_photo, :delete_chat_photo, :group_chat_created, :supergroup_chat_created, :channel_chat_created, :migrate_from_chat_id, :migrate_to_chat_id )'); $update_id = $update->getUpdateId(); $message_id = $message->getMessageId(); $from_id = $from->getId(); $reply_to_message = $message->getReplyToMessage(); $reply_to_message_id = null; if (is_object($reply_to_message)) { $reply_to_message_id = $reply_to_message->getMessageId(); //Creating a fake update to insert this message if privacy is on //text messages between users are not delivered to the bot //this can be also a message sent by the bot to the user $fake_update['update_id'] = 0; $fake_update['message'] = $reply_to_message->reflect(); // please notice that, as explaied in the documentation, reply_to_message don't contain other // reply_to_message field so recursion deep is 1 self::insertRequest(new Update($fake_update, self::$telegram->getBotName(), 1)); } $text = $message->getText(); $audio = $message->getAudio(); $document = $message->getDocument(); $sticker = $message->getSticker(); $video = $message->getVideo(); $voice = $message->getVoice(); $caption = $message->getCaption(); $contanc = $message->getContact(); $location = $message->getLocation(); $new_chat_title = $message->getNewChatTitle(); $delete_chat_photo = $message->getDeleteChatPhoto(); $group_chat_created = $message->getGroupChatCreated(); $supergroup_chat_created = $message->getSupergroupChatCreated(); $channel_chat_created = $message->getChannelChatCreated(); $migrate_from_chat_id = $message->getMigrateFromChatId(); $sth->bindParam(':update_id', $update_id, \PDO::PARAM_INT); $sth->bindParam(':message_id', $message_id, \PDO::PARAM_INT); $sth->bindParam(':user_id', $from_id, \PDO::PARAM_INT); $sth->bindParam(':date', $date, \PDO::PARAM_STR); $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_STR); $sth->bindParam(':forward_from', $forward_from, \PDO::PARAM_STR); $sth->bindParam(':forward_date', $forward_date, \PDO::PARAM_STR); $sth->bindParam(':reply_to_message', $reply_to_message_id, \PDO::PARAM_INT); $sth->bindParam(':text', $text, \PDO::PARAM_STR); $sth->bindParam(':audio', $audio, \PDO::PARAM_STR); $sth->bindParam(':document', $document, \PDO::PARAM_STR); $var = []; if (is_array($photo)) { foreach ($photo as $elm) { $var[] = json_decode($elm, true); } $photo = json_encode($var); } else { $photo = ''; } $sth->bindParam(':photo', $photo, \PDO::PARAM_STR); $sth->bindParam(':sticker', $sticker, \PDO::PARAM_STR); $sth->bindParam(':video', $video, \PDO::PARAM_STR); $sth->bindParam(':voice', $voice, \PDO::PARAM_STR); $sth->bindParam(':caption', $caption, \PDO::PARAM_STR); $sth->bindParam(':contact', $contanct, \PDO::PARAM_STR); $sth->bindParam(':location', $location, \PDO::PARAM_STR); $sth->bindParam(':new_chat_participant', $new_chat_paticipant, \PDO::PARAM_STR); $sth->bindParam(':left_chat_participant', $left_chat_paticipant, \PDO::PARAM_STR); $sth->bindParam(':new_chat_title', $new_chat_title, \PDO::PARAM_STR); //Array of Photosize $var = []; if (is_array($new_chat_photo)) { foreach ($new_chat_photo as $elm) { $var[] = json_decode($elm, true); } $new_chat_photo = json_encode($var); } else { $new_chat_photo = ''; } $sth->bindParam(':new_chat_photo', $new_chat_photo, \PDO::PARAM_STR); $sth->bindParam(':delete_chat_photo', $delete_chat_photo, \PDO::PARAM_STR); $sth->bindParam(':group_chat_created', $group_chat_created, \PDO::PARAM_STR); $sth->bindParam(':supergroup_chat_created', $migrate_from_chat_id, \PDO::PARAM_INT); $sth->bindParam(':channel_chat_created', $supergroup_chat_created, \PDO::PARAM_INT); $sth->bindParam(':migrate_from_chat_id', $channel_chat_created, \PDO::PARAM_INT); $sth->bindParam(':migrate_to_chat_id', $migrate_from_chat_id, \PDO::PARAM_INT); $status = $sth->execute(); } catch (PDOException $e) { throw new TelegramException($e->getMessage()); } return true; }
/** * setUp */ public function setUp() { $this->telegram = new Telegram('apikey', 'testbot'); $this->telegram->addCommandsPath(BASE_COMMANDS_PATH . '/UserCommands'); $this->telegram->getCommandsList(); }
/** * @test */ public function getBotName() { $this->assertEquals('testbotname', $this->telegram->getBotName()); }