Answer the question
In order to leave comments, you need to log in
How to process a voice message in the framework for telegram bots telebot?
I am writing a telegram bot in Go using https://github.com/tucnak/telebot .
I already looked all over the documentation, but there is simply no information about this. I've searched the sources to no avail. I assumed that there should simply be a reader in the Voice structure, from where I count the audio bytes. I miss something and don't do it. Please write a simple function that will return the same audio in response to voice, thanks.
Or tell me how can I find a project on github where there is work with voice messages through this framework?
Answer the question
In order to leave comments, you need to log in
I figured it out. Maybe someone will be useful.
Voice messages sent to telegram are Opus ogg.
That is, you need to handle all voice messages like this,
bot.Handle(tb.OnVoice, voiceHandler)
any handler in this framework should have such a prototype `func fun(msg *tb.Message)`
msg is a structure that stores all information about the message. Accordingly, `msg.Voice` will be available in the voice message handler.
This is already a voice message structure with content like `audio/opus`
The file structure is built into the voice structure. She has fileID. With this ID, the file of our voice message is stored on the cart's servers. Using the `bot.FileURLByID(msg.Voice.FileID)` function, we can get the URL where we will make a request and get our audio bytes.
Here is the code
func (r *RecognitionVoiceHandler) VoiceRecognize(msg *tb.Message) {
ctx := context.TODO()
isFmt, encoding := checkFmt(msg, r.TeleContent())
if encoding == "ogg" {
encoding = "opus"
}
if !isFmt {
r.logger.Warnf(ctx, "Wrong format: %s", msg.Voice.MIME)
r.bot.Send(msg.Sender, fmt.Sprintf(formatError, msg.Voice.MIME))
}
fileURL, err := r.bot.FileURLByID(msg.Voice.FileID)
if err != nil {
r.logger.Error(ctx, "Can't get URL from fileID")
r.bot.Send(msg.Sender, telegramError)
}
text, err := r.recognize(ctx, fileURL, encoding)
if err != nil {
r.bot.Send(msg.Sender, telegramError)
}
if _, err = r.bot.Send(msg.Sender, text); err != nil {
r.logger.Error(ctx, "Can't send telegram response")
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question