forked from DigiPlatMOOC/TelegramBotSample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpull.php
More file actions
52 lines (43 loc) · 1.34 KB
/
pull.php
File metadata and controls
52 lines (43 loc) · 1.34 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
<?php
/*
* Telegram Bot Sample
* ===================
* UWiClab, University of Urbino
* ===================
* Basic message processing in pull mode for your bot.
* Start editing here. =)
*/
include ('lib_msg_processing.php');
// Reload latest update ID received (if any) from persistent store
$last_update = file_get_contents("pull-last-update.txt");
// Fetch updates from API
// Note: we rememer the last fetched ID and query for the next one, if available.
// The third parameter enabled long-polling. Switch to any number of seconds
// to enable (the request will hang until timeout or until a message is received).
$content = telegram_get_updates(intval($last_update) + 1, 1, false);
if($content === false) {
error_log('Failed to fetch updates from API');
exit;
}
if(count($content) == 0) {
echo ('No new messages.' . PHP_EOL);
exit;
}
$first_update = $content[0];
echo ('New update received:' . PHP_EOL);
print_r($first_update);
// Updates have the following structure:
// [
// {
// "update_id": 123456789,
// "message": {
// ** message object **
// }
// }
// ]
$update_id = $first_update['update_id'];
$message = $first_update['message'];
// Update persistent store with latest update ID received
file_put_contents("pull-last-update.txt", $update_id);
process_message($message);
?>