Infosys Microsoft Alliance and Solutions blog

« MYOC - Make Your Opinion Cloud- Series 2 | Main | MYOC - Update Twitter Status »

MYOC - Telephony with Twilio to Vote

MYOC (Make Your Opinion Count) – an online poll application hosted on Microsoft Azure, uses Twilio to make it easier for people to participate in the online polls. Twilio is telephony in the cloud which exposes RESTful APIs to build scalable voice applications. It supports both inbound and outbound telephony calls. Pricing is developer friendly with pay-as-you-go model.

MYOC uses Twilio in two ways –
1. Poll creator can place a call  for participant to caste his/her vote
2. A participant can dial-in for a particular poll to caste his/her vote

Let’s see what all it takes to use twilio in MYOC to call a participant and accept his/her vote or to handle an incoming call to cast a vote.

Twilio Account Setup

1. The first and foremost thing is to have a twilio account. To play around, you can register for a free account which gives $30 credit. Open the link https://www.twilio.com/try-twilio, provide your details and click on ‘TRY TWILIO’ button.
 

2. This will create a user account on twilio and show account dashboard. It will display following things –
a. API Credentials – which has account Sid and auth token to be used while calling twilio REST APIs from application
 

b. Trial Sandbox Details
 

c. Account Balance – it will show current available account balance


Getting Ready
1. Download twilio documentation from http://www.twilio.com/resources/tarball/twilio-documentation.zip to know how to use twilio in an application.

2. Download C# helper libraries for twilio from http://www.twilio.com/docs/libraries and add twiliorest.cs in the webrole project.

3. Add the parameters in csdef file as below -

<ConfigurationSettings>

  ...

  <!-- twilio account SID -->

  <Setting name="Acct_SID"/>

  <!-- twilio auth token -->

  <Setting name="Auth_Token"/>

  <!-- API version of twilio rest -->

  <Setting name="Api_Version"/>

  <!-- a US number authorized by twilio used to place outbound calls -->

  <Setting name="Caller_ID" />

</ConfigurationSettings>

4. Add the values of above defined settings in cscfg file as below -

<ConfigurationSettings>

  ...

  <Setting name="Acct_SID" value="**********************************"/>

  <Setting name="Auth_Token" value="********************************"/>

  <Setting name="Api_Version" value="2008-08-01"/>

  <Setting name="Caller_ID" value="XXX-XXX-XXXX"/>

</ConfigurationSettings>


Placing an outbound call on a given number

1. Read values form configuration file

string Acct_SID = RoleManager.GetConfigurationSetting("Acct_SID");

string Auth_Token = RoleManager.GetConfigurationSetting("Auth_Token");

string Api_Version = RoleManager.GetConfigurationSetting("Api_Version");

string Caller_Id = RoleManager.GetConfigurationSetting("Caller_ID");

2. Initialize call process by creating twilio account instance with account SID & auth token and creating the service URL to be called once participant receives the call.

//Initate the call

TwilioRest.Account twilioacc = new TwilioRest.Account(Acct_SID, Auth_Token);

 

// create the URL to post at Twilio to place a call

string postUrl = "/{0}/Accounts/{1}/Calls";

postUrl = string.Format(postUrl, Api_Version, Acct_SID);

 

// create the service URL to be called when participant vote over the call

HttpContext current = HttpContext.Current;

pollURL = "http://" + current.Request.Url.DnsSafeHost +

"/MyPollService/Poll/Call/{0}/{1}";

pollURL = string.Format(pollURL, pollId, userId);

3. Place the call on specified contact number from caller ID.

Hashtable vars = new Hashtable(3);

vars.Add("Caller", Caller_Id);

vars.Add("Called", contactNumber);

vars.Add("Url", pollURL);

string response = twilioacc.request(postUrl, "POST", vars);

4. The service invoked with the pollURL will handle the user response over call. User can provide the dialogues to use in the call and what action to take according to user input using Twilio Markup Language(TwiML). TwiML contains tags to define various activities like say a text, pause for defined time, get user input, play a recorded file, record the voice, call another service etc. Please refer to the TwiML documentation available at http://www.twilio.com/docs/api/2008-08-01/twiml/ to get more details. The service to handle the call will be as shown below -

HttpContext current = HttpContext.Current;

string welcomeMsg = "Welcome to the Poll created by " + pollToDial.CreatedBy + ".";

// If poll is not open at that time

If (!pollToDial.IsOpen)

{

    statusMessage = "Unfortunately the Poll is not available at this time. Please try again for another Poll.";     

    current.Response.Write("<Response><Say voice='woman'>" + welcomeMsg + "</Say><Pause length='1'/><Say voice='woman'>" + statusMessage + "</Say></Response>");

    return;

}

 

// Present poll details to user like poll name, poll question and poll choices, then prompt participant to submit his/her vote for any of the given choice

 

// poll will be submitted using below URL             

string action = @"http://" + current.Request.Url.DnsSafeHost + "/MyPollService/Poll/Call/Submit/" + pollName + "/" + pollSubmitter;

 

// get the selected choice and submit by calling poll submit service URL

string responseOfPoll = @"<Response><Say voice='woman'>" + welcomeMsg + "</Say><Gather method='Post' numDigits='2' action='" +

action + "' finishOnKey='#'>" + respond + @"</Gather></Response>";

 

current.Response.Write(responseOfPoll);

return;


Handling an Inbound Call

1. Map the ‘Uses URL’ of the twilio account with the application service URL and save. When user dials in the twilio number, this service will be invoked.

2. When participant logs in MYOC to vote for a poll, he/she will be given the dial-in number with PIN number and the poll ID. Participant will call the given number and enter the PIN, then he/she will be redirected to the service specified in Uses URL of twilio account.

3. The service will be similar to the service used in placing an outbound call using twilio. The only difference will be that participant will be prompted for entering the poll ID for which he/she wishes to vote.

4. The service to handle the call will be as shown below –

// get the poll ID entered by user on call

if (!string.IsNullOrEmpty(current.Request.Form["Digits"]))

{

    pollId = current.Request.Form["Digits"];

}

 

// if no poll ID has been entered by participant

if (string.IsNullOrEmpty(pollId))

{

    string actionSelf = current.Request.Url.ToString().Replace(":" + current.Request.Url.Port.ToString(), "");

    string introMsg =

    "Please enter the numeric digits of the Poll you wish to participate by pressing the keys on your touch tone phone.";

 

    current.Response.Write(@"<Response><Say voice='woman'>Welcome.</Say><Gather method = 'Post' numDigits='4' action ='" +

          actionSelf + "'><Say voice='woman'>" + introMsg + @"</Say></Gather></Response>");

    return;

}

5. If the participant has entered a valid poll ID then present the poll choices to the user and get his/her response and submit his vote as explained in the step 4 of above section ‘Placing an outbound call on a given number’.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Follow us on

Blogger Profiles

Infosys on Twitter