2019-10-11 22:40:28 +02:00
|
|
|
import logging
|
|
|
|
import requests
|
|
|
|
from telegram import InlineQueryResultArticle, InputTextMessageContent
|
|
|
|
from telegram.ext import Updater, CommandHandler, InlineQueryHandler
|
|
|
|
|
|
|
|
token_file = open("token", "r")
|
|
|
|
updater = Updater(token=token_file.read(), use_context=True)
|
|
|
|
token_file.close()
|
|
|
|
|
|
|
|
dispatcher = updater.dispatcher
|
|
|
|
|
|
|
|
logging.basicConfig(format='%(acstime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
|
|
|
|
|
|
|
|
|
|
|
|
def start(update):
|
|
|
|
update.message.reply_text("Hello")
|
|
|
|
|
|
|
|
|
|
|
|
def search_station(query):
|
|
|
|
request = requests.get("https://efa-api.asw.io/api/v1/station/?search=" + query)
|
|
|
|
if request.status_code != 200:
|
|
|
|
return -1
|
|
|
|
else:
|
|
|
|
return request.json()[0]['stationId']
|
|
|
|
|
|
|
|
|
|
|
|
def get_vvs_departures(update, context):
|
2019-10-14 13:22:56 +02:00
|
|
|
number_departures = 4
|
|
|
|
last_index_string = len(context.args)
|
2019-10-11 22:40:28 +02:00
|
|
|
if len(context.args) == 0:
|
|
|
|
update.message.reply_text("No argument specified")
|
|
|
|
return
|
2019-10-14 13:22:56 +02:00
|
|
|
if context.args[-1].isdigit():
|
|
|
|
number_departures = context.args[-1]
|
|
|
|
last_index_string -= 1
|
2019-10-11 22:40:28 +02:00
|
|
|
if context.args[0].isdigit():
|
|
|
|
station_id = context.args[0]
|
|
|
|
else:
|
2019-10-14 13:22:56 +02:00
|
|
|
station_id = search_station(' '.join(context.args[0:last_index_string]))
|
2019-10-11 22:40:28 +02:00
|
|
|
request = requests.get("https://efa-api.asw.io/api/v1/station/" + station_id + "/departures")
|
|
|
|
if request.status_code != 200:
|
|
|
|
update.message.reply_text("Error with server communication")
|
|
|
|
update.message.reply_text("Next departures:")
|
2019-10-14 13:22:56 +02:00
|
|
|
for station in request.json()[:int(number_departures)]:
|
2019-10-11 22:40:28 +02:00
|
|
|
update.message.reply_text(
|
|
|
|
"Line " + station['number'] + " to \"" + station['direction'] + "\" at "
|
|
|
|
+ station['departureTime']['hour'].zfill(2) + ':' + station['departureTime']['minute'].zfill(2)
|
|
|
|
+ " +" + str(station['delay']))
|
|
|
|
|
|
|
|
|
|
|
|
def inline_station_search(update, context):
|
|
|
|
query = update.inline_query.query
|
|
|
|
if len(query) < 5 or not query:
|
|
|
|
return
|
|
|
|
results = list()
|
2019-10-15 19:17:57 +02:00
|
|
|
for station in get_station_id_list(query):
|
2019-10-11 22:40:28 +02:00
|
|
|
results.append(
|
|
|
|
InlineQueryResultArticle(
|
|
|
|
id=station['stationId'],
|
|
|
|
title=station['name'],
|
|
|
|
input_message_content=InputTextMessageContent("/vvs " + station['stationId'])
|
|
|
|
)
|
|
|
|
)
|
|
|
|
context.bot.answer_inline_query(update.inline_query.id, results)
|
|
|
|
|
|
|
|
|
2019-10-15 19:17:57 +02:00
|
|
|
def get_station_id_list(name):
|
2019-10-11 22:40:28 +02:00
|
|
|
request = requests.get("https://efa-api.asw.io/api/v1/station/?search=" + name)
|
|
|
|
return request.json()
|
|
|
|
|
|
|
|
|
|
|
|
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.start_polling()
|
|
|
|
updater.idle()
|