-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_handler.py
93 lines (73 loc) · 3.21 KB
/
request_handler.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from enum import Enum
import logging
from socketserver import BaseRequestHandler
class RequestHandler(BaseRequestHandler):
class StringEnum(Enum):
@classmethod
def has_value(cls, value):
return any(value == item.value for item in cls)
class Response(StringEnum):
Error = 'Error'
Success = 'Success'
class Group(StringEnum):
Admin = 'Admin'
class Level(StringEnum):
Error = 'Error'
Warning = 'Warning'
Info = 'Info'
class DataType(StringEnum):
Test = 'Test'
Text = 'Text'
def handle(self):
data = self.request.recv(1024).strip().decode('utf-8')
logging.info('Incoming message from client {}'.format(self.client_address))
logging.info('<< "{}"'.format(data))
# data = 'SenderName|Group|Level|DataType|Message'
# check response structure
if data.count('|') < 4:
self.send_response(RequestHandler.Response.Error, 'Invalid command structure')
return
sender_name, group, level, data_type, message = data.split('|', 4)
group = group.title()
level = level.title()
data_type = data_type.title()
# check sender_name
if sender_name is '':
self.send_response(RequestHandler.Response.Error, 'Empty sender name')
return
# check group
if not RequestHandler.Group.has_value(group):
self.send_response(RequestHandler.Response.Error, 'Invalid group')
return
# check level
if not RequestHandler.Level.has_value(level):
self.send_response(RequestHandler.Response.Error, 'Invalid level')
return
# check data type
if not RequestHandler.DataType.has_value(data_type):
self.send_response(RequestHandler.Response.Error, 'Invalid data type')
return
if RequestHandler.DataType[data_type] is not RequestHandler.DataType.Test:
self.send_telegram_message(sender_name,
RequestHandler.Group[group],
RequestHandler.Level[level],
RequestHandler.DataType[data_type],
message)
self.send_response(RequestHandler.Response.Success)
return
def send_response(self, response, message=''):
data = '{}|{}'.format(response.value, message)
logging.info('Send message to client {}'.format(self.client_address))
logging.info('>> "{}"'.format(data))
self.request.sendall(bytes(data, 'utf-8'))
def send_telegram_message(self, sender_name, group, level, data_type, data):
for user in GROUPS[group]:
message = ''
message += '{level} {sender}\n'.format(level=LEVELS[level], sender=sender_name)
if data_type is RequestHandler.DataType.Text:
message += '{message}'.format(message=data)
self.server.dispatcher.bot.send_message(user, message)
GROUPS = {RequestHandler.Group.Admin: [110086856]}
LEVELS = {RequestHandler.Level.Error: '❌',
RequestHandler.Level.Warning: '⚠️',
RequestHandler.Level.Info: 'ℹ️'}