-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
83 lines (61 loc) · 2.62 KB
/
server.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import json
import logging
from util import get_json_file
from ingredients import get_cocktail_ingredients
from aiogram import Bot, Dispatcher, executor, types
API_TOKEN = os.getenv("API_TELEGRAM_BOT")
RANDOM_COCKTAIL_URI = "https://www.thecocktaildb.com/api/json/v1/1/random.php"
logging.basicConfig(level=logging.INFO)
telegram_bot = Bot(token=API_TOKEN)
dispatcher = Dispatcher(telegram_bot)
@dispatcher.message_handler(commands=["start", "help"])
async def send_welcome(message: types.Message):
"""
This handler will be called when user sends `/start` or `/help` command
"""
await message.answer(
"Hi!\nI'm CocktailBot!\nI help you cock a cocktail.\n"
"Send me the name of the cocktail and I'll find it 🍸\n"
"/help - Show bot commands\n"
"/random - Casual cocktail recipe"
)
@dispatcher.message_handler(commands=["random"])
async def random_cocktail(message: types.Message):
get_json_file(RANDOM_COCKTAIL_URI)
with open("json_obj.json") as file:
templates = json.load(file)
await message.answer(templates["drinks"][0]["strDrink"] + " 🍺")
await message.answer("we're gonna need:")
ingredients: list = get_cocktail_ingredients()
for ingredient in ingredients:
await message.answer(ingredient)
await message.answer(templates["drinks"][0]["strInstructions"])
await telegram_bot.send_photo(
message.chat.id,
types.InputFile.from_url(templates["drinks"][0]["strDrinkThumb"]),
)
@dispatcher.message_handler() # title search cocktail
async def search_cocktail(message: types.Message):
try:
cocktail_name = message.text
URL = (
f"https://www.thecocktaildb.com/api/json/v1/1/search.php?s={cocktail_name}"
)
get_json_file(URL) # get json file
with open("json_obj.json") as file:
templates = json.load(file)
await message.answer(templates["drinks"][0]["strDrink"] + " 🍺")
await message.answer("we're gonna need:")
ingredients_ = get_cocktail_ingredients()
for ingredient in ingredients_:
await message.answer(ingredient)
await message.answer(templates["drinks"][0]["strInstructions"])
await telegram_bot.send_photo(
message.chat.id,
types.InputFile.from_url(templates["drinks"][0]["strDrinkThumb"]),
)
except TypeError:
await message.answer("Sorry, I doesn't find your request... 😭")
if __name__ == "__main__":
executor.start_polling(dispatcher, skip_updates=True)