-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
69 lines (53 loc) · 2.07 KB
/
main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import logging
import os
from dotenv import load_dotenv
import requests
load_dotenv()
from telegram import Update
from telegram.ext import ContextTypes, CommandHandler, ApplicationBuilder
TOKEN = os.getenv("TOKEN")
# /start command
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
logging.warning(f"Got message from {update.effective_chat.username}")
await context.bot.send_message(
chat_id=update.effective_chat.id, text="Kirjoita botille /makkara"
)
# /makkara command
async def makkara(update: Update, context: ContextTypes.DEFAULT_TYPE):
message_id = (
await context.bot.send_message(
chat_id=update.effective_chat.id, text="Loading..."
)
).id
api_url = "https://makkara.fly.dev/api"
response = requests.get(api_url)
data = response.json()
data_list = []
string = ""
# check if there are multiple dates of sausages available
multiple_days = False
for i in data:
if len(data[i]) > 1:
multiple_days = True
break
# format result based on multiple_days
if not multiple_days:
for i in data:
data_list.append(str(f"{i} - {data[i][0]}"))
string = "\n".join(data_list)
message = f"Meksikolaista uunimakkaraa on saatavilla näistä ravintoloista: \n{string} \n\n\nTieto on suuntaa antavaa, varmista Unicafen sivuilta meksikolaisen uunimakkaran saatavuus."
else:
for i in data:
data_list.append(str(f"{i} {data[i]}"))
message = f"Meksikolaista uunimakkaraa on saatavilla näistä ravintoloista: {data_list}"
logging.warning(f"Got message from {update.effective_chat.username}")
await context.bot.edit_message_text(
chat_id=update.effective_chat.id, message_id=message_id, text=message
)
print("RUNNING")
application = ApplicationBuilder().token(TOKEN).build()
start_handler = CommandHandler("start", start)
makkara_handler = CommandHandler("makkara", makkara)
application.add_handler(start_handler)
application.add_handler(makkara_handler)
application.run_polling()