Externalized processing of requests to another method

Now it is easier to get the finished messages and send it if no reply is
possible
This commit is contained in:
JuliusFreudenberger 2019-10-17 16:42:14 +02:00
parent 0e08ae8b2b
commit 86438abdec

30
bot.py
View file

@ -40,30 +40,38 @@ def search_station(query):
return request.json()[0]['stationId']
def get_vvs_departures(update, context):
if len(context.args) == 0:
update.message.reply_text("No argument specified")
return
query = Query(context.args)
def handle_vvs(update, context):
for reply in get_vvs_departures(context.args):
update.message.reply_text(reply)
def get_vvs_departures(args):
reply = []
if len(args) == 0:
reply.append("No argument specified")
return reply
query = Query(args)
if query.station_id == -1:
update.message.reply_text("No station matching this name found!")
return
reply.append("No station matching this name found!")
return reply
request = requests.get("https://efa-api.asw.io/api/v1/station/" + query.station_id + "/departures")
if request.status_code != 200:
update.message.reply_text("Error with server communication")
update.message.reply_text("Next departures:")
reply.append("Error with server communication")
return reply
reply.append("Next departures:")
printed_departures = 0
for station in request.json():
if station['direction'].casefold().find(query.destination) != -1 or station['direction'].find(
query.destination) != -1:
if query.line == '' or (query.line != -1 and station['number'] == query.line):
update.message.reply_text(
reply.append(
"Line " + station['number'] + " to \"" + station['direction'] + "\" at "
+ station['departureTime']['hour'].zfill(2) + ':' + station['departureTime']['minute'].zfill(2)
+ " +" + str(station['delay']))
printed_departures += 1
if printed_departures >= query.departure_count:
break
return reply
def inline_station_search(update, context):
@ -91,7 +99,7 @@ inline_station_search_handler = InlineQueryHandler(inline_station_search)
dispatcher.add_handler(inline_station_search_handler)
updater.dispatcher.add_handler(CommandHandler('vvs', get_vvs_departures))
updater.dispatcher.add_handler(CommandHandler('vvs', handle_vvs))
updater.start_polling()
updater.idle()