mirror of
https://github.com/andwati/unsplash-bot.git
synced 2026-11-25 12:08:47 +00:00
implemented random pic functionality
This commit is contained in:
+14
-4
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user