summaryrefslogtreecommitdiff
path: root/localwebsite/classes/TelegramBotClient.php
blob: b9583eebdc529c69626263a6bfb311a964d5eb5b (plain)
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
<?php

class TelegramBotClient {

    protected string $token;

    public function __construct(string $token) {
        $this->token = $token;
    }

    public function sendMessage(int $chat_id, string $text): bool {
        $ch = curl_init();
        $url = 'https://api.telegram.org/bot'.$this->token.'/sendMessage';
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_POSTFIELDS, [
            'chat_id' => $chat_id,
            'text' => $text,
            'parse_mode' => 'html',
            'disable_web_page_preview' => 1
        ]);
        $body = curl_exec($ch);
        curl_close($ch);

        $resp = jsonDecode($body);
        if (!$resp['ok']) {
            debugError(__METHOD__ . ': ' . $body);
            return false;
        }

        return true;
    }

}