implemented random pic functionality

This commit is contained in:
2023-09-27 03:20:04 +03:00
parent 534dd0206f
commit d2e068d527
6 changed files with 23 additions and 5 deletions
+14 -4
View File
@@ -1,5 +1,6 @@
import requests
import telebot
from config import TELEGRAM_TOKEN
from config import ACCESS_TOKEN, BASE_URL, TELEGRAM_TOKEN
bot = telebot.TeleBot(TELEGRAM_TOKEN)
@@ -9,9 +10,18 @@ def send_welcome(message):
bot.reply_to(message, "Hello, I'm a bot!")
@bot.message_handler(func=lambda msg: True)
def echo_all(message):
bot.reply_to(message, message.text)
@bot.message_handler(commands=["random"])
def get_random_photo(message):
url = "https://api.unsplash.com/photos/random"
response = requests.get(
url, timeout=10, headers={"Authorization": f"Client-ID {ACCESS_TOKEN}"}
)
data = response.json()
image_url = data["urls"]["regular"]
description = data["alt_description"]
image_content = requests.get(image_url)
bot.send_photo(message.chat.id, image_content.content, caption=description)
bot.infinity_polling()