Viral Sarvaiya
Viral Sarvaiya

Reputation: 781

Twilio call forwarding for specific time of day

With Twilio I'd like to implement a service that sends inbound calls to a voice recording during specific times of day.

Can Twilio provide this behavior?

Upvotes: 1

Views: 1376

Answers (2)

Soup Cup
Soup Cup

Reputation: 121

Here is cakePhp - twilio url is xyz.com/Aptw/dialMtmTextJobLine/ - see the view function slap in the middle of the example.

<?php
App::uses('AppController', 'Controller');
class AptwController extends AppController {
    public function beforeFilter() { $this->Auth->allow(); }

    public $forwardIn = '<?xml version="1.0" encoding="UTF-8"?>
        <Response>
            <Dial>
                <Sip>';
    public $forwardOut = '</Sip></Dial></Response>';

    public function dialMtmTextJobLine() {
       date_default_timezone_set("America/New_York");
       $t=date("H:i",time());if ($t>"09:00" && $t<"16:00") {$this->dial103(); }
       else { $this->dial2223(); }
    }

    public function dial103() { die($this->forwardIn.'sip:103@myast.com' . $this->forwardOut); }
    public function dial2223() { die($this->forwardIn.'sip:2223@myast.com' . $this->forwardOut); }

}

Between 9am and 4pm, when twilio fetches the xml from your app, it sees one thing, and outside of that it sees another. Twilio is amazing, but it is meant to be just part of the answer, and I respect that.

Watch your formatting on the xml, Twilio is particular. I had tried to scrunch everything on one line and Twilio would not parse it.

This example does not properly use view templates, I get that. It's not entirely cakey, instead of dying, and instead of using the class variables to contain the xml boilerplate, I'll end up moving that to a view, making this code even shorter.

In fact, dial103 could be rendered with a view with no php inside it.

But better yet, direct dial urls for Twilio could look like xyz.com/Aptw/dialx/103 for my purposes. In cakephp you parse the next argument after controller Aptw and action dialx like thus: function dialx($extension) ... and the 103 automagically jumps into the $extension variable. Then you set a variable for the view, that's cakey.

Upvotes: 1

philnash
philnash

Reputation: 73090

Twilio developer evangelist here. You can absolutely do that! I'm not sure what language or framework you're using, but here's an idea of how you'd accomplish this.

When Twilio receives a call on a Twilio number it makes a webhook request to your server to respond and tell it what to do with the call. The instructions are built in XML (TwiML). So, to do time based stuff you probably want to do something like this (my example is in Ruby using Sinatra as a web framework):

post '/call' do
  time = Time.now
  content_type 'text/xml'
  response = "<Response>"
  if out_of_hours?(time)
    response = "<Say>Please leave a message</Say><Record />"
  else
    response = "<Dial><Number>YOUR_PHONE_NUMBER</Number></Dial>"
  end
  response = "</Response>"
  response
end

You can then define out_of_hours? to follow the rules you want.

I hope this help, please let me know if you have any other questions.

Upvotes: 4

Related Questions