N
N
nakem2021-12-08 03:29:10
go
nakem, 2021-12-08 03:29:10

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

1 answer(s)
N
nakem, 2021-12-13
@nakem

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")
  }
}

If you need to process audio files like mp3 and so on, then this is the Audio structure and the content of the endpoint tb.OnAudio.
If you need waf and so on, then this is the Document structure and endpoint tb.OnDocument

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question