forked from bot4s/telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoiceFileBot.scala
More file actions
44 lines (38 loc) · 1.46 KB
/
Copy pathVoiceFileBot.scala
File metadata and controls
44 lines (38 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import org.apache.pekko.http.scaladsl.Http
import org.apache.pekko.http.scaladsl.model.{ HttpRequest, Uri }
import org.apache.pekko.http.scaladsl.unmarshalling.Unmarshal
import org.apache.pekko.util.ByteString
import cats.instances.future._
import cats.syntax.functor._
import com.bot4s.telegram.api.declarative.Commands
import com.bot4s.telegram.future.Polling
import com.bot4s.telegram.methods._
import scala.concurrent.Future
import scala.util.{ Failure, Success }
/**
* This bot receives voice recordings and outputs the file size.
*/
class VoiceFileBot(token: String) extends PekkoExampleBot(token) with Polling with Commands[Future] {
onMessage { implicit msg =>
using(_.voice) { voice =>
request(GetFile(voice.fileId)).andThen {
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]
_ <- reply(s"File with ${bytes.size} bytes received.")
} yield ()
case None =>
reply("No file_path was returned")
}
case Failure(e) =>
logger.error("Exception: " + e) // poor's man logging
}.void
}
}
}