Issues (2963)

LibreNMS/Alert/Transport/Telegram.php (1 issue)

1
<?php
2
/**
3
 * transport-telegram.inc.php
4
 *
5
 * LibreNMS Telegram alerting transport
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2017 Neil Lathwood
23
 * @author     Neil Lathwood <[email protected]>
24
 */
25
26
namespace LibreNMS\Alert\Transport;
27
28
use LibreNMS\Alert\Transport;
29
30
class Telegram extends Transport
31
{
32
    public function deliverAlert($obj, $opts)
33
    {
34
        $telegram_opts['chat_id'] = $this->config['telegram-chat-id'];
35
        $telegram_opts['token'] = $this->config['telegram-token'];
36
        $telegram_opts['format'] = $this->config['telegram-format'];
37
38
        return $this->contactTelegram($obj, $telegram_opts);
39
    }
40
41
    public static function contactTelegram($obj, $data)
42
    {
43
        $curl = curl_init();
44
        set_curl_proxy($curl);
45
        $text = urlencode($obj['msg']);
46
        $format = '';
47
        if ($data['format']) {
48
            $format = '&parse_mode=' . $data['format'];
49
            if ($data['format'] == 'Markdown') {
50
                $text = urlencode(preg_replace('/([a-z0-9]+)_([a-z0-9]+)/', "$1\_$2", $obj['msg']));
51
            }
52
        }
53
        curl_setopt($curl, CURLOPT_URL, ("https://api.telegram.org/bot{$data['token']}/sendMessage?chat_id={$data['chat_id']}&text=$text{$format}"));
54
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
55
        $ret = curl_exec($curl);
56
        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
57
        if ($code != 200) {
58
            var_dump('Telegram returned Error'); //FIXME: propper debuging
0 ignored issues
show
Security Debugging Code introduced by
var_dump('Telegram returned Error') looks like debug code. Are you sure you do not want to remove it?
Loading history...
59
            var_dump('Return: ' . $ret); //FIXME: propper debuging
60
61
            return 'HTTP Status code ' . $code . ', Body ' . $ret;
62
        }
63
64
        return true;
65
    }
66
67
    public static function configTemplate()
68
    {
69
        return [
70
            'config' => [
71
                [
72
                    'title' => 'Chat ID',
73
                    'name' => 'telegram-chat-id',
74
                    'descr' => 'Telegram Chat ID',
75
                    'type' => 'text',
76
                ],
77
                [
78
                    'title' => 'Token',
79
                    'name' => 'telegram-token',
80
                    'descr' => 'Telegram Token',
81
                    'type' => 'text',
82
                ],
83
                [
84
                    'title' => 'Format',
85
                    'name' => 'telegram-format',
86
                    'descr' => 'Telegram format',
87
                    'type' => 'select',
88
                    'options' => [
89
                        '' => '',
90
                        'Markdown' => 'Markdown',
91
                        'HTML' => 'HTML',
92
                    ],
93
                ],
94
            ],
95
            'validation' => [
96
                'telegram-chat-id' => 'required|string',
97
                'telegram-token' => 'required|string',
98
                'telegram-format' => 'string',
99
            ],
100
        ];
101
    }
102
}
103