Beispiel #1
0
    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)
        {
            _context = _tempContext as Dictionary <string, object>;
        }
        else
        {
            Log.Debug("ExampleConversation.OnMessage()", "Failed to get context");
        }

        if (resp != null && (messageResponse.intents.Length > 0 || messageResponse.intents.Length > 0))
        {
            Response.text = string.Join(", ", messageResponse.output.text);
            HandleIntents(messageResponse.intents [0]);
            speech.SpeakText(Response.text);
        }
    }