Example #1
0
    private void OnBotResponse(object sender, Assets.BotDirectLine.BotResponseEventArgs e)
    {
        Debug.Log($"OnBotResponse: {e.ToString()}");

        switch (e.EventType)
        {
        case EventTypes.ConversationStarted:
            if (!string.IsNullOrWhiteSpace(e.ConversationId))
            {
                // Store the ID
                textToSpeech.SpeakSsml("<?xml version=\"1.0\"?><speak speed=\"80%\" version=\"1.0\" xmlns=\"http://www.w3.org/2001/10/synthesis\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.w3.org/2001/10/synthesis http://www.w3.org/TR/speech-synthesis/synthesis.xsd\" xml:lang=\"en-US\">Bot connection established!</speak>");
                conversationId = e.ConversationId;
            }
            else
            {
                textToSpeech.SpeakSsml("<?xml version=\"1.0\"?><speak speed=\"80%\" version=\"1.0\" xmlns=\"http://www.w3.org/2001/10/synthesis\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.w3.org/2001/10/synthesis http://www.w3.org/TR/speech-synthesis/synthesis.xsd\" xml:lang=\"en-US\">Error while connecting to Bot!</speak>");
            }
            break;

        case EventTypes.MessageSent:
            if (!string.IsNullOrEmpty(conversationId))
            {
                // Get the bot's response(s)
                BotDirectLineManager.Instance.GetMessagesAsync(conversationId).Wait();
            }

            break;

        case EventTypes.MessageReceived:
            // Handle the received message(s)
            if (!string.IsNullOrWhiteSpace(conversationId))
            {
                var messageActivity = e.Messages.LastOrDefault();
                Debug.Log(messageActivity.Text);
                textToSpeech.SpeakSsml("<?xml version=\"1.0\"?><speak speed=\"80%\" version=\"1.0\" xmlns=\"http://www.w3.org/2001/10/synthesis\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.w3.org/2001/10/synthesis http://www.w3.org/TR/speech-synthesis/synthesis.xsd\" xml:lang=\"en-US\"> " + messageActivity.Text + "</speak>");

                dictationRecognizer.Stop();

                dictationRecognizer.DictationResult     -= DictationRecognizer_DictationResult;
                dictationRecognizer.DictationHypothesis -= DictationRecognizer_DictationHypothesis;
                dictationRecognizer.DictationComplete   -= DictationRecognizer_DictationComplete;
                dictationRecognizer.DictationError      -= DictationRecognizer_DictationError;

                dictationRecognizer.Dispose();

                //do
                //{

                //} while (dictationRecognizer.Status != SpeechSystemStatus.Stopped);

                PhraseRecognitionSystem.Restart();
            }
            break;

        case EventTypes.Error:
            // Handle the error
            break;
        }
    }
Example #2
0
    private void Instance_BotResponse(object sender, Assets.BotDirectLine.BotResponseEventArgs e)
    {
        switch (e.EventType)
        {
        case Assets.BotDirectLine.EventTypes.ConversationStarted:
            _conversationState.ConversationId = e.ConversationId;
            if (!string.IsNullOrEmpty(_conversationState.ConversationId))
            {
                StartCoroutine(BotDirectLineManager.Instance.GetMessagesCoroutine(_conversationState.ConversationId));
            }
            Debug.Log("Conversation Started");
            break;

        case Assets.BotDirectLine.EventTypes.MessageReceived:

            if (e.Watermark != null)
            {
                int messageId = Convert.ToInt32(e.Watermark);

                responseText.text = e.Messages[messageId].Text;

                SpeechManager.Instance.Speech(responseText.text);
                character.StartTalking();
                Debug.Log($"Message Received: {e.Messages[messageId].Text}");
            }
            break;

        case Assets.BotDirectLine.EventTypes.MessageSent:
            if (!string.IsNullOrEmpty(_conversationState.ConversationId))
            {
                StartCoroutine(BotDirectLineManager.Instance.GetMessagesCoroutine(_conversationState.ConversationId));
            }
            Debug.Log("Message Sent");
            break;

        case Assets.BotDirectLine.EventTypes.Error:
            Debug.Log($"Error: {e.Message}");
            break;

        default:
            break;
        }
    }
Example #3
0
    private void OnBotResponse(object sender, Assets.BotDirectLine.BotResponseEventArgs e)
    {
        switch (e.EventType)
        {
        case Assets.BotDirectLine.EventTypes.ConversationStarted:
            // Store the ID
            Debug.Log("Conversation Started.");
            answerReceived = true;
            ConversationId = e.ConversationId;

            StartCoroutine(BotDirectLineManager.Instance.SendMessageCoroutine(
                               ConversationId, "UnityUserId", "Start", "Unity User 1"));
            break;

        case Assets.BotDirectLine.EventTypes.MessageSent:
            Debug.Log("Message sent..");
            if (!string.IsNullOrEmpty(ConversationId))
            {
                // Get the bot's response(s)
                StartCoroutine(BotDirectLineManager.Instance.GetMessagesCoroutine(ConversationId));
            }

            break;

        case Assets.BotDirectLine.EventTypes.MessageReceived:
            // Handle the received message(s)

            //Grab the First Question.
            if (totalResponses < 1)
            {
                StartCoroutine(BotDirectLineManager.Instance.SendMessageCoroutine(
                                   ConversationId, "UnityUserId", "1", "Unity User 1"));
            }

            Debug.Log("Message received");
            totalResponses++;
            Debug.Log("Total responses for far: " + totalResponses);
            if (e.Messages.Count > 1)
            {
                Debug.Log("OnBotResponse: " + e.ToString());

                JSONNode ResObject;
                JSONNode FromObject;
                string   fromId;

                for (int L = e.Messages.Count - 1; L > 0; L--)
                {
                    ResObject = JSON.Parse((e.Messages[L]).ToString());

                    FromObject = JSON.Parse(ResObject["from"].ToString());
                    fromId     = FromObject["id"];
                    Debug.Log("from is: " + fromId);

                    if (fromId == BotId)
                    {
                        Debug.Log("Latest message from  Bot: " + ResObject["text"]);
                        setQuestionNameAndAnswers(ResObject["text"]);
                        break;
                    }
                }
            }
            else
            {
                // StartCoroutine(BotDirectLineManager.Instance.SendMessageCoroutine(
                //     ConversationId, "UnityUserId", "Start", "Unity User 1"));
            }

            break;

        case Assets.BotDirectLine.EventTypes.Error:
            // Handle the error
            break;
        }
    }