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

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

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

我的文件是.png,我哪里错了? 如果我用 sendDocument() 替换 sendPhoto() 一切正常,但我的项目中需要照片。 如果我直接将 graphUrl 放入 sendPhoto,而不使用 urllib2,它不起作用(错误 400 - 错误请求)。

我认为您必须指定 Telegram 服务器的文件扩展名才能将其识别为图像。例如:

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

文件名无关紧要,只要扩展名与图像类型匹配即可。

从本地磁盘上传图片时不必这样做,因为可以从文件系统中猜出文件扩展名。但是,你必须为 URL 执行此操作,因为否则无法获得文件扩展名。