-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtelegram_utils.py
78 lines (56 loc) · 1.88 KB
/
telegram_utils.py
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
from __future__ import print_function
import socket
import logging
import requests
from django.conf import settings
logger = logging.getLogger('nextgis_common.telegram')
class SimpleTelegramBot:
API_URL = 'https://api.telegram.org/'
def __init__(self, bot_id):
self.bot_id = bot_id
@property
def bot_url(self):
return self.API_URL + 'bot%s' % self.bot_id
def command_url(self, command):
return self.bot_url + '/' + command
def send_message(self, chat_id, message, parse_mode='HTML'):
url = self.command_url('sendMessage')
data = {
'chat_id': chat_id,
'text': message,
'parse_mode': parse_mode
}
try:
requests.post(url, data=data)
except requests.RequestException as e:
logger.error("SimpleTelegramBot exception: %s" % e)
def construct_message(html_msg, add_header=True):
message = ''
separator = '-' * 36
if add_header:
if hasattr(settings, 'TELEGRAM_MSG_HEADER'):
header = settings.TELEGRAM_MSG_HEADER
else:
header = socket.getfqdn()
header += ' (dev)' if settings.DEBUG else ''
message += '<b>%s</b>\n%s\n' % (header, separator)
if isinstance(html_msg, list):
html_msg_parts = html_msg
else:
html_msg_parts = [html_msg]
separator_with_new_lines = '\n%s\n' % separator
message += separator_with_new_lines.join(html_msg_parts)
return message
def send_message(html_msg):
if settings.TELEGRAM_TOKEN is None:
return
if settings.TELEGRAM_CHAT_ID is None:
return
bot = SimpleTelegramBot(settings.TELEGRAM_TOKEN)
if getattr(settings, 'TELEGRAM_PRINT_ONLY', False):
print(construct_message(html_msg))
return
bot.send_message(
settings.TELEGRAM_CHAT_ID,
construct_message(html_msg)
)