【问题标题】:Can't send audio file with telegram bot无法使用电报机器人发送音频文件
【发布时间】:2021-10-25 01:06:48
【问题描述】:

我一直在试图让我的机器人发送音频文件并尝试阅读我能阅读的所有内容,但由于某种原因仍然无法正常工作......

这是我的代码,它很长,所以我只包含了相关部分:

import Constants as keys
from telegram.ext import *
from telegram import ReplyKeyboardMarkup,InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery, Update
import time
import Responses as R
import song
import logging
import emoji
import random
import requests

def yes_command(update, context):
    time.sleep(5)
    update.message.reply_text("Let's test here, press play to listen.")
    time.sleep(3)
    update.message.reply_audio("press play", audio=open("C:/Users/0836/Documents/omsk/bot2/song/bootgong.mp3"))

def main():
    updater = Updater(keys.API_KEY, use_context=True)
    dp = updater.dispatcher

    dp.add_handler(CommandHandler("start", start_command))
    dp.add_handler(CommandHandler("play", play_command))
    dp.add_handler(CommandHandler("no", no_command))
    dp.add_handler(CommandHandler("yes", yes_command))
    dp.add_handler(MessageHandler(Filters.text, handle_message))
    dp.add_handler(MessageHandler(Filters.photo, photo_command))
    updater.start_polling()
    updater.idle()
    
    
    main()

我对机器人和 python 真的很陌生,所以不确定我做错了什么,请帮忙!我看过有关将其上传到电报服务器的内容,但不确定如何?

【问题讨论】:

  • 先尝试加载文件,然后添加到回复音频功能
  • 我在哪里加载它?对不起,我很新
  • f = open("demofile.txt", "r")

标签: python bots telegram telegram-bot


【解决方案1】:

尝试在 open 中添加'rb'

update.message.reply_audio(open("C:/Users/0836/Documents/omsk/bot2/song/bootgong.mp3", "rb"), title="press play")

【讨论】:

  • 我收到此错误:TypeError: reply_audio() got multiple values for argument 'audio'
  • 哦第一个参数应该是文件(这是一个辅助方法)
  • lol 也是 stackoverflow 的新手,刚刚意识到如何做到这一点 :) 谢谢!
猜你喜欢
  • 2017-06-16
  • 2021-08-16
  • 2019-02-25
  • 2020-03-10
  • 2022-09-28
  • 2016-07-18
  • 2019-06-02
  • 2021-09-10
  • 2016-05-04
相关资源
最近更新 更多