-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLux_Bus_Telegram_Bot.py
312 lines (242 loc) · 12.6 KB
/
Lux_Bus_Telegram_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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import os
from dotenv import load_dotenv
from datetime import datetime
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, MessageHandler, Filters
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
import pymongo
import dns
import getRealTime
from utils import build_menu
load_dotenv('.env')
client = pymongo.MongoClient(os.getenv("mongodb_connection"))
db = client["LuxBusBot"]
users = db["users"]
favs = db["favourites"]
stations = db["stations"]
logs = db["logs"]
main_menu_keyboard = [InlineKeyboardButton('Search by Bus Number', callback_data='searchBusNo'), InlineKeyboardButton(
'Search by Stop', callback_data='searchStop'), InlineKeyboardButton('Favourites', callback_data='checkFavs'), InlineKeyboardButton('Help', callback_data='help')]
footer = InlineKeyboardButton('Main Menu', callback_data='startover')
def log(chat_id, data, func, level):
# Logging function
timestamp = datetime.now()
logs.insert_one({"timestamp": timestamp, "chat_id": chat_id, "func": func, "data": data, "level": level})
def start(update, context):
log(update.message.chat_id, "start", "start", "debug")
start_rmarkup = InlineKeyboardMarkup(
build_menu(main_menu_keyboard, n_cols=2))
update.message.reply_text(
text='Welcome to the Lux Bus Telegram Bot. Please select from the options below.', reply_markup=start_rmarkup)
def start_over(update, context):
query = update.callback_query
query.answer()
log(query.message.chat_id, query.data, "start_over", "debug")
start_rmarkup = InlineKeyboardMarkup(
build_menu(main_menu_keyboard, n_cols=2))
query.edit_message_text(
text='Welcome back to the beginning. Please select from the options below.', reply_markup=start_rmarkup)
def searchBusNo(update, context):
query = update.callback_query
query.answer()
log(query.message.chat_id, query.data, "searchBusNo", "debug")
query.message.reply_text(
text='What\'s the number of the bus that you want to check?')
def searchStop(update, context):
query = update.callback_query
query.answer()
log(query.message.chat_id, query.data, "searchStop", "debug")
query.message.reply_text(text='What stop do you want to check?')
def selectResults(update, context):
msg = update.message.text.title()
log(update.message.chat_id, msg, "selectResults", "debug")
if len(msg) < 4:
# If message is under 4 characters, assumes it is a number.
# Shortest station is Bois. Fix might be required.
unique_destinations = stations.find(
{"line": str(msg)}).distinct("destination")
if len(unique_destinations) == 0:
log(update.message.chat_id, msg, "selectResults", "warning")
update.message.reply_text(
text="That bus doesn't exist in our database. Try with a different one.")
else:
busno_keyboard = []
for destination in unique_destinations:
busno_keyboard.append([InlineKeyboardButton(str(
msg) + ' towards ' + destination, callback_data=msg + '-' + destination + '-getStops')])
busno_keyboard.append([footer])
busno_rmarkup = InlineKeyboardMarkup(busno_keyboard)
update.message.reply_text(
text='Which one of these?', reply_markup=busno_rmarkup)
else:
unique_stations = stations.find(
{"stop": {"$regex": ".*" + msg + ".*", "$options": "i"}}).distinct("stop")
if len(unique_stations) == 0:
log(update.message.chat_id, msg, "selectResults", "warning")
update.message.reply_text(
text='No station by that name exists in our database. Try a different one.')
else:
station_name_keyboard = []
for station in unique_stations:
station_id = stations.find_one({"stop": station})["station_id"]
station_name_keyboard.append([InlineKeyboardButton(
station, callback_data='undefined-' + str(station_id) + '-getStation')])
station_name_keyboard.append([footer])
station_name_rmarkup = InlineKeyboardMarkup(station_name_keyboard)
update.message.reply_text(
text='Which one of these?', reply_markup=station_name_rmarkup)
def getStops(update, context):
# Fetches the Stops where the selected Bus passes
query = update.callback_query
query.answer()
log(query.message.chat_id, query.data, "getStops", "debug")
line = query.data.split('-')[0]
destination = query.data.split('-')[1]
unique_stops = stations.find(
{"line": str(line), "destination": destination}).distinct("stop")
stops_keyboard = []
for stop in unique_stops:
station_id = stations.find_one({"stop": stop})["station_id"]
stops_keyboard.append(InlineKeyboardButton(
stop, callback_data=line + '-' + str(station_id) + '-getStation'))
stops_rmarkup = InlineKeyboardMarkup(
build_menu(stops_keyboard, n_cols=2, footer_buttons=footer))
query.edit_message_text(
text='Which station do you want to check?', reply_markup=stops_rmarkup)
def getStation(update, context):
query = update.callback_query
query.answer()
log(query.message.chat_id, query.data, "getStation", "debug")
chat_id = query.message.chat_id
line = str(query.data.split('-')[0])
station_id = query.data.split('-')[1]
result = getRealTime.main(station_id, line)
result = result.head(5)
# Currently fetching the top 5 results. A higher number might be required
# for stations like Hamilius or Gare to avoid having
# to do a second search with only the bus the person wants
if result.empty:
log(query.message.chat_id, query.data, "getStops", "warning")
text = "No bus " + line + " in the near future or the line has been deprecated."
else:
text = "Next Bus:\n"
for n in range(0, result.shape[0]):
text += "Bus " + str(result.iat[n, 0]) + " heading to " + result.iat[n, 1] + " will depart in " + str(
result.iat[n, 3]) + " minutes at " + result.iat[n, 2] + ". \n"
text += "Last updated at " + datetime.now().strftime("%H:%M:%S")
if favs.find_one({"chat_id": chat_id, "station_id": int(station_id), "line": line}):
station_keyboard = [[InlineKeyboardButton('Check Again', callback_data=line + '-' + str(station_id) + '-getStation')], [
InlineKeyboardButton('Remove from Favourites', callback_data='rem-' + line + '-' + str(station_id) + '-changeFavs')], [footer]]
else:
station_keyboard = [[InlineKeyboardButton('Check Again', callback_data=line + '-' + str(station_id) + '-getStation')], [
InlineKeyboardButton('Add to Favourites', callback_data='add-' + line + '-' + str(station_id) + '-changeFavs')], [footer]]
station_rmarkup = InlineKeyboardMarkup(
station_keyboard)
query.edit_message_text(text=text, reply_markup=station_rmarkup)
def checkFavs(update, context):
query = update.callback_query
query.answer()
log(query.message.chat_id, query.data, "checkFavs", "debug")
chat_id = query.message.chat_id
if favs.count_documents({"chat_id": chat_id}) != 0:
favs_keyboard = []
for fav in favs.find({"chat_id": chat_id}):
station_id = fav['station_id']
station_name = stations.find_one(
{"station_id": station_id})["stop"]
line = fav["line"]
if line == 'undefined':
favs_keyboard.append(InlineKeyboardButton(
station_name, callback_data=line + '-' + str(station_id) + '-getStation'))
else:
favs_keyboard.append(InlineKeyboardButton(
line + " out of " + station_name, callback_data=line + '-' + str(station_id) + '-getStation'))
favs_rmarkup = InlineKeyboardMarkup(build_menu(
favs_keyboard, n_cols=2, footer_buttons=footer), resize_keyboard=True)
query.edit_message_text(
text="Here are your favourites:", reply_markup=favs_rmarkup)
else:
noFavs_rmarkup = InlineKeyboardMarkup(
[[footer]])
query.edit_message_text(
text="You don't have any favourites yet. Add them next time you search for a stop/bus.", reply_markup=noFavs_rmarkup)
def changeFavs(update, context):
query = update.callback_query
query.answer()
log(query.message.chat_id, query.data, "changeFavs", "debug")
chat_id = query.message.chat_id
action = str(query.data.split('-')[0])
line = str(query.data.split('-')[1])
station_id = str(query.data.split('-')[2])
if action == 'add':
favs.insert_one({"chat_id": chat_id, "line": line,
"station_id": int(station_id)})
station_keyboard = [[InlineKeyboardButton('Check Again', callback_data=line + '-' + str(station_id) + '-getStation')], [
InlineKeyboardButton('Remove from Favourites', callback_data='rem-' + line + '-' + str(station_id) + '-changeFavs')], [footer]]
station_rmarkup = InlineKeyboardMarkup(station_keyboard)
result = getRealTime.main(station_id, line)
result = result.head(5)
if result.empty:
log(query.message.chat_id, query.data, "changeFavs", "warning")
text = "No bus " + line + " in the near future or the line has been deprecated."
else:
text = "Next Bus:\n"
for n in range(0, result.shape[0]):
text += "Bus " + str(result.iat[n, 0]) + " heading to " + result.iat[n, 1] + " will depart in " + str(
result.iat[n, 3]) + " minutes at " + result.iat[n, 2] + ". \n"
text += "Last updated at " + datetime.now().strftime("%H:%M:%S")
query.edit_message_text(
text="The station/bus has been added.\n" + text, reply_markup=station_rmarkup)
else:
favs.delete_one({"chat_id": chat_id, "line": line,
"station_id": int(station_id)})
station_keyboard = [[InlineKeyboardButton('Check Again', callback_data=line + '-' + str(station_id) + '-getStation')], [
InlineKeyboardButton('Add to Favourites', callback_data='add-' + line + '-' + str(station_id) + '-changeFavs')], [footer]]
station_rmarkup = InlineKeyboardMarkup(
station_keyboard)
result = getRealTime.main(station_id, line)
result = result.head(5)
if result.empty:
log(query.message.chat_id, query.data, "changeFavs", "warning")
text = "No bus " + line + " in the near future or the line has been deprecated."
else:
text = "Next Bus:\n"
for n in range(0, result.shape[0]):
text += "Bus " + str(result.iat[n, 0]) + " heading to " + result.iat[n, 1] + " will depart in " + str(
result.iat[n, 3]) + " minutes at " + result.iat[n, 2] + ". \n"
text += "Last updated at " + datetime.now().strftime("%H:%M:%S")
query.edit_message_text(
text="The station/bus has been removed.\n" + text, reply_markup=station_rmarkup)
def help(update, context):
query = update.callback_query
query.answer()
log(query.message.chat_id, query.data, "help", "debug")
help_keyboard = [[InlineKeyboardButton(
'GitHub Link', url='https://github.com/hugoncosta/luxbus-telegram-bot/')], [footer]]
help_rmarkup = InlineKeyboardMarkup(help_keyboard)
query.edit_message_text(
text="Simple bot to check Real Time data from public busses and trams in Luxembourg. For more info, check the GitHub repo.", reply_markup=help_rmarkup)
def main():
updater = Updater(token=os.getenv('bot_api'), use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CallbackQueryHandler(
searchBusNo, pattern='^searchBusNo$'))
dispatcher.add_handler(CallbackQueryHandler(
searchStop, pattern='^searchStop$'))
dispatcher.add_handler(CallbackQueryHandler(
getStops, pattern='^(.|\n)*?-getStops$'))
dispatcher.add_handler(CallbackQueryHandler(
getStation, pattern='^(.|\n)*?-getStation$'))
dispatcher.add_handler(CallbackQueryHandler(
changeFavs, pattern='^(.|\n)*?-changeFavs$'))
dispatcher.add_handler(CallbackQueryHandler(
checkFavs, pattern='^checkFavs$'))
dispatcher.add_handler(MessageHandler(Filters.text, selectResults))
dispatcher.add_handler(CallbackQueryHandler(
start_over, pattern='^startover$'))
dispatcher.add_handler(CallbackQueryHandler(
help, pattern='^' + 'help' + '$'))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()