【问题标题】:How to use sendAudio in Telegram bot (Python)如何在 Telegram bot (Python) 中使用 sendAudio
【发布时间】:2016-11-26 20:35:47
【问题描述】:

我有一个 Telegram 机器人,它回复文本和图像,但在回复中发送 MP3 文件时遇到问题。有人可以帮忙吗?

这部分代码定义了回复:

        def reply(msg=None, img=None, aud=None):
        if msg:
            resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({
                'chat_id': str(chat_id),
                'text': msg.encode('utf-8'),
                'disable_web_page_preview': 'false',
                # 'reply_to_message_id': str(message_id),
                'reply_markup': json_keyboard,
            })).read()
        elif img:
            resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [
                ('chat_id', str(chat_id)),
                ('reply_to_message_id', str(message_id)),
            ], [
                ('photo', 'image.jpg', img),
            ])
        elif aud:
            resp = multipart.post_multipart(BASE_URL + 'sendAudio', [
                ('chat_id', str(chat_id)),
                ('reply_to_message_id', str(message_id)),
            ], [
                ('audio', 'aud.mp3', aud),
            ])
        else:
            logging.error('no msg or img specified')
            resp = None

这个定义了它应该返回的消息类型:

       elif 'Two' in text:
        img = Image.open('statimg/firstf.jpg')
        output = StringIO.StringIO()
        img.save(output, 'JPEG')
        reply(img=output.getvalue())
    elif 'Three' in text:
        aud = open('statimg/firsta.mp3')
        output = StringIO.StringIO()
        aud.save(output, 'MP3')
        reply(aud=output.getvalue())
    elif 'One' in text:
        # json_keyboard = json.dumps({keym: [bline3]})
        bline1 = [b1]
        bline2 = [b2]
        json_keyboard = json.dumps({keym: [bline1, bline2]})
        if func6.state == 0:
            reply('Hello text1')
            func6()
        elif func6.state == 1:
            func6()
            reply('Hello text2')

对于文本中的“一”和“二”效果很好(“一”返回文本,“二”返回图像),但对于“三”,它不返回 mp3 文件。

可能是什么问题?提前非常感谢!

【问题讨论】:

    标签: python json mp3 telegram telegram-bot


    【解决方案1】:

    我认为问题出在output = StringIO.StringIO()

    你应该使用 io.BytesIO()

    这是我正在使用的工作代码:

    def sendTelegramAudio(self, file_url, text):
        url = "https://api.telegram.org/botxxx:yyyy/sendAudio";
        remote_file = requests.get(file_url)
        file1 = io.BytesIO(remote_file.content)
        file1.name = 'audio.mp3'
        files = {'audio': file1}
        data = {'chat_id' : "@your_channel", 'caption':text}
        r= requests.post(url, files=files, data=data)
        print(r.status_code, r.reason, r.content)
    

    【讨论】:

    • 谢谢!有没有机会在不使用 requests.get() 的情况下使用这种 io.BytesIO 方法?通过 multipart.post_multipart() 传输数据?
    【解决方案2】:

    通过这种方式简单地解决了问题:

            elif aud:
                resp = multipart.post_multipart(BASE_URL + 'sendAudio', [
                    ('chat_id', str(chat_id)),
                    #('caption', 'Music in the park'),
                    ('reply_markup', json_keyboard),
                ], [
                    ('audio', 'Play', aud),
                ])
    
    reply(aud=urllib.urlopen('statimg/musone.mp3').read())
    

    【讨论】:

      猜你喜欢
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 2019-08-07
      • 2020-10-09
      • 2020-03-26
      相关资源
      最近更新 更多