Search code examples
c#ivr

Create an Outbound Campaign through PlumVoice IVR using C#


I have been working with the PlumVoice IVR system for a while and now need to utilize their Outbound Calling capabilities. I need to do this in C# but am having difficulties figuring out how to proceed due to lack of documented C# support. Here are the specifics of my project:

  • I parse data (such as phone numbers) from files and then call [those] numbers with the files' data.
  • It is not an MVC project (you can see how it would have been easier if it were)

Now I need to:

  1. Create a Call [Campaign] with the queuecall web service
  2. Create reporting functionality for post-call processing

Solution

  • I have compiled and tested the necessary code for my project and it works exactly as desired (until the client updates the requirements, obviously).


    WebRequest method

    public static void PlumOutboundQueuecall(ObjectModel model)
    {
        // Create a request for the URL.        
        WebRequest request = WebRequest.Create(Settings.IvrOutboundApi); //ease of editing/reusability //http://outbound.plumvoice.com/webservice/queuecall.php
        request.Method = "POST";
        //request.ContentType = "multipart/form-data"; //This is only if I choose to upload a [.csv] file of phone numbers
        request.ContentType = "application/x-www-form-urlencoded";
    
        // I used http://stackoverflow.com/questions/14702902/post-form-data-using-httpwebrequest as reference for proper encoding
        StringBuilder postData = new StringBuilder();
        postData.Append(HttpUtility.UrlEncode("login") + "=" +              HttpUtility.UrlEncode("<MY_EMAIL_FOR_PLUM>") + "&");
        postData.Append(HttpUtility.UrlEncode("pin") + "=" +                HttpUtility.UrlEncode("<MY_PIN_FOR_PLUM>") + "&");
        postData.Append(HttpUtility.UrlEncode("phone_number") + "=" +       HttpUtility.UrlEncode(model.PhoneNumber) + "&");
        postData.Append(HttpUtility.UrlEncode("start_url") + "=" +          HttpUtility.UrlEncode("<MY_vXML_AS_ASPX_PAGE_SHOWN_BELOW>") + "&"); //"http://ip_address/MY_ASPX_PAGE.aspx"
        postData.Append(HttpUtility.UrlEncode("call_parameters") + "=" +    HttpUtility.UrlEncode("<THE_PHONE_MESSAGE_TEXT_TO_BE_READ>") + "&"); //includes 'model' properties
        postData.Append(HttpUtility.UrlEncode("result_url") + "=" +         HttpUtility.UrlEncode("<MY_RESULT_URL_AS_ASMX_PAGE_SHOWN_BELOW>")); //"http://ip_address/MY_ASMX_PAGE.asmx/PlumOutboundCallback"
    
        ASCIIEncoding ascii = new ASCIIEncoding();
        byte[] postBytes = ascii.GetBytes(postData.ToString());
    
        // add post data to request
        Stream postStream = request.GetRequestStream();
        postStream.Write(postBytes, 0, postBytes.Length);
        postStream.Flush();
        postStream.Close();
    
        // Get response
        // Used Fiddler to deconstruct/reverse-engineer
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            StreamReader reader = new StreamReader(response.GetResponseStream()); // Get the response stream  
            string result = reader.ReadToEnd(); // Read the contents and return as a string  
    
            if (result.Contains("failed"))
            {
                // Whatever error handling you want to do if your "result_url" page throws an error. I used this extensively while testing the proper way to set this up
            }
        }
    }
    

    My .vxml file as .aspx page. This is Published and sitting on the server ("ip_address/MY_ASPX_PAGE.aspx")

    <%Response.ContentType = "text/xml"%>
    <?xml version="1.0" ?>
    <vxml version="2.0">
        <form>
            <block>
                <prompt>
                    <%-- Set the "Custom Call Parameters" to be THE_PHONE_MESSAGE_TEXT_TO_BE_READ --%>
                    <%=request.form("call_parameters") %>  <%-- This says "Cannot resolve symbol 'request'" but just ignore it --%>
                </prompt>
            </block>
        </form>
    </vxml> 
    

    I know this appears to be written in error but just ignore it ('request' will be in red)


    My "result_url" is a .asmx page. This is Published and sitting on the server ("ip_address/MY_ASMX_PAGE.asmx/PlumOutboundCallback")

    /// <summary>
    /// Summary description for OutboundResultUrl
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class OutboundResultUrl : System.Web.Services.WebService
    {
        [WebMethod]
        public string PlumOutboundCallback()
        {
            bool heartbeat = string.IsNullOrEmpty(HttpContext.Current.Request.Form["phone_number"]); //Plum implements a heartbeat and checks that this 'result_url' is working. A 'phone_number' has to exist in order for me to process the data FOR a call (duh) so I check to see if it is null or empty
    
            if (!heartbeat)
            {
                //BusinessLayer.OutboundCallback model = new BusinessLayer.OutboundCallback { phone_number = HttpContext.Current.Request.Form["phone_number"], message_reference = HttpContext.Current.Request.Form["message_reference"], call_id = Convert.ToInt32(HttpContext.Current.Request.Form["call_id"]), result = HttpContext.Current.Request.Form["result"], callee_type = HttpContext.Current.Request.Form["callee_type"], attempts = Convert.ToInt32(HttpContext.Current.Request.Form["attempts"]), last_attempt_timestamp = HttpContext.Current.Request.Form["last_attempt_timestamp"], duration = Convert.ToInt32(HttpContext.Current.Request.Form["duration"]) }; //if you're into this sort of thing
                BusinessLayer.OutboundCallback model = new BusinessLayer.OutboundCallback();
                model.phone_number = HttpContext.Current.Request.Form["phone_number"];
                model.message_reference = HttpContext.Current.Request.Form["message_reference"];
                model.call_id = Convert.ToInt32(HttpContext.Current.Request.Form["call_id"]);
                model.result = HttpContext.Current.Request.Form["result"];
                model.callee_type = HttpContext.Current.Request.Form["callee_type"];
                model.attempts = Convert.ToInt32(HttpContext.Current.Request.Form["attempts"]);
                model.last_attempt_timestamp = HttpContext.Current.Request.Form["last_attempt_timestamp"];
                model.duration = Convert.ToInt32(HttpContext.Current.Request.Form["duration"]);
    
                string stringResultData = string.Format("Collecting parameters posted into here. <br />Phone Number = {0}, <br />Message Reference = {1}, <br />Call ID = {2}, <br />Result = {3}, <br />Callee Type = {4}, <br />Attempts = {5}, <br />Last Attempt Timestamp = {6}, <br />Duration = {7}",
                                        model.phone_number, model.message_reference, model.callee_type, model.result, model.callee_type, model.attempts, model.last_attempt_timestamp, model.duration); //this is for debugging/testing purposes
    
                try
                {
                    // Whatever you want to do with this data! Send it in an email, Save it to the database, etc.
                }
                catch (Exception error)
                {
                    Console.WriteLine("Error: " + error);
                }
            }
    
            return "Plum Outbound API has posted back to this 'result_url' successfully.";
        }
    }
    

    LAST STEP: Remember to set your campaign to run! Change the status under the "Campaign Status" cell at http://hosting.plumgroup.com/developer_tools_outbound.php

    I really hope this helps everyone who utilizes PlumVoice through C#. Please comment and share any improvements or questions!