cannot upload audio files in Bale bot using python-bale-bot
Question:
I intend to create a bot in a Telegram-like messenger called Bale that should send audio files. I am using python-bale-bot which is a wrapper for Bale Messenger API. When I try to open the mp3 file and read it and pass it to the method, I get an error that I did not find its solution anywhere.
Here is the code:
"""keyboard handler"""
@client.listen(EventType.CALLBACK)
async def when_receive_callback(callback: CallbackQuery):
if callback.data == "audio":
audio_file = open("/path_to/audio_file.mp3","rb")
audio = audio_file.read()
await callback.message.reply_document(audio)
And when the users clicks on the keyboard button and the callback is sent, I get the following error:
error on_callback Can not serialize value type: <class 'int'>
headers: {}
value: 800646076
Any ideas?
Answers:
However, I found the solution to this issue. To see the complete discussion, read what the library maintainer suggested here.
And if you don’t feel like reading it, here’s the summary:
To send an audio file in Bale chatbot, we can follow these steps:
- First, send the audio file to the bot in Bale messenger in order to find its
file_id
.
@bot.listen(bale.EventType.MESSAGE)
async def on_callback(message: bale.Message):
if message.document:
print(message.document.file_id)
- Now that we have the file_id, we can send the file to them, as if the bot is forwarding a document message.
@bot.listen(bale.EventType.CALLBACK)
async def on_callback(callback: bale.CallbackQuery):
await callback.message.chat.send_document(document = "Your file id", ...)
I intend to create a bot in a Telegram-like messenger called Bale that should send audio files. I am using python-bale-bot which is a wrapper for Bale Messenger API. When I try to open the mp3 file and read it and pass it to the method, I get an error that I did not find its solution anywhere.
Here is the code:
"""keyboard handler"""
@client.listen(EventType.CALLBACK)
async def when_receive_callback(callback: CallbackQuery):
if callback.data == "audio":
audio_file = open("/path_to/audio_file.mp3","rb")
audio = audio_file.read()
await callback.message.reply_document(audio)
And when the users clicks on the keyboard button and the callback is sent, I get the following error:
error on_callback Can not serialize value type: <class 'int'>
headers: {}
value: 800646076
Any ideas?
However, I found the solution to this issue. To see the complete discussion, read what the library maintainer suggested here.
And if you don’t feel like reading it, here’s the summary:
To send an audio file in Bale chatbot, we can follow these steps:
- First, send the audio file to the bot in Bale messenger in order to find its
file_id
.
@bot.listen(bale.EventType.MESSAGE)
async def on_callback(message: bale.Message):
if message.document:
print(message.document.file_id)
- Now that we have the file_id, we can send the file to them, as if the bot is forwarding a document message.
@bot.listen(bale.EventType.CALLBACK)
async def on_callback(callback: bale.CallbackQuery):
await callback.message.chat.send_document(document = "Your file id", ...)