-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull.php
50 lines (40 loc) · 1.35 KB
/
pull.php
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
<?php
/*
* Telegram Bot Sample
* ===================
* UWiClab, University of Urbino
* ===================
* Basic message processing in pull mode for your bot.
* Start editing here. =)
*/
include('lib.php');
// Reload latest update ID received (if any) from persistent store
$last_update = @file_get_contents(dirname(__FILE__) . '/pull-last-update.txt');
// Fetch updates from API
// Note: we remember 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, 60);
if($content === false) {
Logger::fatal('Failed to fetch updates from API', __FILE__);
}
if(count($content) == 0) {
Logger::debug('No new messages', __FILE__);
exit;
}
$update = $content[0];
Logger::debug('New update received: ' . print_r($update, true), __FILE__);
// Updates have the following structure:
// [
// {
// "update_id": 123456789,
// "message": {
// ** message object **
// }
// }
// ]
$update_id = $update['update_id'];
// Update persistent store with latest update ID received
file_put_contents(dirname(__FILE__) . '/pull-last-update.txt', $update_id);
include 'msg_processing_simple.php';
?>