-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
223 lines (187 loc) · 7.42 KB
/
handlers.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/python
import mysql
import re
import datetime
# some func
def makeUserDic(update):
user = {'first_name': update.message.from_user.first_name, 'last_name': update.message.from_user.last_name,
'username': update.message.from_user.username, 'user_id': update.message.from_user.id}
return user
def emojiAnswer(answer_id):
emoji = {
'1': u'\u2705', # willattend
'2': u'\u274C', # wontattend
'3': u'\U0001F414' # tentative
}
answer = str(int(answer_id))
return emoji[answer]
def buildListText(list_obj, status):
title = u''
if status == 'open':
title = u"On the list for '{}': \n \n".format(list_obj['listName'])
elif status == 'close':
title = u"List '{}' closed! Final results: \n \n".format(list_obj['listName'])
print list_obj
wi_i = 0
wo_i = 0
t_i = 0
will_attend = u''
wont_attend = u''
tent = u''
time_last = None
time_text = u''
for tup in list_obj['users']:
emoji = emojiAnswer(tup[0])
# time -----------------------------------------
if tup[4] != 'NULL' and tup[4] is not None:
print "line 40: tuple[4]:", tup[4]
time = (datetime.datetime.min + tup[4]).time()
time_str = u" " + u"\u231A" + u"{:%H:%M}".format(time)
print "line 44: time_str:", time_str
if time_last is None:
time_last = time
else:
if time > time_last:
time_last = time
else:
time_str = ""
# time -----------------------------------------
if int(tup[0]) == 1:
wi_i += 1
will_attend = will_attend + unicode(str(wi_i)) + u'.' + emoji + u' ' + tup[1] + u' ' + tup[
2] + time_str + u'\n'
elif int(tup[0]) == 2:
wo_i += 1
wont_attend = wont_attend + unicode(str(wo_i)) + u'.' + emoji + u' ' + tup[1] + u' ' + tup[
2] + time_str + u'\n'
elif int(tup[0]) == 3:
t_i += 1
tent = tent + unicode(str(t_i)) + u'.' + emoji + u' ' + tup[1] + u' ' + tup[
2] + time_str + u'\n'
summary_list = []
if will_attend.replace(" ", "") != "":
will_attend += u'\n'
summary_list.append(u'{0} will attend'.format(unicode(str(wi_i))))
if wont_attend.replace(" ", "") != "":
wont_attend += u'\n'
summary_list.append(u'{0} wont attend'.format(unicode(str(wo_i))))
if tent.replace(" ", "") != "":
tent += u'\n'
summary_list.append(u'{0} is tentative'.format(unicode(str(t_i))))
summary_str = u''
if len(summary_list) > 0:
summary_str = u'{0}. \n \n'.format(u', '.join(summary_list))
if time_last is not None:
time_text = u'The last attendee will arrive at {:%H:%M}.'.format(time_last)
text = title + summary_str + will_attend + tent + wont_attend + time_text
return text
def stringToTime(string):
# noinspection PyBroadException
try:
time_obj = re.split(":", string)
time = datetime.time(int(time_obj[0]), int(time_obj[1]), 00)
except:
time = None
return time
# handlers
def start(bot, update):
bot.sendMessage(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!")
# TODO change that shit to make something usefull, like help
def krystian(bot, update):
bot.sendMessage(chat_id=update.message.chat_id,
text="Krystian is a pussy who uses cheats (like TriggerBot, WH and, "
"of course, AimBot) to pretend, that he's somewhat better in"
" CS:GO than rest of his team")
def make(bot, update, args):
name = u' '.join(args)
# debug
print name.encode('utf-8')
# /debug
chat_id = update.message.chat_id
# check if list already exists in chat
exists = mysql.checkListExistence(chat_id)
if exists:
text = "There is already created list. You have to close it first with /close"
print "list exists"
else:
if name.replace(" ", "") != "":
result = mysql.create_list(chat_id, name)
if result:
text = u"List {} has been created successfully!".format(unicode(str(name)))
print "list created"
else:
text = "Ups! Something went wrong :( Your list has not been created." "" \
"Please contact MSokol00 on telegram"
else:
text = "Why don't You give a name for Your list? Try '/make noobs gathering'"
bot.sendMessage(chat_id=chat_id, text=text)
def close(bot, update):
chat_id = update.message.chat_id
exists = mysql.checkListExistence(chat_id)
if exists:
list_obj = mysql.getListRSVP(chat_id)
text = buildListText(list_obj, 'close')
mysql.close_list(chat_id)
print "list closed"
else:
text = "There is no list to close! You have to first create one with /make"
bot.sendMessage(chat_id=chat_id, text=text)
def willattend(bot, update, args):
# time section -----------------------------------------
text_time = u''
time = None
if len(args) != 0:
print args[0]
if re.match("[0-9]{2}:[0-9]{2}", args[0]):
time = stringToTime(args[0])
if time is None:
text_time = u"Nice try with time :)"
print text_time
else:
text_time = u"Given time is wrong however. Inform about time of Your arrival with: /willattend 16:45."
print text_time
# /time section ----------------------------------------
chat_id = update.message.chat_id
exists = mysql.checkListExistence(chat_id)
if exists:
user = makeUserDic(update)
mysql.attend(chat_id, user, answer='will', time=time)
if time is not None:
text = u"{} {} will attend at {:%H:%M}!".format(user['first_name'], user['last_name'], time)
else:
text = u"{} {} will attend!".format(user['first_name'], user['last_name']) + u" " + text_time
text = text.replace('None ', '')
else:
text = u"There is no list to attend! Create one with /make"
bot.sendMessage(chat_id=chat_id, text=text)
def wontattend(bot, update):
chat_id = update.message.chat_id
exists = mysql.checkListExistence(chat_id)
if exists:
user = makeUserDic(update)
mysql.attend(chat_id, user, answer='wont')
text = u"{} {} won't attend!".format(user['first_name'], user['last_name'])
text = text.replace('None ', '')
else:
text = "There is no list to attend! Create one with /make"
bot.sendMessage(chat_id=chat_id, text=text)
def tentative(bot, update):
chat_id = update.message.chat_id
exists = mysql.checkListExistence(chat_id)
if exists:
user = makeUserDic(update)
mysql.attend(chat_id, user, answer='tent')
text = u"{} {} is tentative...".format(user['first_name'], user['last_name'])
text = text.replace('None ', '')
else:
text = "There is no list to attend! Create one with /make"
bot.sendMessage(chat_id=chat_id, text=text)
def showlist(bot, update):
chat_id = update.message.chat_id
exists = mysql.checkListExistence(chat_id)
if exists:
list_obj = mysql.getListRSVP(chat_id)
text = buildListText(list_obj, 'open')
else:
text = "There is no list. You should create one with /make"
bot.sendMessage(chat_id=chat_id, text=text)