Skip to content

Commit fe4c545

Browse files
committed
Merge pull request php-telegram-bot#202 from jacklul/quickupdate
Quick updates
2 parents 1549a4f + 0fcd30a commit fe4c545

File tree

5 files changed

+224
-155
lines changed

5 files changed

+224
-155
lines changed

src/Botan.php

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class Botan
3434
*/
3535
protected static $token = '';
3636

37+
/**
38+
* @var string The actual command that is going to be reported
39+
*/
40+
public static $command = '';
41+
3742
/**
3843
* Initilize botan
3944
*/
@@ -47,40 +52,57 @@ public static function initializeBotan($token)
4752
}
4853

4954
/**
50-
* Track function
55+
* Lock function to make sure only the first command is reported
56+
* ( in case commands are calling other commands $telegram->executedCommand() )
5157
*
52-
* @todo Advanced integration: https://github.com/botanio/sdk#advanced-integration
58+
* @param string $command
59+
*/
60+
public static function lock($command = '')
61+
{
62+
if (empty(self::$command)) {
63+
self::$command = $command;
64+
}
65+
}
66+
67+
/**
68+
* Track function
5369
*
5470
* @param string $input
5571
* @param string $command
72+
*
5673
* @return bool|string
74+
* @throws TelegramException
5775
*/
5876
public static function track($input, $command = '')
5977
{
60-
if (empty(self::$token)) {
78+
if (empty(self::$token) || $command != self::$command) {
6179
return false;
6280
}
6381

6482
if (empty($input)) {
6583
throw new TelegramException('Input is empty!');
6684
}
6785

86+
self::$command = '';
87+
6888
$obj = json_decode($input, true);
6989
if (isset($obj['message'])) {
7090
$data = $obj['message'];
71-
72-
if ((isset($obj['message']['entities']) && $obj['message']['entities'][0]['type'] == 'bot_command' && $obj['message']['entities'][0]['offset'] == 0) || substr($obj['message']['text'], 0, 1) == '/') {
73-
if (strtolower($command) == 'generic') {
74-
$command = 'Generic';
75-
} elseif (strtolower($command) == 'genericmessage') {
76-
$command = 'Generic Message';
77-
} else {
78-
$command = '/' . strtolower($command);
91+
$event_name = 'Message';
92+
93+
if (isset($obj['message']['entities']) && is_array($obj['message']['entities'])) {
94+
foreach ($obj['message']['entities'] as $entity) {
95+
if ($entity['type'] == 'bot_command' && $entity['offset'] == 0) {
96+
if (strtolower($command) == 'generic') {
97+
$command = 'Generic';
98+
} else {
99+
$command = '/' . strtolower($command);
100+
}
101+
102+
$event_name = 'Command (' . $command . ')';
103+
break;
104+
}
79105
}
80-
81-
$event_name = 'Command ('.$command.')';
82-
} else {
83-
$event_name = 'Message';
84106
}
85107
} elseif (isset($obj['inline_query'])) {
86108
$data = $obj['inline_query'];
@@ -129,7 +151,9 @@ public static function track($input, $command = '')
129151
*
130152
* @param $url
131153
* @param $user_id
154+
*
132155
* @return string
156+
* @throws TelegramException
133157
*/
134158
public static function shortenUrl($url, $user_id)
135159
{

src/Commands/SystemCommands/InlinequeryCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public function execute()
4040
$data = ['inline_query_id' => $inline_query->getId()];
4141

4242
$articles = [
43-
['id' => '001', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => $query ]) ],
44-
['id' => '002', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => $query ]) ],
45-
['id' => '003', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => $query ]) ],
43+
['id' => '001', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => ' ' . $query ])],
44+
['id' => '002', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => ' ' . $query ])],
45+
['id' => '003', 'title' => 'https://core.telegram.org/bots/api#answerinlinequery', 'message_text' => 'you enter: ' . $query , 'input_message_content' => new InputTextMessageContent([ 'message_text' => ' ' . $query ])],
4646
];
4747

4848
$array_article = [];

0 commit comments

Comments
 (0)