Beispiel #1
0
        public static MessageActivity FromJson(SimpleJSON.JSONNode activityJsonRootNode)
        {
            MessageActivity messageActivity = new MessageActivity();

            messageActivity.Id        = activityJsonRootNode[BotJsonProtocol.KeyId];
            messageActivity.Timestamp = Convert.ToDateTime(activityJsonRootNode[BotJsonProtocol.KeyTimestamp]);
            messageActivity.ChannelId = activityJsonRootNode[BotJsonProtocol.KeyChannelId];

            SimpleJSON.JSONNode fromJsonRootNode = activityJsonRootNode[BotJsonProtocol.KeyFrom];

            if (fromJsonRootNode != null)
            {
                messageActivity.FromId   = fromJsonRootNode[BotJsonProtocol.KeyId];
                messageActivity.FromName = fromJsonRootNode[BotJsonProtocol.KeyName];
            }

            SimpleJSON.JSONNode conversationJsonRootNode = activityJsonRootNode[BotJsonProtocol.KeyConversation];

            if (conversationJsonRootNode != null)
            {
                messageActivity.ConversationId = fromJsonRootNode[BotJsonProtocol.KeyId];
            }

            messageActivity.Text      = activityJsonRootNode[BotJsonProtocol.KeyText];
            messageActivity.ReplyToId = activityJsonRootNode[BotJsonProtocol.KeyReplyToId];

            return(messageActivity);
        }
Beispiel #2
0
        /// <summary>
        /// Sends the given message to the given conversation.
        /// </summary>
        /// <param name="conversationId">The conversation ID.</param>
        /// <param name="fromId">The ID of the sender.</param>
        /// <param name="message">The message to sent.</param>
        /// <param name="fromName">The name of the sender (optional).</param>
        /// <returns></returns>
        public IEnumerator SendMessageCoroutine(string conversationId, string fromId, string message, string fromName = null)
        {
            if (string.IsNullOrEmpty(conversationId))
            {
                throw new ArgumentException("Conversation ID cannot be null or empty");
            }

            if (IsInitialized)
            {
                Debug.Log("SendMessageCoroutine: " + conversationId + "; " + message);

                var body = new MessageActivity(fromId, message, DirectLineChannelId, null, fromName).ToJsonString();
                body = body.Replace("\"channelId\": \"directline\", ", "\"locale\": \"en-EN\", ");
                UnityWebRequest webRequest = CreateWebRequest(
                    WebRequestMethods.Post,
                    DirectLineConversationsApiUri
                    + "/" + conversationId
                    + "/" + DirectLineActivitiesApiUriPostfix,
                    body);

                yield return(webRequest.SendWebRequest());

                if (webRequest.isNetworkError)
                {
                    Debug.Log("Web request failed: " + webRequest.error);
                }
                else
                {
                    string responseAsString = webRequest.downloadHandler.text;

                    if (!string.IsNullOrEmpty(responseAsString))
                    {
                        //Debug.Log("Received response:\n" + responseAsString);
                        BotResponseEventArgs eventArgs = CreateBotResponseEventArgs(responseAsString);

                        if (BotResponse != null)
                        {
                            BotResponse.Invoke(this, eventArgs);
                        }
                    }
                    else
                    {
                        Debug.Log("Received an empty response");
                    }
                }
            }
            else
            {
                Debug.Log("Bot Direct Line manager is not initialized");
                yield return(null);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Creates a new BotResponseEventArgs instance based on the given response.
        /// </summary>
        /// <param name="responseAsString"></param>
        /// <returns></returns>
        private BotResponseEventArgs CreateBotResponseEventArgs(string responseAsString)
        {
            if (string.IsNullOrEmpty(responseAsString))
            {
                throw new ArgumentException("Response cannot be null or empty");
            }

            JSONNode             responseJsonRootNode = JSONNode.Parse(responseAsString);
            JSONNode             jsonNode             = null;
            BotResponseEventArgs eventArgs            = new BotResponseEventArgs();

            if ((jsonNode = responseJsonRootNode[BotJsonProtocol.KeyError]) != null)
            {
                eventArgs.EventType = EventTypes.Error;
                eventArgs.Code      = jsonNode[BotJsonProtocol.KeyCode];
                string message = jsonNode[BotJsonProtocol.KeyMessage];

                if (!string.IsNullOrEmpty(message))
                {
                    eventArgs.Message = message;
                }
            }
            else if (responseJsonRootNode[BotJsonProtocol.KeyConversationId] != null)
            {
                eventArgs.EventType      = EventTypes.ConversationStarted;
                eventArgs.ConversationId = responseJsonRootNode[BotJsonProtocol.KeyConversationId];
            }
            else if (responseJsonRootNode[BotJsonProtocol.KeyId] != null)
            {
                eventArgs.EventType     = EventTypes.MessageSent;
                eventArgs.SentMessageId = responseJsonRootNode[BotJsonProtocol.KeyId];
            }
            else if ((jsonNode = responseJsonRootNode[BotJsonProtocol.KeyActivities]) != null)
            {
                eventArgs.EventType = EventTypes.MessageReceived;
                eventArgs.Watermark = responseJsonRootNode[BotJsonProtocol.KeyWatermark];
                JSONArray jsonArray = jsonNode.AsArray;

                foreach (JSONNode activityNode in jsonArray)
                {
                    MessageActivity messageActivity = MessageActivity.FromJson(activityNode);
                    eventArgs.Messages.Add(messageActivity);
                }
            }

            return(eventArgs);
        }