簡體   English   中英

在電報機器人中發送照片的問題

[英]problem with sending a photo in a telegram bot

我為電報制作了一個機器人,通過一個代碼字,它應該拋出某個組的照片,例如機器人扔掉與的照片
機器人向狗扔了一張照片,但出現了一個問題:“錯誤 - 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'))

如何使用 Python Telegram Bot 發送本地圖片

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM