From 86438abdec8a6b074334295337db1e06881ce29e Mon Sep 17 00:00:00 2001 From: JuliusFreudenberger Date: Thu, 17 Oct 2019 16:42:14 +0200 Subject: [PATCH] Externalized processing of requests to another method Now it is easier to get the finished messages and send it if no reply is possible --- bot.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/bot.py b/bot.py index f08462d..ce14af9 100644 --- a/bot.py +++ b/bot.py @@ -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()