我为电报制作了一个机器人,例如,通过代码字,它应该从某个组中抛出一张照片
机器人扔掉与狗
的合影
然后机器人扔了一张与狗的照片,但有一个问题:
“错误 - 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张图片说明如何修复错误并显示该子文件夹中的随机图片。
答案 0 :(得分:1)
您正在尝试按以下方式发送照片:
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'))