We've Moved! Please visit our new and improved forum over at our new portal: https://portal.plumvoice.com/hc/en-us/community/topics

How to use data tag and XML document with possible iteration

Questions and answers about IVR programming for Plum DEV

Moderators: admin, support

Post Reply
Sparker
Posts: 14
Joined: Tue Jan 27, 2015 10:48 am

How to use data tag and XML document with possible iteration

Post by Sparker »

Please pardon extreme noobie questions...however I'm new to VXML, IVR and XML...I will try to explain what I'm attempting. I know you are a php shop, however we are a .NET shop, so I'm trying to do all this in C#, with ASP.NET. I can attempt to translate php code into C#, but sometimes have difficulty.

To begin, I am performing outbound calling. What I'm attempting to do is dynamically create a questionnaire/survey based on parameters that are posted to my start_url -- these will be put into the call_parameters. My database consists of tables of questions and tables of possible answer choices (I'm simplifying this, obviously); a question, of course, can/will have multiple answer choices.

I can successfully initiate a call and post a call_parameter to my start_url. I can then, in my code behind query my database and even have the application call me and say the questionnaire greeting to me. I am also, in code behind, able to get a list of questions...however it's there that I run into problems. I am not sure how then transfer this list from my code behind into a vxml and then iterate through each question and present the possible choices...

My issues are--I need to do this dynamically because each questionnaire may be different, therefore the questions/answers may be different and therefore I do not know how many questions I will be processing. I was thinking to create an XML document and using the data tag...however I'm running into difficulties doing this as well. I'm thinking I possibly call this in the wrong place as I create the xml document in the same code behind and the same page..and I also do not save it to my server, I simply pass it in memory...

Any guidance, examples, assistance would be greatly appreciated.

Currently this is what I have:

ASPX Code

Code: Select all

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="playGreeting.aspx.cs" Inherits="PVTest.playGreeting" %>
<%
    Response.ContentType = "text/xml";
%><?xml version='1.0'?>
<vxml version="2.0">
    <var name="callParam" expr =" '<% =Request["callParams"] %>'" />
    <form id="Welcome">
    <block>
        <prompt>
            Call Params are: <value expr="callParam" />
            <%=qGreeting.ToString() %>>
        </prompt>
        <goto next="#playQuestion" />
    </block>
  </form>
   <form id="playQuestion">
        <var name="datasource" expr="<%=myDoc;%>" />
        <data name="domQuestion" srcexpr="datasource" />
        <block>
            <script>
                function question(QuestionID, QuestionName, QuestionTypeID,
                        QuestionText, QuestionIsActive, OptionGroupID, QuestionTypeText,
                        QuestionActionId, QuestionActionText, QuestionSkipLogic,
                        QuestionCSSClass, QuestionOrder, QuestionIsRandom, QuestionRequired) {
                    this.QuestionID = QuestionID;
                    this.QuestionName = QuestionName;
                    this.QuestionTypeID = QuestionTypeID;
                    this.QuestionText = QuestionText;
                    this.QuestionIsActive = QuestionIsActive,
                    this.OptionGroupID = OptionGroupID;
                    this.QuestionTypeText = QuestionTypeText;
                    this.QuestionActionId = QuestionActionId;
                    this.QuestionActionText = QuestionActionText;
                    this.QuestionSkipLogic = QuestionSkipLogic;
                    this.QuestionCSSClass = QuestionCSSClass;
                    this.QuestionOrder = QuestionOrder;
                    this.QuestionIsRandom = QuestionIsRandom;
                    this.QuestionRequired = QuestionRequired;
                };

                var q = domQuestion.documentElement.getElementsByTagName("Question");
                var qArray = [];
                for (var i = 0; i = i.q.length; i++) {
                    var newQ = new question
                        (q.item(i).childNode[0].toString(),
                         q.item(i).childNode[1].toString(),
                        q.item(i).childNode[2].toString(),
                        q.item(i).childNode[3].toString(),
                        q.item(i).childNode[4].toString(),
                        q.item(i).childNode[5].toString(),
                        q.item(i).childNode[6].toString(),
                        q.item(i).childNode[7].toString(),
                        q.item(i).childNode[8].toString(),
                        q.item(i).childNode[9].toString(),
                        q.item(i).childNode[10].toString(),
                        q.item(i).childNode[11].toString(),
                        q.item(i).childNode[12].toString(),
                        q.item(i).childNode[13].toString()
                        );
                    qArray[i] = newQ;
                }
            </script>
            <prompt>
                <foreach item="question" array="qArray">
                    <value expr="question.QuestionText"></value>
                </foreach>
            </prompt>
        </block>
    </form>
</vxml> 
Code behind:

Code: Select all

private XDocument _doc;
        public XDocument myDoc { get { return _doc; } }

        private Questionnaire qt;
        public Questionnaire questionnaire { get { return qt; } }

        public string callParam;
        private string greeting;
        public string qGreeting { get { return greeting; } }

      protected void GetGreeting()
        {
            callParam = Request["callParams"];

            int questID = Convert.ToInt32(callParam);
            HttpClient client = new HttpClient();
            string baseUrl = "http://XXX.XX.XXX.XX/PVTest/";

            client.BaseAddress = new Uri(baseUrl);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            string serviceUrl;
            serviceUrl = "api/Questionnaire/" + questID.ToString();

            HttpResponseMessage response = client.GetAsync(serviceUrl).Result;
            if (response.IsSuccessStatusCode)
            {
                Questionnaire quest = response.Content.ReadAsAsync<Questionnaire>().Result;
                qt = quest;
                greeting = quest.QuestionnaireGreeting.ToString();

                XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", string.Empty),
                        new XElement("questions")
                     );

                foreach (QuestionnaireQuestions q in qt.Questions)
                {
                    doc.Root.Add(
                        new XElement("Question",
                            new XElement("QuestionID", q.QuestionID),
                            new XElement("QuestionName", q.QuestionName),
                            new XElement("QuestionTypeId", q.QuestionTypeId),
                            new XElement("QuestionText", q.QuestionText),
                            new XElement("QuestionIsActive", q.QuestionIsActive),
                            new XElement("OptionGroupID", q.OptionGroupID),
                            new XElement("QuestionTypeText", q.QuestionTypeText),
                            new XElement("QuestionActionId", q.QuestionActionId),
                            new XElement("QuestionActionText", q.QuestionActionId),
                            new XElement("QuestionSkipLogic", q.QuestionSkipLogic),
                            new XElement("QuestionCSSClass", q.QuestionCSSClass),
                            new XElement("QuestionOrder", q.QuestionOrder),
                            new XElement("QuestionIsRandom", q.QuestionIsRandom),
                            new XElement("QuestionRequired", q.QuestionRequired)
                    ));
                }

                _doc = doc;
            }
            else
            {
                greeting = "No greeting.";
            }

        }

    }

support
Posts: 3632
Joined: Mon Jun 02, 2003 3:47 pm
Location: Boston, MA
Contact:

Re: How to use data tag and XML document with possible itera

Post by support »

Hi,

If you're able to dynamically inject your C# code into the view (in this case vxml) we would suggest doing something similar to the following.

If you can do things like this in asp.net then this should greatly simplify what you're looking to accomplish. Here's a basic html list where we generate the list items from an array in php:

Code: Select all

<ul>
<? foreach ($items as $item) { ?>
  <li><?= $item ?></li>
<? } ?>
</ul>
If this is possible, we would suggest doing as much of the processing of your initial structure in you high level language and then dynamically building the vxml code from there. Something along the lines of:

fetch the initial structure
iterate through your questions
based on the type of question, dynamically build the vxml code and inject it into your vxml file in the same manner as the example above

vxml is, on a very, very general level, the phone equivalent of html, so if you're familiar with building dynamic html pages in asp then you should be able to do the same in vxml (for the most part).

Using this method, you could fetch your initial question list from your back end service and dynamically build your vxml page, avoiding both the DOM and javascript.

Hopefully this helps, but please let us know if you have any additional questions.

Regards,
Plum Support

Post Reply