-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponses.py
196 lines (173 loc) · 7.63 KB
/
responses.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
import datetime
import re
import requests
from turkish import TurkishText
class Responses:
"""Class for handling responses to messages.
Attributes:
__methods -- the list of methods, contains function pointers
__canned -- the dictionary of canned responses
"""
__methods = []
__canned = dict()
__commands = dict()
def __init__(self):
"""Adds the canned responses and command responses to the
respective dictionaries.
"""
# Canned responses
self.__canned['hello'] = "Hello from the other side"
self.__canned['günaydın'] = "Günaydın hocam!"
# Command responses
self.__commands[
'/start'] = "Merhaba! Ben ODTÜ Bot. \n\nGüncel yemekhane menüsünü öğrenmek için `/yemekhane` " \
"yazabilirsin. `/yemekhane yarın` komutu ile yarının menüsünü de öğrenebilirsin. Belirli bir " \
"tarihin yemekhane menüsünü görmek için `/yemekhane GG-AA-YYYY` formatını " \
"kullanabilirsin.\n\n`/menu` komutu ile yemekhane servisine abone olabilirsin. Bu servis ile " \
"haftaiçi her gün, sabah 9'da güncel yemek menüsünü özel mesaj olarak gönderiyorum." \
"\n\nGözüne çarpan hataları ya da botta olmasını istediğin özellikleri @frozsgy'e " \
"iletebilirsin.\n\nUmarım beni seversin :) "
self.__commands[
'/help'] = "Help will arrive for the ones who really need."
def respond(self, message):
"""Processes the message and adds the response (if exists) to the response list.
Returns list of responses.
"""
message = TurkishText(message).lower()
if message.count('@') > 0:
message_tuple = message.split('@')
if 'odtubot' in message_tuple:
message = message_tuple[0]
else:
return []
# Canned responses
res = self.canned(message)
# Command responses
res += self.commands(message)
# Cafeteria function
if re.search('/yemekhane', message):
if re.search('yarın', message):
res.append(self.food('tomorrow'))
elif len(message.split()) > 1:
res.append(self.food(message.split()[1]))
else:
res.append(self.food())
# Daily cafeteria menu function
if re.search('/menu', message):
res.append(('service', 1))
return res
def canned(self, message):
"""Checks the message for possible canned responses.
Returns list of responses.
"""
res = []
message = TurkishText(message.strip()).lower()
for key, val in self.__canned.items():
if re.search(key, message):
res.append(val)
return res
def commands(self, message):
"""Checks the message for possible responses to commands.
Requires an exact match of the phrase.
Returns list of responses.
"""
res = []
message = TurkishText(message.strip()).lower()
for key, val in self.__commands.items():
if re.search(r'^' + key + '$', message):
res.append(val)
return res
def food(self, date='today'):
"""Fetches the menu offered at METU Cafeteria for today, tomorrow, or any given date.
Returns string.
"""
now = datetime.datetime.now()
date_search = re.search(
r"(0{0,1}[1-9]|[12][0-9]|3[01])[- /.](0{0,1}[1-9]|1[012])[- /.](19|20)\d\d",
date)
if date_search:
date_match = re.search(r"(\d{1,2})(.*?)(\d{1,2})(.*?)(\d{4})",
date)
dates = date_match.groups()
iday = '/'.join(dates[::2])
else:
if date == 'tomorrow':
now += datetime.timedelta(days=1)
else:
date = 'today'
iday = now.strftime("%d/%m/%Y")
url = "https://kafeterya.metu.edu.tr/views/ajax?date_filter[value][date]=" + iday + "&view_name=yemek_listesi&view_display_id=block"
veggie_regex = r"<div class=\"vejeteryan\">Vejetaryen: (.*?)<\/div>"
regex = r"<span property=\"dc:title\" content=\"(.*?)\""
response = requests.request("POST", url)
ajax_response = response.json()
daily = [[], []]
if len(ajax_response) > 1:
data = ajax_response[1]
daily_menu = data['data']
matches = re.finditer(regex, daily_menu, re.MULTILINE)
match_count = 0
for matchNum, match in enumerate(matches, start=1):
for groupNum in range(0, len(match.groups())):
groupNum += 1
if match_count < 4:
daily[0].append(
TurkishText(match.group(groupNum)).capitalize())
else:
daily[1].append(
TurkishText(match.group(groupNum)).capitalize())
match_count += 1
match_count = 0
matches = re.finditer(veggie_regex, daily_menu, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
for groupNum in range(0, len(match.groups())):
groupNum += 1
if match_count < 1:
daily[0].append(
TurkishText(match.group(groupNum)).capitalize())
else:
daily[1].append(
TurkishText(match.group(groupNum)).capitalize())
match_count += 1
if daily != [[], []]:
if date == 'today':
menu_response = ["🍴 Bugün yemekhanede şunlar varmış hocam:"]
elif date == 'tomorrow':
menu_response = ["🍴 Yarın yemekhanede şunlar varmış hocam:"]
else:
menu_response = [
"🍴 %s tarihinde yemekhanede şunlar varmış hocam:" % date
]
if len(daily[0]) > 3:
menu_response.append("")
menu_response.append("*Öğle Yemeği*")
for j in range(4):
if daily[0][j] != '*':
menu_response.append("· " + daily[0][j])
menu_response.append("")
if len(daily[1]) > 3:
if daily[1][0] != "*":
menu_response.append("*Akşam Yemeği*")
for j in range(4):
menu_response.append("· " + daily[1][j])
menu_response.append("")
if len(daily[0]) > 4 or len(daily[1]) > 4:
menu_response.append("🥬 Vejetaryen alternatifler:")
menu_response.append("")
if len(daily[0]) > 4 and daily[0][4] != '':
menu_response.append("*Öğle Yemeği*")
menu_response.append("· " + daily[0][4])
menu_response.append("")
if len(daily[1]) > 4 and daily[1][0] != "*":
menu_response.append("*Akşam Yemeği*")
menu_response.append("· " + daily[1][4])
menu_response.append("")
menu_response.append("Afiyet olsun!")
return '\n'.join(menu_response)
else:
if date == 'today':
return "Bugün yemek yok hocam 😔"
elif date == 'tomorrow':
return "Yarın yemek yok hocam 😔"
else:
return "%s tarihinde yemek yok hocam 😔" % date