Commit fe4c5455 authored by Jack'lul's avatar Jack'lul

Merge pull request #202 from jacklul/quickupdate

Quick updates
parents 1549a4fd 0fcd30a8
...@@ -34,6 +34,11 @@ class Botan ...@@ -34,6 +34,11 @@ class Botan
*/ */
protected static $token = ''; protected static $token = '';
/**
* @var string The actual command that is going to be reported
*/
public static $command = '';
/** /**
* Initilize botan * Initilize botan
*/ */
...@@ -47,17 +52,30 @@ class Botan ...@@ -47,17 +52,30 @@ class Botan
} }
/** /**
* Track function * Lock function to make sure only the first command is reported
* ( in case commands are calling other commands $telegram->executedCommand() )
* *
* @todo Advanced integration: https://github.com/botanio/sdk#advanced-integration * @param string $command
*/
public static function lock($command = '')
{
if (empty(self::$command)) {
self::$command = $command;
}
}
/**
* Track function
* *
* @param string $input * @param string $input
* @param string $command * @param string $command
*
* @return bool|string * @return bool|string
* @throws TelegramException
*/ */
public static function track($input, $command = '') public static function track($input, $command = '')
{ {
if (empty(self::$token)) { if (empty(self::$token) || $command != self::$command) {
return false; return false;
} }
...@@ -65,22 +83,26 @@ class Botan ...@@ -65,22 +83,26 @@ class Botan
throw new TelegramException('Input is empty!'); throw new TelegramException('Input is empty!');
} }
self::$command = '';
$obj = json_decode($input, true); $obj = json_decode($input, true);
if (isset($obj['message'])) { if (isset($obj['message'])) {
$data = $obj['message']; $data = $obj['message'];
$event_name = 'Message';
if ((isset($obj['message']['entities']) && $obj['message']['entities'][0]['type'] == 'bot_command' && $obj['message']['entities'][0]['offset'] == 0) || substr($obj['message']['text'], 0, 1) == '/') {
if (strtolower($command) == 'generic') { if (isset($obj['message']['entities']) && is_array($obj['message']['entities'])) {
$command = 'Generic'; foreach ($obj['message']['entities'] as $entity) {
} elseif (strtolower($command) == 'genericmessage') { if ($entity['type'] == 'bot_command' && $entity['offset'] == 0) {
$command = 'Generic Message'; if (strtolower($command) == 'generic') {
} else { $command = 'Generic';
$command = '/' . strtolower($command); } else {
$command = '/' . strtolower($command);
}
$event_name = 'Command (' . $command . ')';
break;
}
} }
$event_name = 'Command ('.$command.')';
} else {
$event_name = 'Message';
} }
} elseif (isset($obj['inline_query'])) { } elseif (isset($obj['inline_query'])) {
$data = $obj['inline_query']; $data = $obj['inline_query'];
...@@ -129,7 +151,9 @@ class Botan ...@@ -129,7 +151,9 @@ class Botan
* *
* @param $url * @param $url
* @param $user_id * @param $user_id
*
* @return string * @return string
* @throws TelegramException
*/ */
public static function shortenUrl($url, $user_id) public static function shortenUrl($url, $user_id)
{ {
......
...@@ -40,9 +40,9 @@ class InlinequeryCommand extends SystemCommand ...@@ -40,9 +40,9 @@ class InlinequeryCommand extends SystemCommand
$data = ['inline_query_id' => $inline_query->getId()]; $data = ['inline_query_id' => $inline_query->getId()];
$articles = [ $articles = [
['id' => '001', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => $query ]) ], ['id' => '001', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => ' ' . $query ])],
['id' => '002', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => $query ]) ], ['id' => '002', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => ' ' . $query ])],
['id' => '003', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => $query ]) ], ['id' => '003', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => ' ' . $query ])],
]; ];
$array_article = []; $array_article = [];
......
This diff is collapsed.
...@@ -515,11 +515,16 @@ class Telegram ...@@ -515,11 +515,16 @@ class Telegram
//Handle a generic command or non existing one //Handle a generic command or non existing one
$this->last_command_response = $this->executeCommand('Generic'); $this->last_command_response = $this->executeCommand('Generic');
} else { } else {
//Botan.io integration, make sure only the command user executed is reported
if ($this->botan_enabled) {
Botan::lock($command);
}
//execute() method is executed after preExecute() //execute() method is executed after preExecute()
//This is to prevent executing a DB query without a valid connection //This is to prevent executing a DB query without a valid connection
$this->last_command_response = $command_obj->preExecute(); $this->last_command_response = $command_obj->preExecute();
//Botan.io integration //Botan.io integration, send report after executing the command
if ($this->botan_enabled) { if ($this->botan_enabled) {
Botan::track($this->update, $command); Botan::track($this->update, $command);
} }
...@@ -582,9 +587,11 @@ class Telegram ...@@ -582,9 +587,11 @@ class Telegram
$user_id = $from->getId(); $user_id = $from->getId();
} elseif (($inline_query = $this->update->getInlineQuery()) && ($from = $inline_query->getFrom())) { } elseif (($inline_query = $this->update->getInlineQuery()) && ($from = $inline_query->getFrom())) {
$user_id = $from->getId(); $user_id = $from->getId();
} elseif (($chosen_inline_result = $this->update->getChosenInlineResult()) && ($from = $chosen_inline_result->getFrom())) {
$user_id = $from->getId();
} elseif (($callback_query = $this->update->getCallbackQuery()) && ($from = $callback_query->getFrom())) { } elseif (($callback_query = $this->update->getCallbackQuery()) && ($from = $callback_query->getFrom())) {
$user_id = $from->getId(); $user_id = $from->getId();
} elseif (($chosen_inline_result = $this->update->getChosenInlineResult()) && ($from = $chosen_inline_result->getFrom())) { } elseif (($edited_message = $this->update->getEditedMessage()) && ($from = $edited_message->getFrom())) {
$user_id = $from->getId(); $user_id = $from->getId();
} }
} }
...@@ -808,7 +815,6 @@ class Telegram ...@@ -808,7 +815,6 @@ class Telegram
* Enable Botan.io integration * Enable Botan.io integration
* *
* @param $token * @param $token
* @param $custom
* @return Telegram * @return Telegram
*/ */
public function enableBotan($token) public function enableBotan($token)
......
This diff is collapsed.
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