如何使用远程机器人从服务器发送文件?

How to send file from server using telebot?

我制作了一个机器人,它使用 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()

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

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)