【问题标题】:How to send file from server using telebot?如何使用远程机器人从服务器发送文件?
【发布时间】:2020-12-14 16:37:41
【问题描述】:

我制作了一个机器人,它使用 URL 从外部服务器发送文件。

我希望机器人直接从您的服务器发送文件。

我做错了什么?为什么open() 命令不起作用?

import telebot

bot = telebot.TeleBot("Token")

@bot.message_handler(commands=['start','help'])
def send_start_message(message):
    bot.reply_to(message, "Hi.")

@bot.message_handler(func=lambda m: True)
def echo_all(message):
    print(message.text)
    duck = open('duck.png','r')
    bot.send_photo(message.chat.id, duck)
    bot.send_message(message.chat.id, "hi")

bot.polling()

【问题讨论】:

  • 你得到什么错误信息?

标签: python telegram telegram-bot python-telegram-bot


【解决方案1】:

您需要告诉 使用二进制模式

duck = open('duck.png', 'rb')

Read more about open()


发送本地图像的最小工作示例:

import telebot

bot = telebot.TeleBot("<YOUR-TOKEN-HERE>")
cid = <YOUR-ID-HERE>

img = open('image.jpg', 'rb')
bot.send_photo(cid, img)

【讨论】:

  • 谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-12-24
  • 2017-01-03
  • 2016-09-17
  • 1970-01-01
  • 2021-07-06
  • 2011-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多