Пример #1
0
        static void DecoratorExample()
        {
            var emailSender = new EmailSender();

            emailSender.Send("*****@*****.**", "Hello world!");

            var skypeSender = new SkypeSender(emailSender)
            {
                NickName = "Jack"
            };

            skypeSender.Send("*****@*****.**", "Hello world!");

            var facebookSender = new FacebookSender(skypeSender);

            facebookSender.Send("*****@*****.**", "Hello world!");
        }
Пример #2
0
        public async Task DispatchAgent(Messaging incomingMessage, Company company)
        {
            try
            {
                //read user name here. Return null if user not found
                _senderInfo = await _fbApiClientService.GetUserInfoAsync(Utility.ParseDInfo(company.FbPageToken, _applicationSettings.General.SysInfo), incomingMessage.sender.id);

                _senderInfo.senderConversationId = incomingMessage.sender.id;
                _company = company;

                // INIT.1.0 Consider Redis before proceed A.1.0
                // INIT.1.1 Check any active conversation (Past x mins) To break the conversation loop

                // Check whether user temporarily mute Bot service
                var mutedConvLog = _conversationService.GetActiveConversation($"{_senderInfo.senderConversationId}~{company.FbPageId}", ConvLogType.MuteLog);
                if (mutedConvLog != null)
                {
                    // Extend TTL (Time-To-Live)
                    mutedConvLog.ModifiedOn = DateTime.Now;
                    _conversationService.Update(mutedConvLog.Id, mutedConvLog);

                    // No need reply anything
                    return;
                }

                //TODO: Email all muted conversation to the company email.

                // If no active conversation, send greeting
                if (_conversationService.GetActiveConversation($"{_senderInfo.senderConversationId}~{company.FbPageId}") == null)
                {
                    // A.1.0
                    // Send greeting first
                    // E.g.: Hi there, how can I help you? + Quick Reply Button(Raise a ticket / Check ticket status)
                    await ConstructAndSendMessage(ConstructType.Greeting);
                }
                else
                {
                    // If no quick reply button associated
                    // Check db for previous conversation SELECT TOP 1 * FROM Conversation WHERE CompanyId = X AND FbSenderId = X AND ModifiedOn DESC
                    // If record found --> extract Conversation.Answered
                    // var lastQuestion = _conversationService.GetLastQuestion(_company.Id, _senderInfo.id);
                    var lastQuestion = _conversationService.LastConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}");

                    if (lastQuestion != null)
                    {
                        if (!lastQuestion.Answered)
                        {
                            // If retry option = true
                            // Delete retry option from cache and roll back to conversation before retry

                            // If answer not fullfilled, ask/ask again
                            // Extract Conversation.LastQuestionAsked
                            // extract message from payload. Validate input
                            await ValidateQnA(lastQuestion, incomingMessage.message.text);
                        }
                        else
                        {
                            // If answer fullfilled, prepare next question

                            // Check Payload
                            // Is there quick reply buttons associated
                            // If quick reply button associated: To-do
                            var anyQuickReply = CheckQuickReplyPayload(incomingMessage);
                            if (!string.IsNullOrEmpty(anyQuickReply))
                            {
                                // Conversation.LastQuestionAsked, Conversation.AnswerFreeText
                                switch (anyQuickReply)
                                {
                                case RAISE_TICKET:
                                    await PrepareRaiseTicket();

                                    break;

                                case TICKET_STATUS:
                                    await PrepareCheckTicketStatus();

                                    break;

                                case RETRY_YES:
                                    _conversationService.RemoveActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}");
                                    await ConstructAndSendMessage(ConstructType.RequestBotAssistance);

                                    break;

                                case RETRY_NO:
                                    await ConstructAndSendMessage(ConstructType.Ending);

                                    break;

                                case CASE_SUBMIT_YES:
                                    await CreateJiraTicket();

                                    break;

                                case CASE_SUBMIT_NO:
                                    var reply = JObject.FromObject(new
                                    {
                                        recipient = new { id = _senderInfo.senderConversationId },
                                        message   = new { text = $"Alright, No worries 👍🏽" }
                                    });

                                    await ConstructAndSendMessage(ConstructType.Ending, additionalMessage : reply);

                                    break;

                                case JUST_BROWSE:
                                    await ConstructAndSendMessage(ConstructType.Ending);

                                    break;

                                case CANCEL_NOTIF:
                                    await UserUnsubscribeNotification();

                                    break;

                                case REQ_BOT_ASSIST:
                                    await ConstructAndSendMessage(ConstructType.RequestBotAssistance);

                                    break;

                                case NO_BOT_ASSIST:
                                    await ConstructAndSendMessage(ConstructType.RequestOperator);

                                    break;

                                default:
                                    // prompt: Apologize due to not recognize selected option. Send relevant / available option again
                                    // await ConstructAndSendMessage(ConstructType.NotImplemented);
                                    break;
                                }
                            }
                            else
                            {
                                await ConstructAndSendMessage(ConstructType.NotImplemented);
                            }
                        }
                    }
                    else
                    {
                        await ConstructAndSendMessage(ConstructType.Greeting);
                    }
                }
            }
            catch (Exception ex)
            {
                await ConstructAndSendMessage(ConstructType.Error);

                LoggingHelper.LogError(ex, _logger);
            }
        }