forked from DigiPlatMOOC/TelegramBotSample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_msg_processing.php
More file actions
67 lines (61 loc) · 1.7 KB
/
lib_msg_processing.php
File metadata and controls
67 lines (61 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/*
* Telegram Bot Sample
* ===================
* UWiClab, University of Urbino
* ===================
* Basic message processing functionality,
* used by both pull and push scripts.
*
* Put your custom bot intelligence here!
*/
include ('lib.php');
/**
* Processes a message received through the Telegram API.
* @param object $message A message object as parsed from the Telegram API.
* @return Nothing.
*/
function process_message($message) {
// Extract important information from the message object
//
// Message object structure: {
// "message_id": 123,
// "from": {
// "id": 123456789,
// "first_name": "First",
// "last_name": "Last",
// "username": "FirstLast"
// },
// "chat": {
// "id": 123456789,
// "first_name": "First",
// "last_name": "Last",
// "username": "FirstLast",
// "type": "private"
// },
// "date": 1460036220,
// "text": "Text"
// }
$message_id = $message['message_id'];
$chat_id = $message['chat']['id'];
if (isset($message['text'])) {
// We got an incoming text message
$text = $message['text'];
if (strpos($text, "/start") === 0) {
// Start command
// ...do something
echo 'Received /start command!' . PHP_EOL;
}
else {
// Something else
// ...do something else
echo "Received message: $text!" . PHP_EOL;
}
}
else {
if(telegram_send_message($chat_id, 'Sorry, I understand only text messages!', null) === false) {
error_log('Failed to answer in chat');
}
}
}
?>