1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* This is a simple Telegram bot which, given a message, returns all information |
4
|
|
|
* about the Telegram user who send it. |
5
|
|
|
* |
6
|
|
|
* Change the token to reflect your bot's one. |
7
|
|
|
*/ |
8
|
|
|
require_once '../../vendor/autoload.php'; |
9
|
|
|
|
10
|
|
|
class WhoAmIBot extends PhpBotFramework\Bot { |
|
|
|
|
11
|
|
|
// Override 'processMessage' in order to intercept the message and |
12
|
|
|
// get information about its author (if forwarded, its original author). |
13
|
|
|
protected function processMessage($message) { |
14
|
|
|
// Check if the message was forward |
15
|
|
|
isset($message['forward_from']) ? $index = 'forward_from' : $index = 'from'; |
16
|
|
|
|
17
|
|
|
$response = $this->prepareResponse($message[$index]); |
18
|
|
|
$this->sendMessage($response); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
private function prepareResponse($user) { |
22
|
|
|
return '<strong>Message sent by </strong>' . $user['first_name'] . "\n" . |
23
|
|
|
'<strong>User ID: </strong>' . $user['id'] . "\n" . |
24
|
|
|
'<strong>Username: </strong>' . $user['username'] . "\n"; |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$bot = new WhoAmIBot('BOT_TOKEN'); |
29
|
|
|
|
30
|
|
|
// Add a welcome message |
31
|
|
|
$bot->addMessageCommand('start', function ($bot, $message) { |
|
|
|
|
32
|
|
|
$bot->sendMessage('<strong>Hey there!</strong> Send or forward me a text message :)'); |
33
|
|
|
} |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
// Add various commands at once |
37
|
|
|
$about_command = new PhpBotFramework\Commands\MessageCommand('about', function($bot, $message) { |
|
|
|
|
38
|
|
|
$bot->sendMessage('Powered by PhpBotFramework'); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
$codename_command = new PhpBotFramework\Commands\MessageCommand('codename', function($bot, $message) { |
|
|
|
|
42
|
|
|
$bot->sendMessage('Iron Bird'); |
43
|
|
|
}); |
44
|
|
|
|
45
|
|
|
$bot->addCommands($about_command, $codename_command); |
46
|
|
|
|
47
|
|
|
$bot->getUpdatesLocal(); |
48
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.