在电报机器人中发送照片的问题

problem with sending a photo in a telegram bot

我为电报制作了一个机器人,例如,它应该通过代码字抛出某个组的照片 bot 甩掉与 dogs
的图片 机器人扔了一张和狗的照片,但有一个问题: “错误 - TeleBot:”对 Telegram API 的请求不成功。错误代码:400。说明:错误请求:指定的 HTTP URL 错误” 这是我的代码:

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()

我在包含机器人的文件夹中有一个 cute_dogs 子文件夹,其中有 5 张图片说明如何修复错误并显示该子文件夹中的随机图片。

您正在尝试按以下方式发送照片:

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

由于照片是本地文件,您需要Python的open()来获取文件的内容:

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

How to send a local picture with Python Telegram Bot