This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
daemon.php
95 lines (73 loc) · 3.01 KB
/
daemon.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Telegram\Bot\Api;
function getTelegramClient()
{
// TODO: Construction has changed in dotenv v3 (the version Laravel 5.8 uses).
$dotenv = Dotenv\Dotenv::create(__DIR__);
$dotenv->load();
return new Api(getenv('TELEGRAM_BOT_TOKEN'));
}
$telegram = getTelegramClient();
echo "Starting bot with config {$telegram->getMe()}\n";
const S = 1000000; // 1000000 microseconds = 1 second.
const POLL_DURATION = S; // In microseconds.
printf("\nStart polling Telegram api (will poll for updates every %d ms)\n\n", POLL_DURATION / 1000);
/**
* Identifier of the first update to be returned.
* Must be greater by one than the highest among the identifiers of previously received updates.
* By default, updates starting with the earliest unconfirmed update are returned.
* An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id.
*/
$offset = null;
function getUpdates($telegram, $offset)
{
if ($offset === null) {
$res = $telegram->getUpdates();
} else {
$res = $telegram->getUpdates(['offset' => $offset]);
}
// Update to the latest offset value.
foreach ($res as $update) {
if ($offset === null || $offset <= $update->getUpdateId()) {
$offset = $update->getUpdateId() + 1;
} else {
continue; // We already processed this update.
}
$payload = json_encode($update);
echo "\n\n";
echo "---------------------------------------------------------------------------------------\n";
echo $payload; // Log the updates to the console.
echo "\n\n";
// Send the updates to the Laravel application, just as if Telegram had used our webhook.
$ch = curl_init('http://caddy/api/' . getenv('TELEGRAM_BOT_TOKEN'));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response instead of printing.
$response = curl_exec($ch);
if ($response === false) { // Error with cURL. Probably something misconfigured in execution environment.
echo 'cURL failed with error '. curl_error($ch) . ":\n" . curl_errno($ch) . "\n";
}
curl_close($ch);
// Print Laravel response.
var_dump($response);
echo "---------------------------------------------------------------------------------------\n";
echo "\n\n";
}
return $offset;
}
while (true) {
$start = (int) microtime(true) * S;
// get updates here
$offset = getUpdates($telegram, $offset);
// end get updates
$end = (int) microtime(true) * S;
$poll_took = $end - $start;
$slept = 0;
if ($poll_took < POLL_DURATION) {
$slept = POLL_DURATION - $poll_took;
usleep($slept);
}
printf("Poll took %d ms (slept %d µs)\n", $poll_took, $slept);
}