This repository has been archived by the owner on Feb 3, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
148 lines (121 loc) · 4.62 KB
/
bot.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# coding=utf-8
import logging
import traceback
from base64 import b64decode, b64encode
import main
import messages
from dozor import DozoR
__author__ = 'm_messiah'
RUS = (1072, 1073, 1074, 1075, 1076, 1077, 1105, 1078, 1079, 1080, 1081, 1082,
1083, 1084, 1085, 1086, 1087, 1088, 1089, 1090, 1091, 1092, 1093, 1094,
1095, 1096, 1097, 1098, 1099, 1100, 1101, 1102, 1103)
ENG = (97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122)
class Bot(object):
def __init__(self, sender):
self.chat_id = sender['id']
self.title = sender.get('title', sender.get('username', ''))
self.dozor = DozoR(self.chat_id)
self.name = messages.BOT_NAME
def show_sessions(self, _):
sessions = ["%s (%s)" % (v.title, k) for k, v in main.SESSIONS.items()]
return messages.BOT_SESSIONS_TEMPL % u"\n".join(sessions)
def not_found(self, _):
return messages.BOT_COMMAND_NOT_FOUND
def version(self, _):
return messages.BOT_VERSION
def help(self, _):
return messages.BOT_HELP
def start(self, _):
return messages.BOT_START
def stop(self, _):
try:
self.dozor.enabled = False
if self.dozor.credentials and self.dozor.credentials in main.CREDENTIALS:
del main.CREDENTIALS[self.dozor.credentials]
if self.chat_id in main.SESSIONS:
del main.SESSIONS[self.chat_id]
except Exception:
logging.warn(traceback.format_exc())
return messages.BOT_STOP
def about(self, _):
return messages.BOT_ABOUT
def base64(self, arguments):
response = None
try:
response = b64decode(arguments.encode("utf8"))
assert len(response)
response.decode("utf8").encode("utf8")
except Exception:
response = b64encode(arguments.encode("utf8"))
finally:
return response
def _construct_from_pos(self, positions, lang):
return u"".join(map(lambda i: unichr(lang[(i - 1) % len(lang)]), positions))
def pos(self, text):
try:
positions = list(map(int, text.split()))
except ValueError:
try:
positions = list(map(int, text.split(",")))
except Exception:
return None
return u"\n".join(self._construct_from_pos(positions, lang) for lang in (RUS, ENG))
def _gps_dd(self, coords):
return tuple(map(lambda x: round(float(x[0]), 6), coords))
def _gps_dmr(self, coords):
result = []
for lat in coords:
d, m = map(float, lat[:2])
result.append(round(d + m / 60 * (-1 if d < 0 else 1), 6))
return tuple(result)
def _gps_dmsr(self, coords):
result = []
for lat in coords:
d, m, s = map(float, lat[:3])
result.append(round(d + (m * 60 + s) / 3600 * (-1 if d < 0 else 1), 6))
return tuple(result)
def _split_gps(self, text):
raw_coords = text.split(",")
coords = [0, [[], []]]
for i, lat in enumerate(raw_coords):
lat = lat.split()
count = len(lat)
if count > coords[0]:
coords[0] = count
coords[1][i] = lat
return coords
def gps(self, text):
try:
coords = self._split_gps(text)
if coords[0] == 1:
return self._gps_dd(coords[1])
if coords[0] == 2:
return self._gps_dmr(coords[1])
if coords[0] == 3:
return self._gps_dmsr(coords[1])
except Exception:
pass
def handle_command(self, text):
command, _, arguments = text.partition(" ")
if self.name in command:
command = command[:command.find(self.name)]
if "@" in command:
return None
try:
return getattr(self, command[1:], getattr(self.dozor, command[1:], self.not_found))(arguments)
except UnicodeEncodeError as e: # pragma: no cover
return self.not_found(None)
except Exception as e: # pragma: no cover
logging.error(e)
def try_gps(self, text):
if text.count(",") == 1:
return self.gps(text)
def try_hello(self, text):
if u"привет" in text.lower() and u"бот" in text.lower():
return messages.BOT_HELLO
def handle_text(self, text):
for try_text in (self.try_gps, self.dozor.handle_text, self.try_hello):
result = try_text(text)
if result:
return result