-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
62 lines (45 loc) · 1.72 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
import logging
from aiogram import Bot, Dispatcher, types
from aiogram.contrib.middlewares.logging import LoggingMiddleware
from aiogram.types import ParseMode
from aiogram.utils import executor
import requests
# Enable logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# # Telegram bot token and spoonacular api
TELEGRAM_BOT_TOKEN = 'PUT YOUR TELEGRAM BOT TOKEN HERE'
SPOONACULAR_API_KEY = 'PUT YOUR SPOONACULAR API KEY HERE'
# Initialize bot and dispatcher
bot = Bot(token=TELEGRAM_BOT_TOKEN)
dp = Dispatcher(bot)
dp.middleware.setup(LoggingMiddleware())
@dp.message_handler(commands=['start'])
async def send_welcome(message: types.Message):
await message.reply(
"Hi! I am Recipe Finder Bot. Send me ingredients separated by commas, and I will find recipes for you!"
)
def find_recipes(ingredients):
url = "https://api.spoonacular.com/recipes/findByIngredients"
params = {
'apiKey': SPOONACULAR_API_KEY,
'ingredients': ingredients,
'number': 5
}
response = requests.get(url, params=params)
return response.json()
@dp.message_handler()
async def get_recipe(message: types.Message):
ingredients = message.text
recipes = find_recipes(ingredients)
if not recipes:
await message.reply("Sorry, I couldn't find any recipes with those ingredients.")
return
for recipe in recipes:
title = recipe['title']
recipe_id = recipe['id']
recipe_url = f"https://spoonacular.com/recipes/{title.replace(' ', '-')}-{recipe_id}"
await message.reply(f"[{title}]({recipe_url})", parse_mode=ParseMode.MARKDOWN)
if __name__ == '__main__':
# Start polling
executor.start_polling(dp, skip_updates=True)