简体   繁体   English

Telepot 中的 sendPhoto() 对我的机器人不起作用

[英]sendPhoto() in Telepot doesn't work for my bot

I can't send photo, here the code with error:我无法发送照片,这里是有错误的代码:

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

Error: 2016-12-30T17:17:50.803142+00:00 app[worker.1]: TelegramError: (u'Bad Request: Photo has unsupported extension. Use one of .jpg, .jpeg, # .gif, .png, .tif or .bmp', 400, {u'ok': False, u'description': u'Bad Request: Photo has unsupported extension. Use one of .jpg, .jpeg, # .gif, .png, .tif or .bmp', u'error_code': 400})错误: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})

My file is .png , where am I wrong?我的文件是.png ,我错在哪里? If I replace sendPhoto() with sendDocument() everything works perfectly, but I need photos in my project.如果我用sendPhoto()替换sendDocument()一切正常,但我的项目中需要照片。 If I put directly graphUrl in sendPhoto, without using urllib2, it doesn't work (error 400 - bad request).如果我直接将graphUrl放在 sendPhoto 中,而不使用 urllib2,则它不起作用(错误 400 - 错误请求)。

I think you have to specify the file extension for Telegram servers to recognize it as an image.我认为您必须为 Telegram 服务器指定文件扩展名才能将其识别为图像。 For example:例如:

url = urllib2.urlopen('http://i.imgur.com/35HSRQ6.png')
bot.sendPhoto(chat_id, ('abc.png', url))

The filename doesn't matter, as long as the extension matches the image type.文件名无关紧要,只要扩展名与图像类型匹配即可。

You don't have to do that when uploading images from local disk, because the file extension can be guessed from the filesystem.从本地磁盘上传图像时,您不必这样做,因为可以从文件系统猜测文件扩展名。 However, you have to do it for a URL, because the file extension cannot be obtained otherwise.但是,您必须为 URL 执行此操作,因为否则无法获取文件扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM