簡體   English   中英

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

錯誤: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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM