如何在 pyTelegramBotAPI 中处理音频消息?

时间:2021-01-16 11:30:32

标签: python bots telegram

我使用 pyTelegramBotAPI 作为框架来创建电报机器人。 我在处理音频消息时遇到了一些问题,我不明白我错在哪里。

这是我的代码:

Activity

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

我不太清楚如何使用 pyTelegramBotAPI,因为我也遇到了无法解决的问题,所以我放弃了 python-telegram-bot,与 pyTelegramBotAPI 相比,它的文档更好,社区更大,更多积极开发,还有一个活跃的 Telegram group,您可以在其中直接向使用此包装器的其他开发者寻求帮助。

因此,如果您有兴趣更改为 python-telegram-bot,则您的机器人的代码将如下所示:

from telegram import Update
from telegram.ext import Updater, MessageHandler, Filters

token = "" #Insert your token here


def message_handler(update, context):
    update.message.reply_text("Hello")

def audio_handler(update, context):
    if update.message.chat.type == "private": #Checks if the chat is private
        update.message.reply_text("What a nice sound! I'm not here to listen to some audio, tho. My work is to wish a good night to all members of a group chat")



def main():
    """Start the bot."""
    updater = Updater(token, use_context=True)

    # Get the dispatcher to register handlers
    dispatcher = updater.dispatcher


    # on noncommand i.e message
    # Use this if you want to handle also other messages
    dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, message_handler))

    dispatcher.add_handler(MessageHandler(Filters.audio & ~Filters.command, audio_handler))

    # Start the Bot
    updater.start_polling()

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()


if __name__ == '__main__':
    main()

这里还有一些 official examples 使用此包装器制作的机器人,您可以使用它们来更好地理解包装器,如果您想了解更多信息,请随时通过电报 @Husnainn DM 我。