public ChatbaseRequest GetChatbaseBodyRequest(CBBoxContent bodyContentModel, ChatbaseRequest cbRequest = null, string type = "", string message = "", bool notHandled = false, string intent = "")
        {
            if (cbRequest == null)
            {
                cbRequest = new ChatbaseRequest();
            }

            var bodyContent = new CBBoxContent()
            {
                ApiKey    = bodyContentModel.ApiKey,
                Platform  = bodyContentModel.Platform,
                TimeStamp = bodyContentModel.TimeStamp,
                UserId    = bodyContentModel.UserId,
                Version   = bodyContentModel.Version,
            };

            bodyContent.Type       = (type == "") ? "agent" : type;
            bodyContent.Message    = (message == "") ? "{{input.content}}" : message;
            bodyContent.Intent     = (intent == "") ? "{{input.intent.name}}" : intent;
            bodyContent.NotHandled = (notHandled) ? notHandled : false;

            cbRequest.Messages.Add(bodyContent);

            return(cbRequest);
        }
        public CustomAction GetUserChatbaseCustomAction(ChatbaseRequest cbRequest)
        {
            var action = new CustomAction()
            {
                Type     = "ProcessHttp",
                Title    = "Chatbase Event Tracking - User",
                Invalid  = false,
                Settings = new CustomActionSettings()
                {
                    Headers = { },
                    Method  = "POST",
                    Body    = JsonConvert.SerializeObject(cbRequest).Replace("\"{{calendar.unixTimeMilliseconds}}\"", "{{calendar.unixTimeMilliseconds}}"),
                    Uri     = "{{config.chatbaseUrl}}",
                    ResponseStatusVariable = "chatbaseResponseStatus",
                    ResponseBodyVariable   = "chatbaseResponseBody"
                }
            };

            return(action);
        }
Beispiel #3
0
        public void InsertChatbaseRequests()
        {
            Console.WriteLine("What is the FULL PATH of the exported .json file?");
            var path = Console.ReadLine();


            while (!Path.GetExtension(path).Equals(".json"))
            {
                Console.Beep();
                Console.Write("File is not a JSON. Retry? (Y/N): ");

                var answer = Console.ReadLine();
                if (answer.ToUpper().Equals("Y"))
                {
                    path = Console.ReadLine();
                }
                else
                {
                    break;
                }
            }
            if (Path.GetExtension(path).Equals(".json"))
            {
                try
                {
                    var builderFlowJson = GetBuilderFlow(path);
                    var flow            = _flowFactory.Build(builderFlowJson);
                    var cbRequestModel  = new CBBoxContent();

                    Console.WriteLine("These are the properties from Chatbase requests body:\n");
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine("\t\tapi_key:    {{config.chatbaseApiKey}}");
                    Console.WriteLine("\t\tuser_id:    {{contact.identity}}");
                    Console.WriteLine("\t\ttime_stamp: {{calendar.unixTimeMilliseconds}}");
                    Console.WriteLine("\t\tplatform:   {{config.chatbasePlatform}}");
                    Console.WriteLine("\t\tversion:    {{config.chatbaseVersion}}");
                    Console.ResetColor();


                    Console.Write("\nDo you want to configure it? (Y/N): ");
                    var choice = Console.ReadLine();
                    if (choice.ToUpper().Equals("Y"))
                    {
                        cbRequestModel = _chatbaseExtension.EditChatbaseProperties();
                    }

                    var agentMessages = new ChatbaseRequest();
                    var agentTag      = new Tag()
                    {
                        Background          = "#ffc700",
                        CanChangeBackground = false,
                        Id    = "blip-tag-" + Guid.NewGuid().ToString(),
                        Label = "Chatbase (Bot)",
                    };

                    var userTag = new Tag()
                    {
                        Background          = "#a88300",
                        CanChangeBackground = false,
                        Id    = "blip-tag-" + Guid.NewGuid().ToString(),
                        Label = "Chatbase (Usuario)",
                    };

                    foreach (var box in flow.Boxes)
                    {
                        foreach (var item in box.Content)
                        {
                            agentMessages.Messages.Clear();
                            foreach (var customAction in item.Value.ContentActions)
                            {
                                if (customAction.Action != null)
                                {
                                    if (customAction.Action.Settings.Type != ChatState.MIME_TYPE)
                                    {
                                        var agentMessage = "";
                                        if (customAction.Action.Type != "SendMessage")
                                        {
                                            agentMessage = customAction.Action.Settings.RawContent;
                                        }
                                        else
                                        {
                                            switch (customAction.Action.Settings.Type)
                                            {
                                            case Select.MIME_TYPE:
                                                agentMessage = _limeAdapterExtension.QuickReplyToString(customAction.Action.Settings.Content);
                                                break;

                                            case MediaLink.MIME_TYPE:
                                                agentMessage = _limeAdapterExtension.MediaLinkToString(customAction.Action.Settings.Content);
                                                break;

                                            case Lime.Messaging.Contents.Input.MIME_TYPE:
                                                agentMessage = _limeAdapterExtension.LocationInputToString(customAction.Action.Settings.Content);
                                                break;

                                            case WebLink.MIME_TYPE:
                                                agentMessage = _limeAdapterExtension.WebLinkToString(customAction.Action.Settings.Content);
                                                break;

                                            case "application/vnd.lime.collection+json":
                                                agentMessage = _limeAdapterExtension.DocumentCollectionToString(customAction.Action.Settings.Content);
                                                break;

                                            default:
                                                agentMessage = customAction.Action.CardContent.Document.Content.ToString();
                                                break;
                                            }
                                        }
                                        agentMessages = _chatbaseExtension.GetAgentBodyRequest(cbRequestModel, agentMessages, message: agentMessage);
                                    }
                                }
                                else if (customAction.Input != null)
                                {
                                    if (customAction.Input.Bypass == false)
                                    {
                                        var cbRequestBody = _chatbaseExtension.GetChatbaseBodyRequest(cbRequestModel, type: "user", message: "{{input.content}}");
                                        item.Value.LeavingCustomActions.Add(_chatbaseExtension.GetUserChatbaseCustomAction(cbRequestBody));
                                        if (item.Value.Tags == null)
                                        {
                                            item.Value.Tags = new List <Tag>();
                                        }
                                        item.Value.Tags.Add(userTag);
                                    }
                                }
                            }
                            if (agentMessages.Messages.Count > 0)
                            {
                                item.Value.EnteringCustomActions.Add(_chatbaseExtension.GetAgentChatbaseCustomAction(agentMessages));
                                if (item.Value.Tags == null)
                                {
                                    item.Value.Tags = new List <Tag>();
                                }
                                if (!item.Value.Tags.Contains(agentTag))
                                {
                                    item.Value.Tags.Add(agentTag);
                                }
                            }
                        }
                    }

                    flow.ParseProxyIntoFlow();

                    var serialized = string.Empty;
                    foreach (var box in flow.Boxes)
                    {
                        var piece = JsonConvert.SerializeObject(box.Content);
                        serialized = serialized + piece.Substring(1, piece.Length - 2) + ",";
                    }

                    serialized = "{" + serialized.Substring(0, serialized.Length - 1) + "}";
                    var exitName = path.Replace(".json", "_chatbase.json");
                    File.WriteAllText(exitName, serialized);
                    Console.WriteLine($"File saved with Path {exitName}");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }