-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunctions.php
82 lines (65 loc) · 1.66 KB
/
functions.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
<?php
const DATA_FILE = __DIR__.'/data/storage.json';
const BASE_URL = 'https://api.telegram.org/bot'.BOT_TOKEN.'/';
function generateUniqueCode()
{
return bin2hex(random_bytes(8));
}
function storeUsernameAndUniqueCode($username, $token)
{
$storage = getStorage();
$storage[$token] = $username;
saveStorage($storage);
}
function getUsernameByUniqueCode($token)
{
$storage = getStorage();
if (array_key_exists($token, $storage)) {
return $storage[$token];
}
return null;
}
function getStorage()
{
if (file_exists(DATA_FILE)) {
$contents = file_get_contents(DATA_FILE);
return json_decode($contents, true);
}
return [];
}
function saveStorage(array $storage)
{
file_put_contents(DATA_FILE, json_encode($storage, JSON_PRETTY_PRINT));
}
function getUpdates($offset)
{
$query = http_build_query(['offset' => $offset, 'timeout' => 20, 'allowed_updates' => ['message']]);
$url = BASE_URL.'getUpdates?'.$query;
return makeRequest($url);
}
function sendMessage($chatId, $text)
{
$query = http_build_query(['chat_id' => $chatId, 'text' => $text]);
$url = BASE_URL.'sendMessage?'.$query;
return makeRequest($url);
}
function makeRequest($url)
{
$result = json_decode(file_get_contents($url), true);
if ($result['ok'] !== true) {
throw new RuntimeException('Request failed. Telegram API reported: '.$result['description']);
}
return $result['result'];
}
function e($v)
{
return htmlspecialchars($v);
}
function str_startswith($str, $val)
{
return strpos($str, $val) === 0;
}
function println($message, ...$args)
{
echo sprintf($message, ...$args).PHP_EOL;
}