Skip to content

Commit 69a3648

Browse files
committed
V1.6.0
Adicionado logs de interação.
1 parent 2edd5e7 commit 69a3648

File tree

9 files changed

+145
-26
lines changed

9 files changed

+145
-26
lines changed

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,30 @@ $file = Methods::getFile ([
315315
var_dump ($bot->saveFile ($file, __DIR__.'/music.mp3'));
316316
```
317317

318-
### 🔥 [Envie os seus](https://t.me/httd1) bots feito com esse pacote, ele pode ser listado aqui!
318+
## Logs
319+
Você pode obter logs de interação com o bot, basta usar a classe estática ```\TelegramPhp\Config\Logs```, por ela você pode definir uma ou mais classes que irão receber e processar os dados de interação do usuário.
320+
321+
- Classe responsável por processar os logs.
322+
```php
323+
class LogCommands {
324+
// method log is required
325+
public function log ($telegramPhp, $action, $route, $data){
326+
// process data
327+
}
328+
}
329+
```
330+
331+
- Definindo a classe que irá processar os logs.
332+
```php
333+
\TelegramPhp\Config\Logs::catchLogs ([
334+
LogCommands::class,
335+
// LogStatistics::class
336+
]);
337+
```
338+
339+
### 🔥 [Envie os bots feitos com esse pacote](https://t.me/httd1), ele pode ser listado aqui!
319340
• J.M
320-
- [@scdownbot](https://t.me/scdownbot) (+5K Usuários)
321-
- [@twitterdlrobot](https://t.me/twitterdlrobot) (+1K Usuários)
322-
- [@rastreiorobot](https://t.me/rastreiorobot) (+3K Usuários)
323-
- [@btn_bot](https://t.me/btn_bot) (+500 Usuários)
341+
- [@scdownbot](https://t.me/scdownbot) (+18K Usuários)
342+
- [@twitterdlrobot](https://t.me/twitterdlrobot) (+11K Usuários)
343+
- [@rastreiorobot](https://t.me/rastreiorobot) (+14K Usuários)
344+
- [@btn_bot](https://t.me/btn_bot) (+200 Usuários)

examples/MarkdownBot/MarkdownBot.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use \TelegramPhp\Methods;
4+
use \TelegramPhp\Reaction;
45

56
class MarkdownBot {
67

examples/MarkdownBot/bot.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<?php
22

33
include __DIR__.'/../../vendor/autoload.php';
4+
include __DIR__.'/logs/LogComandos.php';
45
include __DIR__.'/MarkdownBot.php';
56

67
use \TelegramPhp\TelegramPhp;
7-
use \TelegramPhp\Methods;
8-
use \TelegramPhp\Buttons;
98

109
\TelegramPhp\Config\Token::setToken ('343008038:AAGIgeZ6jG6EDJwxU5Slu1KrC2LHxwe1Nlk');
1110

1211
$tlg = new TelegramPhp ();
1312

13+
\TelegramPhp\Config\Logs::catchLogs ([
14+
LogComandos::class,
15+
]);
16+
1417
// debug
1518
$tlg->setContent ('{
1619
"update_id":264419866,
@@ -43,6 +46,6 @@
4346

4447
$tlg->command ('/start', 'MarkdownBot:start');
4548
$tlg->command ('/help', 'MarkdownBot:help');
46-
$tlg->commandMatch ('/^[^\/]+/', 'MarkdownBot:markdownText');
49+
$tlg->commandMatch ('/^(?<texto>[^\/]+)/', 'MarkdownBot:markdownText');
4750

4851
$tlg->commandDefault ('MarkdownBot:defaultResponse');
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use \TelegramPhp\Methods;
4+
5+
class LogComandos {
6+
7+
// method log required
8+
public function log ($tlg, $action, $route, $data)
9+
{
10+
11+
$log = "\n------- LOG ---------\n";
12+
$log .= "Text: {$tlg->getText()}\n";
13+
$log .= "Route: {$route}\n";
14+
$log .= "Action: {$action}\n";
15+
$log .= "Data: " . json_encode($data) . "\n";
16+
$log .= "Usuário: {$tlg->getFirstName()} - {$tlg->getUserId()}\n";
17+
18+
Methods::sendMessage ([
19+
'chat_id' => '275123569',
20+
'text' => $log
21+
]);
22+
23+
}
24+
25+
}

src/Config/Logs.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace TelegramPhp\Config;
4+
5+
class Logs {
6+
7+
/**
8+
* @var array
9+
*/
10+
static $logCommands = [];
11+
12+
/**
13+
* Set the class to capture logs of commands.
14+
* **It's required that you define the _log_ method in your class.**
15+
*
16+
* @param array|string $class
17+
*
18+
* @return void
19+
*/
20+
static function catchLogs (string|array $class) :void
21+
{
22+
23+
if (!\is_array ($class)){
24+
$class = [$class];
25+
}
26+
self::$logCommands = [...$class];
27+
28+
}
29+
}

src/Config/Token.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
class Token {
66

7+
/**
8+
* @var string
9+
*/
710
static $token;
811

912
/**

src/Methods.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static function deleteWebhook (array $data) :array
8888
*/
8989
public static function getWebhookInfo () :array
9090
{
91-
return self::call (__FUNCTION__, $data);
91+
return self::call (__FUNCTION__, []);
9292
}
9393

9494
/**

src/Reaction.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Reaction {
1111
*
1212
* @return string
1313
*/
14-
public static function reactionType (array $reaction)
14+
public static function reactionType (array $reaction) :string
1515
{
1616
return \json_encode ($reaction);
1717
}
@@ -25,7 +25,7 @@ public static function reactionType (array $reaction)
2525
*
2626
* @return array
2727
*/
28-
public static function reactionTypeEmoji (string $emoji)
28+
public static function reactionTypeEmoji (string $emoji) :array
2929
{
3030
return [
3131
'type' => 'emoji',
@@ -42,7 +42,7 @@ public static function reactionTypeEmoji (string $emoji)
4242
*
4343
* @return array
4444
*/
45-
public static function reactionTypeCustomEmoji (string $custom_emoji_id)
45+
public static function reactionTypeCustomEmoji (string $custom_emoji_id) :array
4646
{
4747
return [
4848
'type' => 'custom_emoji',

src/TelegramPhp.php

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace TelegramPhp;
44

55
use \TelegramPhp\Config\Token;
6-
use \TelegramPhp\Request\Request;
6+
use \TelegramPhp\Config\Logs;
77

88
/**
99
* Package for Telegram Bot API in php, TelegramPhp
@@ -39,7 +39,7 @@ function __construct ()
3939

4040
if (!\version_compare (PHP_VERSION, 7.0, '>='))
4141
{
42-
throw new Exception ('Unsupported version!');
42+
throw new \Exception ('Unsupported version!');
4343
}
4444

4545
$this->token = Token::$token;
@@ -58,15 +58,16 @@ public function initContent () :void
5858
/**
5959
* Command default executed when there aren't command defined.
6060
*
61-
* @param $action
61+
* @param callable|string $action
6262
*
6363
* @return void
6464
*/
65-
public function commandDefault ($action) :void
65+
public function commandDefault (callable|string $action) :void
6666
{
6767

6868
if ($this->hasCommand == false && !empty ($this->getText ())){
6969
$this->runAction ($action);
70+
$this->callLogs ($action, 'default', []);
7071
}
7172

7273
}
@@ -75,11 +76,11 @@ public function commandDefault ($action) :void
7576
* Execute a callback to a command.
7677
*
7778
* @param string $route
78-
* @param $action
79+
* @param callable|string $action
7980
*
8081
* @return void
8182
*/
82-
public function command (string $route, $action) :void
83+
public function command (string $route, callable|string $action) :void
8384
{
8485

8586
if (empty ($route)){
@@ -100,13 +101,15 @@ public function command (string $route, $action) :void
100101
// /comando é o mesmo em $this->getText () e $route
101102
if ($route_command ['command'] == $text_command ['command']){
102103
$this->runAction ($action, $data);
104+
$this->callLogs ($action, $route, $data);
103105
return;
104106
}
105107
}
106108
}else {
107109
// é uma mensagem normal, compara com o route
108110
if ($this->getText () == $route){
109111
$this->runAction ($action, []);
112+
$this->callLogs ($action, $route, []);
110113
return;
111114
}
112115
}
@@ -116,11 +119,11 @@ public function command (string $route, $action) :void
116119
* Executes a regular expression on a text sent to the bot.
117120
*
118121
* @param string $regex
119-
* @param $action
122+
* @param callable|string $action
120123
*
121124
* @return void
122125
*/
123-
public function commandMatch (string $regex, $action) :void
126+
public function commandMatch (string $regex, callable|string $action) :void
124127
{
125128

126129
if (empty ($regex)){
@@ -133,29 +136,63 @@ public function commandMatch (string $regex, $action) :void
133136

134137
if (!empty ($match)){
135138
$this->runAction ($action, $match);
139+
$this->callLogs ($action, $regex, $match);
136140
return;
137141
}
138142
}
139143

140144
}
141145

142146
/**
143-
* @param $action
147+
* @param callable|string $action
144148
* @param array $data
145149
*/
146-
function runAction ($action, array $data = [])
150+
function runAction (callable|string $action, array $data = []) :void
147151
{
148152
if (is_callable ($action)){
149153
$action ($this, $data);
150154
}else {
151-
list ($class, $method) = $this->match ('/[^:]+/', $action, true);
152-
$obj = new $class;
153-
$obj->$method ($this, $data);
155+
list ($class, $method) = $this->match ('/[^:@]+/', $action, true);
156+
// classe existe!
157+
if (\class_exists ($class)){
158+
159+
$obj = new $class;
160+
161+
// método existe!
162+
if (\method_exists ($obj, $method)){
163+
$obj->$method ($this, $data); //chama método
164+
}else {
165+
throw new \Exception ("Method \"{$method}\" doesn't exist in '{$class}' class");
166+
}
167+
168+
}else {
169+
throw new \Exception ("Class \"{$class}\" doesn't exist");
170+
}
154171
}
155172

156173
if ($this->hasCommand == false) $this->hasCommand = true;
157174
}
158175

176+
/**
177+
* Call log class
178+
*
179+
* @param callable|string $action
180+
* @param string $route
181+
* @param array $data
182+
*
183+
* @return void
184+
*/
185+
public function callLogs (callable|string $action, string $route, array $data) :void
186+
{
187+
188+
foreach (Logs::$logCommands as $log){
189+
$clssLog = new $log;
190+
$action = \is_callable ($action) ? 'function' : $action;
191+
$clssLog->log ($this, $action, $route, $data);
192+
}
193+
194+
}
195+
159196
/**
160197
* Verifies the secret token sent in an “X-Telegram-Bot-Api-Secret-Token” header on each webhook request, from 1 to 256 characters.
161198
* The header is useful to ensure that the request comes from a webhook you define.
@@ -194,7 +231,7 @@ public function getSecretToken () :string
194231
*
195232
* @return void
196233
*/
197-
public function setSecretToken (string $secretToken)
234+
public function setSecretToken (string $secretToken) :void
198235
{
199236
$this->secret_token = $secretToken;
200237
}

0 commit comments

Comments
 (0)