【问题标题】:cant send .mp3 files in python telegram bot无法在 python 电报机器人中发送 .mp3 文件
【发布时间】:2021-08-16 15:00:40
【问题描述】:
import logging
from telegram import Update, ForceReply
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
from telegram.files.audio import Audio


logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)

logger = logging.getLogger(__name__)


def start(update: Update, _: CallbackContext) -> None:
    """Send a message when the command /start is issued."""
    user = update.effective_user
    update.message.reply_markdown_v2(
        fr'Hi {user.mention_markdown_v2()}\!',
        reply_markup=ForceReply(selective=True),
    )


def help_command(update: Update, _: CallbackContext) -> None:
    """Send a message when the command /help is issued."""
    update.message.reply_text('Help!')

def sendAudio(update: Update, context: CallbackContext):
    context.bot.sendAudio(chat_id=update.message.chat_id, Audio=open("hxxxxxxxxxxxxxxxxxxx.mp3", "rb"), timeout=360)


def main() -> None:

    updater = Updater("xxxxxxxxxxxxxxxx")


    dispatcher = updater.dispatcher

    dispatcher.add_handler(CommandHandler("start", start))
    dispatcher.add_handler(CommandHandler("help", help_command))
    dispatcher.add_handler(CommandHandler("send", sendAudio))

    updater.start_polling()

    updater.idle()


if __name__ == '__main__':
    main()

这是我发送/发送显示此错误时的代码

2021-05-28 16:16:18,309 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception.
Traceback (most recent call last):
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\ext\dispatcher.py", line 447, in process_update
    handler.handle_update(update, self, check, context)
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\ext\handler.py", line 160, in handle_update
    return self.callback(update, context)
  File "e:\Projects\Python\Telegram bot\echo.py", line 28, in sendAudio
    context.bot.sendAudio(chat_id=update.message.chat_id, Audio=open("xxxxxxxxxxxxxxxxxxx.mp3", "rb"), timeout=360)
OSError: [Errno 22] Invalid argument: 'xxxxxxxxxxxxxxxxxxx.mp3'

【问题讨论】:

标签: python bots telegram


【解决方案1】:

bot 方法 send_audio 有一个参数 audio 和一个小写的 a - 你写的 Audio 用大写的 A 代替。 请注意,您可以使用 reply_audio 快捷方式简化代码。


免责声明:我目前是python-telegram-bot的维护者

【讨论】:

  • def help_command(update: Update, _: CallbackContext) -> None: update.message.reply_audio("https://xxxxxxxxxxxx/xxx.mp3") 喜欢这样吗?
  • 此链接有效http://www.mlk4.info/audio/71000/Snehaya-K-A-Suneka-Charuni-musiclk=audio.mp3 但此链接不工作https://www.fmderana.lk/helpers/download.php?id=5855&file=Himasha_Manuppriya_Dewiyane.mp3 它说文件"C:\Users\Acer\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\utils\request.py", line 273, in _request_wrapper raise BadRequest(message) telegram.error.BadRequest: Wrong file identifier/http url specified 如何解决这个问题
猜你喜欢
  • 1970-01-01
  • 2017-06-16
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 2019-06-02
  • 2021-09-10
  • 2019-02-25
  • 2016-03-11
相关资源
最近更新 更多