Python 如何在电报机器人上发送照片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36778321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:19:37  来源:igfitidea点击:

How to send photo on telegram bot

pythontelegram-bottelepot

提问by rollotommasi

i'm just implementing a simple bot who should send some photos and videos to my chat_id. Well, i'm using python, this is the script

我只是在实现一个简单的机器人,它应该将一些照片和视频发送到我的chat_id. 好吧,我正在使用 python,这是脚本

import sys
import time
import random
import datetime
import telepot

def handle(msg):
    chat_id = msg['chat']['id']
    command = msg['text']

    print 'Got command: %s' % command

    if command == 'command1':
        bot.sendMessage(chat_id, *******)
    elif command == 'command2':
        bot.sendMessage(chat_id, ******)
    elif command == 'photo':
        bot.sendPhoto(...)

bot = telepot.Bot('*** INSERT TOKEN ***')
bot.message_loop(handle)
print 'I am listening ...'

while 1:
    time.sleep(10)

In the line bot.sendphotoI would insert the path and the chat_idof my image but nothing happens.

在该行中,bot.sendphoto我将插入路径和chat_id图像的路径,但没有任何反应。

Where am I wrong?

我哪里错了?

thanks

谢谢

回答by Cipri

I've tried also sending from python using requests. Maybe it's late answer, but leaving this here for others like me.. maybe it'll come to use.. I succeded with subprocesslike so:

我也尝试过使用请求从 python 发送。也许是迟到的答案,但把它留在这里给像我这样的其他人..也许它会被使用..我subprocess像这样成功了:

def send_image(botToken, imageFile, chat_id):
        command = 'curl -s -X POST https://api.telegram.org/bot' + botToken + '/sendPhoto -F chat_id=' + chat_id + " -F photo=@" + imageFile
        subprocess.call(command.split(' '))
        return

回答by Majid A

you can use following lines:

您可以使用以下几行:

bot.send_photo(chat_id, photo=open('path', 'rb'))
# That path is local path image or use following line to use url from internet
bot.send_photo(chat_id, 'your URl')

回答by RuralGalaxy

You need to pass 2 params

你需要传递2个参数

bot.sendPhoto(chat_id, 'URL')

回答by Arash Moosapour

sendPhotorequires at least two parameters; first one is target chat_id, and for second one photoyou have three options:

sendPhoto至少需要两个参数;第一个是目标chat_id,对于第二张照片,您有三个选项:

  1. Pass file_idif the photo is already uploaded to telegram servers (recommended because you don't need to reupload it).
  2. If the photo is uploaded somewhere else, pass the full http url and telegram will download it (max photo size is 5MB atm).
  3. Post the file using multipart/form-data like you want to upload it via a browser (10MB max photo size this way).
  1. 如果照片已经上传到电报服务器,则传递file_id(推荐,因为您不需要重新上传它)。
  2. 如果照片上传到其他地方,请传递完整的 http url,电报将下载它(最大照片大小为 5MB atm)。
  3. 使用 multipart/form-data 发布文件,就像您想通过浏览器上传文件一样(这种方式最大照片大小为 10MB)。

回答by Manchana

I have used the following command while using python-telegram-botto send the image along with a caption:

我在使用python-telegram-bot发送图像和标题时使用了以下命令:

 context.bot.sendPhoto(chat_id=chat_id, photo=
"url_of_image", caption="This is the test photo caption")