示例#1
0
    /*
     *  OnMessage() callback method
     *  The method is called when a response from the chatbot is received
     */
    private void OnMessage(object resp, Dictionary <string, object> customData)
    {
        Log.Debug("ExampleConversation.OnMessage()", "Conversation: Message Response: {0}", customData["json"].ToString());

        //  Convert resp to fsdata
        fsData   fsdata = null;
        fsResult r      = serializer.TrySerialize(resp.GetType(), resp, out fsdata);

        if (!r.Succeeded)
        {
            throw new WatsonException(r.FormattedMessages);
        }

        //  Convert fsdata to MessageResponse
        MessageResponse messageResponse = new MessageResponse();
        object          obj             = messageResponse;

        r = serializer.TryDeserialize(fsdata, obj.GetType(), ref obj);
        if (!r.Succeeded)
        {
            throw new WatsonException(r.FormattedMessages);
        }

        //  Set context for next round of messaging
        object _tempContext = null;

        (resp as Dictionary <string, object>).TryGetValue("context", out _tempContext);

        if (_tempContext != null)
        {
            contexts = _tempContext as Dictionary <string, object>;
        }
        else
        {
            Log.Debug("ExampleConversation.OnMessage()", "Failed to get context");
        }

        //  Synthesize the message response
        textToSpeech.Synthesize(messageResponse.output.text[0]);
        //  Send the message response to the EnvironmentManager script
        environment.Manage(messageResponse, contexts);
        //  The chatbot has responded
        waitingForResponse = false;
    }