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

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

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

关于python - 如何使用远程机器人从服务器发送文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65292828/

相关文章:

spring - 是否可以在运行时的Spring Boot中创建REST端点?

python - Mastermind Game 中重复某个功能的问题

python - Telegram Bot 位置 python

telegram - 允许机器人访问 Telegram Group 消息

telegram - 当创建者帐户被破坏时,Telegram channel 会发生什么?

firebase - 如何在 https 而不是 http 上运行 firebase(函数)模拟器?

php - TELEGRAMBOT 如何获取 ChosenInlineResult

python - 如何在多步 map-reduce 程序中运行一次最终的 'print' 语句?

python - 连接字符串 + 路径时停产

python - 目标与 networkx json 文件中的节点 ID 不匹配