Further externalization and exceptions
some more methods for easier accessing the steps done to give the answer now also includes exeptions with handlers
This commit is contained in:
parent
86438abdec
commit
0d92c0abe6
2 changed files with 43 additions and 13 deletions
46
bot.py
46
bot.py
|
@ -4,6 +4,8 @@ import re
|
||||||
from telegram import InlineQueryResultArticle, InputTextMessageContent
|
from telegram import InlineQueryResultArticle, InputTextMessageContent
|
||||||
from telegram.ext import Updater, CommandHandler, InlineQueryHandler
|
from telegram.ext import Updater, CommandHandler, InlineQueryHandler
|
||||||
|
|
||||||
|
from exceptions import *
|
||||||
|
|
||||||
token_file = open("token", "r")
|
token_file = open("token", "r")
|
||||||
updater = Updater(token=token_file.read(), use_context=True)
|
updater = Updater(token=token_file.read(), use_context=True)
|
||||||
token_file.close()
|
token_file.close()
|
||||||
|
@ -25,9 +27,9 @@ class Query:
|
||||||
arguments = re.split(r' to | in | times ', request_tmp)
|
arguments = re.split(r' to | in | times ', request_tmp)
|
||||||
self.station_id = search_station(arguments[0])
|
self.station_id = search_station(arguments[0])
|
||||||
if ' to ' in argument_names:
|
if ' to ' in argument_names:
|
||||||
self.destination = arguments[argument_names.index(' to ')+1]
|
self.destination = arguments[argument_names.index(' to ') + 1]
|
||||||
if ' in ' in argument_names:
|
if ' in ' in argument_names:
|
||||||
self.line = arguments[argument_names.index(' in ')+1]
|
self.line = arguments[argument_names.index(' in ') + 1]
|
||||||
if ' times ' in argument_names:
|
if ' times ' in argument_names:
|
||||||
self.departure_count = int(arguments[argument_names.index(' times ') + 1])
|
self.departure_count = int(arguments[argument_names.index(' times ') + 1])
|
||||||
|
|
||||||
|
@ -41,23 +43,27 @@ def search_station(query):
|
||||||
|
|
||||||
|
|
||||||
def handle_vvs(update, context):
|
def handle_vvs(update, context):
|
||||||
for reply in get_vvs_departures(context.args):
|
query = parse_station(context.args)
|
||||||
|
departures = get_vvs_departures(query)
|
||||||
|
|
||||||
|
for reply in departures:
|
||||||
update.message.reply_text(reply)
|
update.message.reply_text(reply)
|
||||||
|
|
||||||
|
|
||||||
def get_vvs_departures(args):
|
def parse_station(args):
|
||||||
reply = []
|
|
||||||
if len(args) == 0:
|
if len(args) == 0:
|
||||||
reply.append("No argument specified")
|
raise NoArgError
|
||||||
return reply
|
|
||||||
query = Query(args)
|
query = Query(args)
|
||||||
if query.station_id == -1:
|
if query.station_id == -1:
|
||||||
reply.append("No station matching this name found!")
|
raise StationNotFoundError
|
||||||
return reply
|
return query
|
||||||
|
|
||||||
|
|
||||||
|
def get_vvs_departures(query):
|
||||||
|
reply = []
|
||||||
request = requests.get("https://efa-api.asw.io/api/v1/station/" + query.station_id + "/departures")
|
request = requests.get("https://efa-api.asw.io/api/v1/station/" + query.station_id + "/departures")
|
||||||
if request.status_code != 200:
|
if request.status_code != 200:
|
||||||
reply.append("Error with server communication")
|
raise ServerCommunicationError
|
||||||
return reply
|
|
||||||
reply.append("Next departures:")
|
reply.append("Next departures:")
|
||||||
printed_departures = 0
|
printed_departures = 0
|
||||||
for station in request.json():
|
for station in request.json():
|
||||||
|
@ -95,11 +101,25 @@ def get_station_id_list(name):
|
||||||
return request.json()
|
return request.json()
|
||||||
|
|
||||||
|
|
||||||
|
def error_callback(update, context):
|
||||||
|
try:
|
||||||
|
raise context.error
|
||||||
|
except NoArgError:
|
||||||
|
update.message.reply_text('No argument specified!')
|
||||||
|
return
|
||||||
|
except StationNotFoundError:
|
||||||
|
update.message.reply_text('No station matching this name found!')
|
||||||
|
return
|
||||||
|
except ServerCommunicationError:
|
||||||
|
update.message.reply_text('Error with server communication')
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
inline_station_search_handler = InlineQueryHandler(inline_station_search)
|
inline_station_search_handler = InlineQueryHandler(inline_station_search)
|
||||||
|
|
||||||
dispatcher.add_handler(inline_station_search_handler)
|
dispatcher.add_handler(inline_station_search_handler)
|
||||||
|
dispatcher.add_handler(CommandHandler('vvs', handle_vvs))
|
||||||
updater.dispatcher.add_handler(CommandHandler('vvs', handle_vvs))
|
dispatcher.add_error_handler(error_callback)
|
||||||
|
|
||||||
updater.start_polling()
|
updater.start_polling()
|
||||||
updater.idle()
|
updater.idle()
|
||||||
|
|
10
exceptions.py
Normal file
10
exceptions.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
class NoArgError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class StationNotFoundError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ServerCommunicationError(Exception):
|
||||||
|
pass
|
Loading…
Reference in a new issue