BELEЦKIY
BELEЦKIY

Reputation: 3

problem with sending a photo in a telegram bot

I make a bot for a telegram which, by a code word, should throw a photo from a certain group, for example bot throw off the picture with the dogs
and the bot throws a photo with the dog but there is a problem: "ERROR - TeleBot: "A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: wrong HTTP URL specified" here's my code:

import os

from random import *

bot = telebot.TeleBot()
p = os.listdir("cute_dogs")


@bot.message_handler(commands=['start'])
def get_commands(message):
    if message.text == '/start':
        bot.send_message(message.chat.id, "start")


@bot.message_handler(content_types=['text'])
def get_photo(message):
    words = "бот картинку картинка фотку фото".split()
    dogs = "собак собака собаки собаку собачками".split()
    plac = "пейзажи пейзаж пейзажом".split()
    space = "космос космосом космоса".split()

    text = message.text.lower()#для удобства

    if any(x in text for x in words): #проверка совпадения ключевого слова через any
        if any(x in text for x in dogs):
            bot.send_message(message.chat.id, "* картинка с собакой *")
            bot.send_photo(message.chat.id, f"cute_dogs/{choice(p)}")
        elif any(x in text for x in plac):
            bot.send_message(message.chat.id, "* картинка с пейзажем *")
        elif any(x in text for x in space):
            bot.send_message(message.chat.id, "* картинка с космосом *")
            #bot.send_photo(message.chat.id, choice(open('space', 'rb')));
bot.polling()

I have a cute_dogs subfolder in the folder with the bot and in it there are 5 pictures of how to fix the error and to display a random picture from this subfolder.

Upvotes: 0

Views: 1654

Answers (1)

0stone0
0stone0

Reputation: 43904

You're trying to send the photo as following:

bot.send_photo(message.chat.id, f"cute_dogs/{choice(p)}")

Since the photo is a local file, you'll need to Python's open() to get the contents of the file:

bot.send_photo(message.chat.id, open("cute_dogs/{choice(p)}", 'rb'))

How to send a local picture with Python Telegram Bot

Upvotes: 1

Related Questions