Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bot to retrieve voice message #48

Closed
ferrlin opened this issue Oct 27, 2017 · 2 comments
Closed

Bot to retrieve voice message #48

ferrlin opened this issue Oct 27, 2017 · 2 comments

Comments

@ferrlin
Copy link

ferrlin commented Oct 27, 2017

Please point me to the right direction on implementing this.

I have the snippet so far:

onMessage{ implicit msg =>
request(GetFile(msg.voice.map(_.fileId).getOrElse(""))
.onComplete{
case Success(file) => ??? //what to do next to get the actual file (when i try file.filePath it gives me something like this --> voice/file_1)
}
}

Thanks,

@mukel
Copy link
Member

mukel commented Oct 27, 2017

Please check the GetFile documentation
Here's my working attempt, when you send a voice recording it replies with the file size, hope it helps:

class FileBot(token: String) extends ExampleBot(token) // just for testing
  with Polling
  with Commands {

  onMessage { implicit msg =>

    using(_.voice) { voice =>
      request(GetFile(voice.fileId)).onComplete {
        case Success(file) =>
          file.filePath match {

            case Some(filePath) =>
              // See https://core.telegram.org/bots/api#getfile
              val url = s"https://api.telegram.org/file/bot${token}/${filePath}"

              for {
                res <- Http().singleRequest(HttpRequest(uri = Uri(url)))
                if res.status.isSuccess()
                bytes <- Unmarshal(res).to[ByteString]
              } /* do */ {
                reply(s"File with ${bytes.size} bytes received.")
              }

            case None =>
              println("No file_path was returned")
          }

        case Failure(e) =>
          println("Exception: " + e) // poor's man logging
      }
    }
  }
}

@ferrlin
Copy link
Author

ferrlin commented Oct 28, 2017

thanks for this. :)

@mukel mukel closed this as completed Oct 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants