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
How to send photo on telegram bot
提问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.sendphoto
I would insert the path and the chat_id
of 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 subprocess
like 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,对于第二张照片,您有三个选项:
- Pass file_idif the photo is already uploaded to telegram servers (recommended because you don't need to reupload it).
- If the photo is uploaded somewhere else, pass the full http url and telegram will download it (max photo size is 5MB atm).
- Post the file using multipart/form-data like you want to upload it via a browser (10MB max photo size this way).
- 如果照片已经上传到电报服务器,则传递file_id(推荐,因为您不需要重新上传它)。
- 如果照片上传到其他地方,请传递完整的 http url,电报将下载它(最大照片大小为 5MB atm)。
- 使用 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")