Plivo Scala helper Library is a Scala API for the Plivo framework. It is heavily derived from Dag Liodden's Scwilio library. It is currently under initial development, with only a subset of the Plivo functionality currently supported. The API aims to deliver Plivo functionality using two layers of abstraction:
- Basic Plivo methods and RESTXML generation
- Higher level "phone devices" where all HTTP and URL plumbing is abstracted away and replace with plain functions
To invoke Plivo methods, get an PlivoClient
instance:
import org.plivo._
import org.plivo.restapi._
val server = new URL("http://127.0.0.1:8088/")
val authId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
val authToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
val plivoClient = PlivoClient(server, authId, authToken)
Then invoke a method, e.g. for dialing a number:
val call = CallOperation(from = "12125550001",
to = "12125551234",
gateways = List("sofia/gateway/att/"),
answerUrl = new URL("http://example.com/answer.xml"))
plivoClient.execute(call)
To generate some RESTXML, put this in a handler of whatever web framework you are using:
import org.plivo.restxml._
val response = Response(
Speak("Hi. This is an automated call."),
Wait(2),
Play(new URL("http://example.com/cowbell.mp3")))
val stringResponse = RestXml(response).toString
Making stateful Plivo services can be a pain. Using Phone
instances, the HTTP
plumbing can can be removed completely. This allows for code like this:
import org.plivo._
import org.plivo.restapi._
import org.plivo.restxml._
import org.plivo.uf._
val server = new URL("http://127.0.0.1:8088/")
val authId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
val authToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
val plivoClient = PlivoClient(server, authId, authToken)
// activate phone instance to receive Plivo callbacks
val port = 8181
val phone = new UnfilteredPhone(port)
phone.activate()
// implicitly convert Scala functions to callback URLs
import phone.URLMaker._
val call = CallOperation(from = "12125550001",
to = "12125551234",
gateways = List("sofia/gateway/att/"),
answerUrl = (call: ActiveCall) => {
// via the magic of Scala implicits, this
// anonymous function is made in to a callback
// URL; freeing developers from HTTP plumbing
Response(
Speak("Hi. This is an automated call."),
Wait(2),
Play(new URL("http://example.com/cowbell.mp3")))
})
plivoClient.execute(call)
Neat, huh?