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
+3
View File
@@ -0,0 +1,3 @@
TELEGRAM_TOKEN=
ACCESS_KEY=
SECRET_KEY=
Generated
+1 -1
View File
@@ -552,4 +552,4 @@ watchdog = ["watchdog (>=2.3)"]
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.11" python-versions = "^3.11"
content-hash = "d51a7ed0e1456398f57862f5a44db12017266f0723bc5123679fc751dbf617a9" content-hash = "5722f06b58085e44f0359d02884aa1de8f1bac83c9d9dd55b7eef024327a48cc"
+1
View File
@@ -10,6 +10,7 @@ python = "^3.11"
flask = "^2.3.3" flask = "^2.3.3"
python-dotenv = "^1.0.0" python-dotenv = "^1.0.0"
pytelegrambotapi = "^4.14.0" pytelegrambotapi = "^4.14.0"
requests = "^2.31.0"
[tool.poetry.group.dev.dependencies] [tool.poetry.group.dev.dependencies]
pre-commit = "^3.4.0" pre-commit = "^3.4.0"
+14 -4
View File
@@ -1,5 +1,6 @@
import requests
import telebot import telebot
from config import TELEGRAM_TOKEN from config import ACCESS_TOKEN, BASE_URL, TELEGRAM_TOKEN
bot = telebot.TeleBot(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.reply_to(message, "Hello, I'm a bot!")
@bot.message_handler(func=lambda msg: True) @bot.message_handler(commands=["random"])
def echo_all(message): def get_random_photo(message):
bot.reply_to(message, message.text) 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() bot.infinity_polling()
+4
View File
@@ -5,3 +5,7 @@ from dotenv import load_dotenv
load_dotenv() load_dotenv()
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN") TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
ACCESS_TOKEN = os.getenv("ACCESS_KEY")
SECRET_KEY = os.getenv("SECRET_KEY")
BASE_URL = "https://api.unsplash.com"
View File