-
Notifications
You must be signed in to change notification settings - Fork 1
/
davwheat.py
43 lines (28 loc) · 1.31 KB
/
davwheat.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
from telegram import ForceReply, Update
from telegram.constants import ParseMode
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
import requests
from requests.auth import HTTPBasicAuth
import daisySecrets
async def crs_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Looks up a CRS code for a requested train station using David Wheatley's API"""
message_args = update.message.text.split(' ')
search_term = ""
for i in message_args[1:]:
if len(search_term) >= 1:
search_term += " "
search_term += i
url = f"https://national-rail-api.davwheat.dev/crs/{search_term}"
response = requests.get(url)
if response.status_code != 200:
await update.message.reply_text(f"The request failed: \n{response.raw}", parse_mode=ParseMode.MARKDOWN)
return
data = response.json()
if len(data) == 0:
await update.message.reply_text(f"No stations found.", parse_mode=ParseMode.MARKDOWN)
return
# If we made it this far chances are there are some stations to be found :)
replyText = ""
for station in data:
replyText += "\n" + f"`{station['crsCode']}: {station['stationName']}`"
await update.message.reply_text(replyText, parse_mode=ParseMode.MARKDOWN)