pyc-website

main website for pyc inc.

git clone https://9o.is/git/pyc-website.git

Twilio.scala

(1210B)


      1 package inc.pyc
      2 package lib
      3 
      4 import config._
      5 import dispatch._, Defaults._
      6 import net.liftweb.util.{Mailer, Props}
      7 import com.ning.http.client.Response
      8 import net.liftmodules.mongoauth.MongoAuth
      9 
     10 object Twilio extends net.liftweb.common.Logger {
     11 
     12   lazy val sid = Props.get("twilio.sid", "")
     13   lazy val token = Props.get("twilio.token", "")
     14   lazy val phone = Props.get("twilio.phone", "")
     15   
     16   def sms(to: String, body: String)(implicit countrycode: String = "+1"): Boolean = {
     17     val host = :/("api.twilio.com").secure / "2010-04-01" / "Accounts" / sid as_!(sid,token)
     18     val r: Future[Response] = Http(host / "Messages.json" << Map("From" -> phone, "To" -> to, "Body" -> body))
     19     
     20     val created = r().getStatusCode() == 201 // CREATED  
     21     info("Status: "+r().getStatusCode())
     22     
     23     if(!created) notifyByEmail(r().getStatusText())
     24     created
     25   }
     26   
     27   /** 
     28    *  Notify us of the new applicant by email, 
     29    *  so we can begin reviewing immediately.
     30    */
     31   def notifyByEmail(msg: String): Unit = {
     32     import Mailer._
     33         
     34     sendMail(
     35       From(MongoAuth.systemFancyEmail),
     36       Subject("Technical: Twilio SMS Failed"),
     37       To(Emails.technical),
     38       PlainMailBodyType(msg)
     39     )
     40   }
     41 }