【问题标题】:sendPhoto() in Telepot doesn't work for my botTelepot 中的 sendPhoto() 不适用于我的机器人
【发布时间】:2017-01-07 21:37:35
【问题描述】:

我无法发送照片,这里的代码有错误:

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:照片的扩展名不受支持。使用 .jpg 之一, .jpeg, # .gif, .png, .tif 或 .bmp', 400, {u'ok': False, u'description':u'Bad Request:照片的扩展名不受支持。利用 .jpg、.jpeg、# .gif、.png、.tif 或 .bmp' 之一,u'error_code': 400})

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

【问题讨论】:

    标签: python python-2.7 urllib2 telegram telegram-bot


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 它工作正常!非常感谢,Telepot 很棒! :)
    猜你喜欢
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 2021-12-12
    相关资源
    最近更新 更多