Exemplo n.º 1
0
        private string GetActivitiesFromBot(DirectLineCommunicator botComm, string convoId)
        {
            var i = 0;

            while (i++ < 10)
            {
                HttpResponseMessage response = botComm.GetActivities(convoId);
                string errorText             = null;
                switch (response.StatusCode)
                {
                case HttpStatusCode.OK:
                    break;

                case HttpStatusCode.Forbidden:
                    errorText = "error: invalid secret or token";
                    break;

                case HttpStatusCode.Unauthorized:
                    errorText = "error: invalid authorization header";
                    break;

                case HttpStatusCode.NotFound:
                    errorText = "error: object not found";
                    break;

                case HttpStatusCode.InternalServerError:
                    errorText = "error: internal server error";
                    break;

                case HttpStatusCode.BadGateway:
                    errorText = "error: bot unavailable or returned an error";
                    break;

                case HttpStatusCode.BadRequest:
                    errorText = "error: Bad Request";
                    break;

                default:
                    errorText = string.Format("error: response code {0}", response.StatusCode);
                    break;
                }
                if (errorText != null)
                {
                    // presently, just returns that there was an error. Any more detail is not really
                    // necessary in speech interface, but the switch statement above really helps for debugging
                    return("error");
                }

                object responseActivity =
                    JsonConvert.DeserializeObject <GetActivity>(response.Content.ReadAsStringAsync().Result);

                if (((GetActivity)responseActivity).activities[((GetActivity)responseActivity).activities.Length - 1].id != convoId)
                {
                    return(((GetActivity)responseActivity).activities[((GetActivity)responseActivity).activities.Length - 1].text);
                }
                Task.Delay(500);
            }
            return(null);
        }
Exemplo n.º 2
0
        private async void ResultGenerated(SpeechRecognitionResult args)
        {
            try
            {
                if (args.Confidence == SpeechRecognitionConfidence.Medium ||
                    args.Confidence == SpeechRecognitionConfidence.High)
                {
                    await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        StatusBlock.Text      = "Result Generated";
                        DictationTextBox.Text = "Heard you say:\n'" + args.Text + "'";
                    });

                    var botComm = new DirectLineCommunicator();

                    if (StartConvo || convoId == null)
                    {
                        convoId    = StartNewConversation(botComm);
                        StartConvo = false;
                    }
                    if (convoId == null)
                    {
                        SpeakText("Sorry, I'm having difficulty initiating connection to the bot.");
                        return;
                    }
                    bool?activitySent = SendActivityToBot(botComm, convoId, args.Text);
                    var  responseText = "";
                    switch (activitySent)
                    {
                    case false:
                        SpeakText("Sorry, I'm having difficulty speaking to the bot.");
                        return;

                    case true:
                        responseText = GetActivitiesFromBot(botComm, convoId);
                        break;

                    case null:
                    default:
                        return;
                    }
                    SpeakText(responseText ?? "hmmm... I'm not getting any response from the bot.");
                }
                else
                {
                    SpeakText("I'm not sure I got that. Would you mind asking again?");
                }
            }
            catch (Exception e)
            {
                await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    StatusBlock.Text      = string.Format("Exception Thrown: {0}", e.Message);
                    DictationTextBox.Text = string.Format("inner exception: {0}", e.InnerException);
                });

                throw;
            }
        }
Exemplo n.º 3
0
        private bool?SendActivityToBot(DirectLineCommunicator botComm, string convoId, string text)
        {
            HttpResponseMessage sentActivity = botComm.SendActivity(text, convoId);

            string errorText = null;

            switch (sentActivity.StatusCode)
            {
            case HttpStatusCode.NoContent:
            case HttpStatusCode.OK:
                break;

            case HttpStatusCode.Forbidden:
                errorText = "error: invalid secret or token";
                break;

            case HttpStatusCode.Unauthorized:
                if (retry > 2)
                {
                    errorText = "error: invalid authorization header";
                    retry     = 0;
                }
                else
                {
                    //In the event post comes in unauthorized, attempt to
                    //create a new convo and try again (max retry 2)
                    this.convoId = StartNewConversation(botComm);
                    SendActivityToBot(botComm, this.convoId, text);
                    return(null);
                }
                break;

            case HttpStatusCode.NotFound:
                errorText = "error: object not found";
                break;

            case HttpStatusCode.InternalServerError:
                errorText = "error: internal server error";
                break;

            case HttpStatusCode.BadGateway:
                errorText = "error: bot unavailable or returned an error";
                break;

            case HttpStatusCode.BadRequest:
                errorText = "error: Bad Request";
                break;

            default:
                errorText = string.Format("error: response code {0}", sentActivity.StatusCode);
                break;
            }
            // presently, just returns that there was an error. Any more detail is not really
            // necessary in speech interface, but the switch statement above really helps for debugging
            return(errorText == null);
        }
Exemplo n.º 4
0
        private string StartNewConversation(DirectLineCommunicator botComm)
        {
            HttpResponseMessage newConversation = botComm.StartConversation();
            object responseObject =
                JsonConvert.DeserializeObject <StartConvo>(newConversation.Content.ReadAsStringAsync().Result);

            string         convoId      = ((StartConvo)responseObject).conversationId;
            HttpStatusCode responseCode = newConversation.StatusCode;

            string errorText = null;

            switch (responseCode)
            {
            case HttpStatusCode.Created:
            case HttpStatusCode.OK:
                break;

            case HttpStatusCode.Forbidden:
                errorText = "error: invalid secret or token";
                break;

            case HttpStatusCode.Unauthorized:
                errorText = "error: invalid authorization header";
                break;

            case HttpStatusCode.Conflict:
                errorText = "error: object already exists";
                break;

            case HttpStatusCode.NotFound:
                errorText = "error: Not Found";
                break;

            default:
                errorText = string.Format("error: unexpected response code {0}", responseCode);
                break;
            }
            // presently, just returns that there was an error. Any more detail is not really
            // necessary in speech interface, but the switch statement above really helps for debugging
            return(errorText == null ? convoId : null);
        }