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