简体   繁体   中英

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

My file is .png , where am I wrong? If I replace sendPhoto() with sendDocument() everything works perfectly, but I need photos in my project. If I put directly graphUrl in sendPhoto, without using urllib2, it doesn't work (error 400 - bad request).

I think you have to specify the file extension for Telegram servers to recognize it as an image. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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