-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
68 lines (49 loc) · 1.63 KB
/
bot.py
File metadata and controls
68 lines (49 loc) · 1.63 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
import logging
import os
import requests
from dotenv import load_dotenv
from telebot import TeleBot, types
load_dotenv()
secret_token = os.getenv('TOKEN')
bot = TeleBot(token=secret_token)
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
@bot.message_handler(commands=['newcat'])
def new_cat(message):
chat_id = message.chat.id
bot.send_photo(chat_id, get_new_image())
@bot.message_handler(commands=['start'])
def wake_up(message):
chat_id = message.chat.id
name = message.from_user.first_name
keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
button = types.KeyboardButton('/newcat')
keyboard.add(button)
bot.send_message(
chat_id=chat_id,
text=f'Привет, {name}. Посмотри, какого котика я тебе нашел',
reply_markup=keyboard,
)
bot.send_photo(chat_id, get_new_image())
@bot.message_handler(content_types=['text'])
def say_hi(message):
chat = message.chat
chat_id = chat.id
bot.send_message(chat_id=chat_id, text='Привет, я KittyBot!')
def main():
bot.polling(none_stop=True)
if __name__ == '__main__':
main()