@@ -34,6 +34,11 @@ class Botan
34
34
*/
35
35
protected static $ token = '' ;
36
36
37
+ /**
38
+ * @var string The actual command that is going to be reported
39
+ */
40
+ public static $ command = '' ;
41
+
37
42
/**
38
43
* Initilize botan
39
44
*/
@@ -47,40 +52,57 @@ public static function initializeBotan($token)
47
52
}
48
53
49
54
/**
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() )
51
57
*
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
53
69
*
54
70
* @param string $input
55
71
* @param string $command
72
+ *
56
73
* @return bool|string
74
+ * @throws TelegramException
57
75
*/
58
76
public static function track ($ input , $ command = '' )
59
77
{
60
- if (empty (self ::$ token )) {
78
+ if (empty (self ::$ token ) || $ command != self :: $ command ) {
61
79
return false ;
62
80
}
63
81
64
82
if (empty ($ input )) {
65
83
throw new TelegramException ('Input is empty! ' );
66
84
}
67
85
86
+ self ::$ command = '' ;
87
+
68
88
$ obj = json_decode ($ input , true );
69
89
if (isset ($ obj ['message ' ])) {
70
90
$ 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
+ }
79
105
}
80
-
81
- $ event_name = 'Command ( ' .$ command .') ' ;
82
- } else {
83
- $ event_name = 'Message ' ;
84
106
}
85
107
} elseif (isset ($ obj ['inline_query ' ])) {
86
108
$ data = $ obj ['inline_query ' ];
@@ -129,7 +151,9 @@ public static function track($input, $command = '')
129
151
*
130
152
* @param $url
131
153
* @param $user_id
154
+ *
132
155
* @return string
156
+ * @throws TelegramException
133
157
*/
134
158
public static function shortenUrl ($ url , $ user_id )
135
159
{
0 commit comments