如何使用远程机器人从服务器发送文件? [英] How to send file from server using telebot?

查看:40
本文介绍了如何使用远程机器人从服务器发送文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个机器人,它通过 URL 从外部服务器发送文件.

I made a bot that sends files from an external server with a URL.

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

I want the bot to send files directly from your server.

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

What am I doing wrong? Why didn't the open() command work?

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')

阅读有关 open() 的更多信息

Read more about open()

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

Minimal working example to send local image:

import telebot

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

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

这篇关于如何使用远程机器人从服务器发送文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆