-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
72 lines (62 loc) · 1.51 KB
/
lib.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
<?php
// SPDX-License-Identifier: GPL-2.0-only
require_once __DIR__."/config.php";
const API_BASE_URL = "https://api.telegram.org/bot".TOKEN_BOT;
function curl(string $url, array $opt = []): ?string
{
$optf = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
]
];
foreach ($opt as $k => $v)
$optf[$k] = $v;
$ch = curl_init($url);
curl_setopt_array($ch, $optf);
$out = curl_exec($ch);
$err = curl_error($ch);
$ern = curl_errno($ch);
curl_close($ch);
if ($err) {
printf("Curl error: %d: %s\n", $ern, $err);
return NULL;
}
return $out;
}
function sendMessage(string $text, int $chatId, array $extra = []): ?array
{
$opt = [
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => json_encode(
[
"text" => $text,
"chat_id" => $chatId
] + $extra
)
];
$out = curl(API_BASE_URL."/sendMessage", $opt);
if (!$out)
return NULL;
return json_decode($out, true);
}
function send_covid19_data(int $chatId, string $country, int $msgId): ?array
{
$raw = file_get_contents(__DIR__."/worldometers_scraper/covid19.json");
$json = json_decode($raw, true);
$ref = &$json[strtolower($country)];
if (!isset($ref)) {
$text = "Country <code>{$country}</code> does not exist";
} else {
$text = "<b>Data COVID-19 for {$country}</b>\n".
"<code>CMT:</code> {$ref["cmt"]}\n".
"<code>FST:</code> {$ref["fst"]}\n".
"<code>SDT:</code> {$ref["sdt"]}";
}
return sendMessage($text, $chatId,
[
"parse_mode" => "HTML",
"reply_to_message_id" => $msgId
]
);
}