[英]How would I upload a BytesIO image to Telegram through telepot Bot.sendPhoto(uid, photo)?
[英]sendPhoto() in Telepot doesn't work for my bot
我无法发送照片,这里是有错误的代码:
if command.startswith('/rank '):
rank(msg)
def rank(msg):
chat_id = msg['chat']['id']
command = msg['text']
user = msg['text'][6:]
graphUrl = 'https://www.website.com/servlet/graph/' + user + '-in_US.png'
print graphUrl
theGraph = urllib2.urlopen(graphUrl)
bot.sendPhoto(chat_id, theGraph, caption=('rank graph for ' + user + '.'))
错误:2016-12-30T17:17:50.803142+00:00 app[worker.1]: TelegramError: (u'Bad Request: Photo has unsupported extension. 使用 .jpg, .jpeg, # .gif, .png 之一, .tif 或 .bmp', 400, {u'ok': False, u'description': u'Bad Request: Photo has unsupported extension. 使用 .jpg, .jpeg, # .gif, .png, 之一。 tif 或 .bmp', u'error_code': 400})
我的文件是.png
,我错在哪里? 如果我用sendPhoto()
替换sendDocument()
一切正常,但我的项目中需要照片。 如果我直接将graphUrl
放在 sendPhoto 中,而不使用 urllib2,则它不起作用(错误 400 - 错误请求)。
我认为您必须为 Telegram 服务器指定文件扩展名才能将其识别为图像。 例如:
url = urllib2.urlopen('http://i.imgur.com/35HSRQ6.png')
bot.sendPhoto(chat_id, ('abc.png', url))
文件名无关紧要,只要扩展名与图像类型匹配即可。
从本地磁盘上传图像时,您不必这样做,因为可以从文件系统猜测文件扩展名。 但是,您必须为 URL 执行此操作,因为否则无法获取文件扩展名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.