【问题标题】:Error with pictures and 'if' in Python TelegramBot. How to fix?Python Telegram Bot 中的图片和“if”错误。怎么修?
【发布时间】:2019-05-25 16:34:55
【问题描述】:

我正在使用pyTelegramBotAPIPython 3.7

Bot 通常可以工作,但是当 BOT 应该发送照片时发生错误。当我写一条机器人消息时,他必须发送照片或贴纸作为回答,它会发送,但是当你接下来输入相同的命令时,机器人什么也不发送。我是新手,所以请建议我如何更好地编写此代码。

elif instead of if isn't working.

...

@bot.message_handler(content_types=["text"])

def bot_messages(message):
    if message.text.lower() == "Привет".lower():
        bot.send_message(message.from_user.id, "Ку! ✋. Напиши /welcome.")

    if message.text.lower() == "/welcome".lower():
        bot.send_message(message.from_user.id, "Я бот Римуру. Напиши название животного, например, 'кот', и я отправлю его картинку.")
        bot.send_sticker(message.from_user.id, sticker1)

    if message.text.lower() == "питон".lower():
        bot.send_photo(message.from_user.id, photo1)
    if message.text.lower() == "мышь".lower():
        bot.send_photo(message.from_user.id, photo2)
    if message.text.lower() == "кот".lower():
        bot.send_photo(message.from_user.id, photo3)
    if message.text.lower() == "жираф".lower():

...

【问题讨论】:

  • 当机器人发送照片时,您能否向我们提供错误信息?
  • 您似乎正在上传不存在的照片,因为错误提示“文件必须为非空”,请检查照片对象是否为空,尝试使用 matplot 或 opencv 将其可视化
  • 非常感谢。我想我明白问题所在了。

标签: python if-statement photo telegram-bot python-telegram-bot


【解决方案1】:

如果此代码不起作用,请编写此代码,请将代码附在https://pastebin.com 并告诉我们它的位置。

@bot.message_handler(func=lambda message: True)
def main(message):
    chat_id = message.chat.id
    user_id = message.from_user.id
    text = str(message.text).decode("utf-8").lower()
    if text == "привет":
        bot.send_message(user_id, "Ку! ✋. Напиши /welcome.")
    if text == "/welcome":
        bot.send_message(user_id, "Я бот Римуру. Напиши название животного, например, 'кот', и я отправлю его картинку.")
    if text == "питон":
        bot.send_photo(user_id, 'https://image.shutterstock.com/image-photo/python-snake-450w-184814297.jpg')
    if text == "мышь":
        bot.send_photo(user_id, 'https://mysekret.ru/wp-content/uploads/2017/01/5-47.jpg')
    if text == "кот":
        bot.send_photo(user_id, 'https://pp.userapi.com/c845122/v845122166/10ba66/gVecT_orwwY.jpg')

【讨论】:

  • 也谢谢你。当您输入 Internet 链接时,一切正常。
  • 图像文件是从计算机内存中还是打印出来的图像文件发送给机器人?
猜你喜欢
  • 2021-06-21
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 2022-11-11
  • 2021-10-03
相关资源
最近更新 更多