public async Task <MessageResponse> SendMessageAsync(string message, string lastContext = "")
        {
            var storeCredentials = await StoreCredentialsCache.GetStoreAsync(uow, storeId);

            if (storeCredentials == null)
            {
                var logRep = uow.GetRepository <LogRepository>();
                await logRep.RecLog(
                    NopLogLevel.Error,
                    LogMessages.Fmt(LogMessages.STORE_CREDENTIALS_NOT_FOUND, storeId)
                    );

                return(null);
            }

            var conversation = new ConversationService(
                storeCredentials.Username,
                storeCredentials.Password,
                ConversationService.CONVERSATION_VERSION_DATE_2017_05_26
                );

            var response = conversation.Message(storeCredentials.WorkspaceId, new MessageRequest
            {
                Input   = new { text = message },
                Context = string.IsNullOrEmpty(lastContext) ? null : JsonConvert.DeserializeObject(lastContext)
            });

            response.Context = JsonConvert.SerializeObject(response.Context);

            return(response);
        }
Exemplo n.º 2
0
        /// <summary>
        /// This method is called after a customer or agent connects
        /// </summary>
        /// <returns>Async obj</returns>
        public override async Task OnConnectedAsync()
        {
            var currentUserDetails = await CurrentUserDetails();

            var chatConversationHandler = CreateHumanAgentConversation();

            switch (currentUserDetails.Role)
            {
            case NopRoles.LIVECHAT_AGENT:
                var agentHubRules = new AgentHubRules(
                    livechatRules: livechatRules,
                    userTracker: userTracker,
                    groupManager: Groups,
                    clientsManager: Clients,
                    conversationHandler: chatConversationHandler
                    );

                await agentHubRules.OnAgentConnectedAsync(currentUserDetails);

                break;

            case NopRoles.LIVECHAT_USER:

                var storeDetails = await StoreCredentialsCache.GetStoreAsync(
                    uow,
                    currentUserDetails.CustomerStoreId);

                // If store is configured to use a bot as an agent, why should create it accordingly
                if (storeDetails != null && storeDetails.ShouldUseWatson)
                {
                    chatConversationHandler = CreateWatsonConversation();
                }

                var customerHubRules = new CustomerHubRules(
                    livechatRules: livechatRules,
                    context: Context,
                    groupManager: Groups,
                    clientsManager: Clients,
                    conversationHandler: chatConversationHandler
                    );

                await customerHubRules.OnCustomerConnectedAsync(currentUserDetails);

                break;

            default:
                await logRepository.RecLog(
                    NopLogLevel.Error,
                    LogMessages.ON_CONNECTED_NO_RULES);

                break;
            }

            await base.OnConnectedAsync();
        }