示例#1
0
        // Here we loop through the different options and create a text
        private static string buildOptionsText(string Caller, IDictionary <int, string> nextOptions)
        {
            StringBuilder responseText;

            responseText = new StringBuilder("Please select one of the following options. ");
            responseText.AppendLine();
            foreach (var option in nextOptions)
            {
                // Press x [to option].
                responseText.Append("Press ").Append(option.Key).Append(" ").Append(option.Value).Append(". ").AppendLine();
            }
            // we end the message by saying: press star to confirm
            responseText.AppendLine("Press star to confirm");

            DebugWriter.Write(Caller, "Sending options\r\n" + responseText.ToString() + "\r\n");
            return(responseText.ToString());
        }
示例#2
0
        public static IDictionary <int, string> GetNextSteps(string callerId, string digit, out string goodbyeStatement)
        {
            goodbyeStatement = null;
            // Here we read the external variable 'UserOptions' from the workflow instance
            // This is a string with multiple lines that we will use to return the different options
            string optionsVariable = "{http://schemas.microsoft.com/workflow/2012/xaml/activities/externalvariable}UserOptions";
            string goodbyeVariable = "{http://schemas.microsoft.com/workflow/2012/xaml/activities/externalvariable}GoodbyeStatement";

            if (_workflows.ContainsKey(callerId))
            {
                string existingWorkflowId = _workflows[callerId];
                // We load the instance for this caller from the workflow manager
                var wfInstance = WFClient.Instances.Get(_workflowName, existingWorkflowId);
                if (wfInstance != null)
                {
                    if (wfInstance.WorkflowStatus == Microsoft.Activities.WorkflowInstanceStatus.Started)
                    {
                        // The workflow is still started, so we know we have to look for the next user options
                        if (wfInstance.MappedVariables.ContainsKey(optionsVariable))
                        {
                            var options = wfInstance.MappedVariables[optionsVariable];
                            return(parseOptions(options));
                        }
                    }
                    else
                    {
                        // Since the workflow is finished, the call should finish and we look for the Goodbye message variable
                        if (wfInstance.MappedVariables.ContainsKey(goodbyeVariable))
                        {
                            goodbyeStatement = wfInstance.MappedVariables[goodbyeVariable];
                            DebugWriter.Write(callerId, "Returning no options, but saying goodbye message: " + goodbyeStatement);
                        }
                        // Removing workflow from cache, because he's finished
                        _workflows.Remove(callerId);
                        // We return null (no next steps, but our goodbye statement (output variable) has been assigned
                        return(null);
                    }
                }
            }
            return(new Dictionary <int, string> {
                { 0, "No state found" }
            });
        }
示例#3
0
        /// <summary>
        /// This operation will be called by Twilio , passing in the different parameters
        /// GET api/flight
        /// </summary>
        /// <param name="Caller">The phone number that is calling our Twilio number</param>
        /// <param name="Digits">The digits that were entered by the caller (not provided on the first call)</param>
        /// <returns></returns>
        public TwilioResponse Get([FromUri] string Caller = null, [FromUri] string Digits = null)
        {
            // If caller is unknown > reject
            // Here you could also block phone numbers from calling, by returning a reject!
            if (Caller == null)
            {
                return(new TwilioResponse {
                    Reject = new TwilioReject {
                        Reason = "rejected"
                    }
                });
            }


            // Check if there is an active call process for this phone number
            if (!StateMachineInterpreter.HasActiveWorkflow(Caller))
            {
                DebugWriter.Write(Caller, "Starting new workflow");
                StateMachineInterpreter.StartNewWorkflow(Caller);
            }
            else
            {
                DebugWriter.Write(Caller, "Sending input to existing workflow");
                StateMachineInterpreter.SendInputToExistingWorkflow(Caller, Digits);
            }

            // I don't like doing this, but I will anyway - taking the time to have the published event being handled
            Thread.Sleep(450);

            // At this step, we have an active workflow, linked with the Caller.
            // Now get the next input
            string goodbyeStatement = null;

            // We get the next steps.  Null means no next steps and say goodbye
            var nextOptions = StateMachineInterpreter.GetNextSteps(Caller, Digits, out goodbyeStatement);

            return(getTwilioResponse(Caller, nextOptions, Digits == null, goodbyeStatement));
        }