-
Notifications
You must be signed in to change notification settings - Fork 0
/
kittybot.py
61 lines (42 loc) · 1.54 KB
/
kittybot.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
import os
import requests
from dotenv import load_dotenv
from telegram import ReplyKeyboardMarkup
from telegram.ext import CommandHandler, Updater
load_dotenv()
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
URL = 'https://api.thecatapi.com/v1/images/search'
def get_new_image():
try:
response = requests.get(URL)
except Exception as error:
logging.error(f'Ошибка при запросе к основному API: {error}')
new_url = 'https://api.thedogapi.com/v1/images/search'
response = requests.get(new_url)
response = response.json()
random_cat = response[0].get('url')
return random_cat
def new_cat(update, context):
chat = update.effective_chat
context.bot.send_photo(chat.id, get_new_image())
def wake_up(update, context):
chat = update.effective_chat
name = update.message.chat.first_name
button = ReplyKeyboardMarkup([['/newcat']], resize_keyboard=True)
context.bot.send_message(
chat_id=chat.id,
text=f'Привет, {name}. Посмотри, какого котика я тебе нашёл',
reply_markup=button
)
context.bot.send_photo(chat.id, get_new_image())
def main():
updater = Updater(token=os.getenv('TOKEN'))
updater.dispatcher.add_handler(CommandHandler('start', wake_up))
updater.dispatcher.add_handler(CommandHandler('newcat', new_cat))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()