Exemplo n.º 1
0
        public bool Message(OnMessage callback, string workspaceID, string input, bool useAlternateIntents, string conversationID = default(string), string customData = default(string))
        {
            if (string.IsNullOrEmpty(workspaceID))
            {
                throw new ArgumentNullException("workspaceId");
            }
            if (string.IsNullOrEmpty(input))
            {
                throw new ArgumentNullException("input");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            MessageRequest messageRequest = new MessageRequest();

            messageRequest.inputText         = input;
            messageRequest.alternate_intents = useAlternateIntents;
            if (conversationID != default(string))
            {
                messageRequest.conversationID = conversationID;
            }

            RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_MESSAGE);

            if (connector == null)
            {
                return(false);
            }

            fsData data;

            sm_Serializer.TrySerialize(messageRequest.GetType(), messageRequest, out data).AssertSuccessWithoutWarnings();
            string reqString = fsJsonPrinter.CompressedJson(data);

            MessageReq req = new MessageReq();

            req.Callback                = callback;
            req.MessageRequest          = messageRequest;
            req.Headers["Content-Type"] = "application/json";
            req.Headers["Accept"]       = "application/json";
            req.Parameters["version"]   = Version.VERSION;
            req.Function                = "/" + workspaceID + "/message";
            req.Data       = customData;
            req.Send       = Encoding.UTF8.GetBytes(reqString);
            req.OnResponse = MessageResp;

            return(connector.Send(req));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Message the specified workspaceId, input and callback.
        /// </summary>
        /// <param name="successCallback">The success callback.</param>
        /// <param name="failCallback">The fail callback.</param>
        /// <param name="workspaceID">Workspace identifier.</param>
        /// <param name="messageRequest">Message request object.</param>
        /// <param name="customData">Custom data.</param>
        /// <returns></returns>
        public bool Message(SuccessCallback <object> successCallback, FailCallback failCallback, string workspaceID, MessageRequest messageRequest, Dictionary <string, object> customData = null)
        {
            if (string.IsNullOrEmpty(workspaceID))
            {
                throw new ArgumentNullException("workspaceId");
            }
            if (successCallback == null)
            {
                throw new ArgumentNullException("successCallback");
            }
            if (failCallback == null)
            {
                throw new ArgumentNullException("failCallback");
            }

            RESTConnector connector = RESTConnector.GetConnector(Credentials, Workspaces);

            if (connector == null)
            {
                return(false);
            }

            IDictionary <string, string> requestDict = new Dictionary <string, string>();

            if (messageRequest.context != null)
            {
                requestDict.Add("context", Json.Serialize(messageRequest.context));
            }
            if (messageRequest.input != null)
            {
                requestDict.Add("input", Json.Serialize(messageRequest.input));
            }
            requestDict.Add("alternate_intents", Json.Serialize(messageRequest.alternate_intents));
            if (messageRequest.entities != null)
            {
                requestDict.Add("entities", Json.Serialize(messageRequest.entities));
            }
            if (messageRequest.intents != null)
            {
                requestDict.Add("intents", Json.Serialize(messageRequest.intents));
            }
            if (messageRequest.output != null)
            {
                requestDict.Add("output", Json.Serialize(messageRequest.output));
            }

            int           iterator      = 0;
            StringBuilder stringBuilder = new StringBuilder("{");

            foreach (KeyValuePair <string, string> property in requestDict)
            {
                string delimeter = iterator < requestDict.Count - 1 ? "," : "";
                stringBuilder.Append(string.Format("\"{0}\": {1}{2}", property.Key, property.Value, delimeter));
                iterator++;
            }
            stringBuilder.Append("}");

            string stringToSend = stringBuilder.ToString();

            MessageReq req = new MessageReq();

            req.SuccessCallback         = successCallback;
            req.FailCallback            = failCallback;
            req.Headers["Content-Type"] = "application/json";
            req.Headers["Accept"]       = "application/json";
            req.Parameters["version"]   = VersionDate;
            req.Function   = "/" + workspaceID + "/message";
            req.Send       = Encoding.UTF8.GetBytes(stringToSend);
            req.OnResponse = MessageResp;
            req.CustomData = customData == null ? new Dictionary <string, object>() : customData;
            if (req.CustomData.ContainsKey(Constants.String.CUSTOM_REQUEST_HEADERS))
            {
                foreach (KeyValuePair <string, string> kvp in req.CustomData[Constants.String.CUSTOM_REQUEST_HEADERS] as Dictionary <string, string> )
                {
                    req.Headers.Add(kvp.Key, kvp.Value);
                }
            }

            return(connector.Send(req));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Message the specified workspaceId, input and callback.
        /// </summary>
        /// <param name="callback">Callback.</param>
        /// <param name="workspaceID">Workspace identifier.</param>
        /// <param name="messageRequest">Message request object.</param>
        /// <param name="customData">Custom data.</param>
        /// <returns></returns>
        public bool Message(OnMessage callback, string workspaceID, MessageRequest messageRequest, string customData = default(string))
        {
            if (string.IsNullOrEmpty(workspaceID))
            {
                throw new ArgumentNullException("workspaceId");
            }
            if (string.IsNullOrEmpty(messageRequest.input["text"] as string))
            {
                throw new ArgumentNullException("messageRequest.input.text");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            RESTConnector connector = RESTConnector.GetConnector(Credentials, Workspaces);

            if (connector == null)
            {
                return(false);
            }

            IDictionary <string, string> requestDict = new Dictionary <string, string>();

            if (messageRequest.context != null)
            {
                requestDict.Add("context", Json.Serialize(messageRequest.context));
            }
            if (messageRequest.input != null)
            {
                requestDict.Add("input", Json.Serialize(messageRequest.input));
            }
            requestDict.Add("alternate_intents", Json.Serialize(messageRequest.alternate_intents));
            if (messageRequest.entities != null)
            {
                requestDict.Add("entities", Json.Serialize(messageRequest.entities));
            }
            if (messageRequest.intents != null)
            {
                requestDict.Add("intents", Json.Serialize(messageRequest.intents));
            }
            if (messageRequest.output != null)
            {
                requestDict.Add("output", Json.Serialize(messageRequest.output));
            }

            int           iterator      = 0;
            StringBuilder stringBuilder = new StringBuilder("{");

            foreach (KeyValuePair <string, string> property in requestDict)
            {
                string delimeter = iterator < requestDict.Count - 1 ? "," : "";
                stringBuilder.Append(string.Format("\"{0}\": {1}{2}", property.Key, property.Value, delimeter));
                iterator++;
            }
            stringBuilder.Append("}");

            string stringToSend = stringBuilder.ToString();

            MessageReq req = new MessageReq();

            req.Callback                = callback;
            req.MessageRequest          = messageRequest;
            req.Headers["Content-Type"] = "application/json";
            req.Headers["Accept"]       = "application/json";
            req.Parameters["version"]   = VersionDate;
            req.Function                = "/" + workspaceID + "/message";
            req.Data       = customData;
            req.Send       = Encoding.UTF8.GetBytes(stringToSend);
            req.OnResponse = MessageResp;

            return(connector.Send(req));
        }