Esempio n. 1
0
        /// <summary>
        /// get text message template
        /// </summary>
        /// <param name="text">text</param>
        /// <param name="sender">sender id</param>
        /// <returns>json</returns>
        private async Task <List <JObject> > ConstructAndSendMessage(ConstructType type, CaseDetail caseDetailResult = null, JObject additionalMessage = null, string messageInfo = null)
        {
            // To-do: consider to convert to JSON
            List <JObject> messageList = new List <JObject>();

            if (additionalMessage != null)
            {
                messageList.Add(additionalMessage);
            }

            switch (type)
            {
            case ConstructType.Greeting:
                var greetingOption = new List <QuickReplyOption>
                {
                    new QuickReplyOption {
                        title = REQ_BOT_ASSIST, payload = REQ_BOT_ASSIST
                    },
                    new QuickReplyOption {
                        title = NO_BOT_ASSIST, payload = NO_BOT_ASSIST
                    },
                };

                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"๐Ÿ‘‹ Greeting { _senderInfo.first_name} { _senderInfo.last_name} ๐Ÿ‘‹! {Environment.NewLine}We love having you with us." }
                }));

                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"Should I help you to work with your suppot cases? Here are some option(s). ๐Ÿ•ต", quick_replies = greetingOption }
                }));

                _conversationService.UpsertActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}", new ConversationData {
                    LastQuestionAsked = (int)Question.None, Answered = true
                });


                break;

            case ConstructType.RequestBotAssistance:
                var assistanceOptions = new List <QuickReplyOption>
                {
                    new QuickReplyOption {
                        title = RAISE_TICKET, payload = RAISE_TICKET
                    },
                    new QuickReplyOption {
                        title = TICKET_STATUS, payload = TICKET_STATUS
                    }
                    //new QuickReplyOption { title = JUST_BROWSE, payload = JUST_BROWSE },
                };

                //check any notification pending, if yes user can cancel it too..
                var result = _jiraUserMgmtService.GetUser(_senderInfo.senderConversationId, _company.Id);
                if (result != null && _tickSysNotifService.GetByUser(result.Id) != null)
                {
                    assistanceOptions.Add(new QuickReplyOption {
                        title = CANCEL_NOTIF, payload = CANCEL_NOTIF
                    });
                }

                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"Greeting {_senderInfo.first_name} {_senderInfo.last_name}! I'm TicketBOT! ๐Ÿค“" }
                }));

                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"๐Ÿ‘จ๐Ÿปโ€๐Ÿ”ง How can I help you? Here are some option(s).", quick_replies = assistanceOptions }
                }));

                _conversationService.UpsertActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}", new ConversationData {
                    LastQuestionAsked = (int)Question.None, Answered = true
                });
                break;

            case ConstructType.RequestOperator:
                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"Okay! Please submit your details here, our team will check and get back to you. " }
                }));

                Conversation conversation = new Conversation
                {
                    SenderPageId        = $"{_senderInfo.senderConversationId}~{_company.FbPageId}",
                    ConversationLogType = (int)ConvLogType.MuteLog,
                    ConversationData    = JsonConvert.SerializeObject(new List <ConversationData> {
                        new ConversationData {
                            LastQuestionAsked = (int)Question.None, Answered = true
                        }
                    }),
                };

                _conversationService.RemoveActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}");

                _conversationService.Create(conversation);
                break;

            case ConstructType.Ending:
                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"Thank you! Have a nice day! :)" }
                }));

                _conversationService.RemoveActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}");
                break;

            case ConstructType.CreateTicket:
                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"Okay got it!\nIn which application do you encounter the issue?" }
                }));
                _conversationService.UpsertActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}", new ConversationData {
                    LastQuestionAsked = (int)Question.IssueApplicationName, Answered = false
                });
                break;

            case ConstructType.TicketDescription:
                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"Can you describe the issue?" }
                }));
                _conversationService.UpsertActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}", new ConversationData {
                    LastQuestionAsked = (int)Question.IssueDescription, Answered = false
                });
                break;

            case ConstructType.TicketCreationConfirmation:
                var createCaseConfirmationOption = new List <QuickReplyOption>
                {
                    new QuickReplyOption {
                        title = CASE_SUBMIT_YES, payload = CASE_SUBMIT_YES
                    },
                    new QuickReplyOption {
                        title = CASE_SUBMIT_NO, payload = CASE_SUBMIT_NO
                    },
                };
                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"Summary:\n\nSubject: {caseDetailResult.Subject} \n\nDescription: {caseDetailResult.Detail} \n\nAre you sure you want to submit?", quick_replies = createCaseConfirmationOption }
                }));;
                _conversationService.UpsertActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}", new ConversationData {
                    LastQuestionAsked = (int)Question.IssueDescription, Answered = true
                });
                break;

            case ConstructType.TicketCreated:
                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"All done! Your case has been logged. Please quote {caseDetailResult.CaseKey} to follow up." }
                }));

                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new
                    {
                        attachment = new
                        {
                            type    = "template",
                            payload = new
                            {
                                template_type = "one_time_notif_req",
                                title         = $"Do you want to get notified with {caseDetailResult.CaseKey} update?",
                                payload       = string.Format(FacebookCustomPayload.CASE_GET_NOTIFIED_PAYLOAD, caseDetailResult.CaseKey)
                            }
                        }
                    }
                }));

                _conversationService.RemoveActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}");
                break;

            case ConstructType.CheckTicket:
                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"Sure! Please quote your ticket code." }
                }));
                _conversationService.UpsertActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}", new ConversationData {
                    LastQuestionAsked = (int)Question.TicketCode, Answered = false
                });
                break;

            case ConstructType.TicketFound:
                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"There you go! \n\nTicket Code: {caseDetailResult.CaseKey} \n\nStatus: {caseDetailResult.Status} \n\nCase Subject: {caseDetailResult.Subject} \n\nClick the link below for more. \n{caseDetailResult.WebURL}" }
                }));
                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"Thank you for using TicketBOT! Have a nice day! :)" }
                }));

                // One time notification integration here
                // Check user whether already subscribe
                if (caseDetailResult.Status != JiraServiceDeskStatus.Declined || caseDetailResult.Status != JiraServiceDeskStatus.Completed)
                {
                    messageList.Add(JObject.FromObject(new
                    {
                        recipient = new { id = _senderInfo.senderConversationId },
                        message   = new
                        {
                            attachment = new
                            {
                                type    = "template",
                                payload = new
                                {
                                    template_type = "one_time_notif_req",
                                    title         = $"Do you want to get notified with {caseDetailResult.CaseKey} updates?",
                                    payload       = string.Format(FacebookCustomPayload.CASE_GET_NOTIFIED_PAYLOAD, caseDetailResult.CaseKey)
                                }
                            }
                        }
                    }));
                }

                _conversationService.RemoveActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}");
                break;

            case ConstructType.SearchCompany:
                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"โ˜๐Ÿป Before we get started, I wanna know one thing. Can you tell me your company name please? \nAs our cilent's privacy matters, we need you to enter full company name. \n๐Ÿ˜ฌ For testing enter 'ZTEST COMPANY' ๐Ÿ˜ฌ" }
                }));
                _conversationService.UpsertActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}", new ConversationData {
                    LastQuestionAsked = (int)Question.CompanyName, Answered = false
                });
                break;

            case ConstructType.RequestVerificationCode:
                string testOTPInfo = string.Empty;
                if (!string.IsNullOrWhiteSpace(messageInfo))
                {
                    testOTPInfo = $"\n๐Ÿ˜ฌ For testing you can enter verification code as {messageInfo} ๐Ÿ˜ฌ";
                }

                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"Okay great! We've sent you a verification email which contains a verification code. Can you tell me your verification code please?{testOTPInfo}" }
                }));
                _conversationService.UpsertActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}", new ConversationData {
                    LastQuestionAsked = (int)Question.VerificationCode, Answered = false
                });
                break;

            case ConstructType.Retry:
                var retryOption = new List <QuickReplyOption>
                {
                    new QuickReplyOption {
                        title = RETRY_YES, payload = RETRY_YES
                    },
                    new QuickReplyOption {
                        title = RETRY_NO, payload = RETRY_NO
                    },
                };

                //messageList.Add(JObject.FromObject(new
                //{
                //    recipient = new { id = _senderInfo.senderConversationId },
                //    message = new { text = $"Sorry, I didn't quite catch that. It seems like invalid option/answer." }
                //}));
                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"Do you want to retry?", quick_replies = retryOption }
                }));
                _conversationService.UpsertActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}", new ConversationData {
                    LastQuestionAsked = (int)Question.Retry, Answered = true
                });
                break;

            case ConstructType.NotImplemented:
                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"Im still learning, you can submit ideas to {_company.contactEmail}. ๐Ÿ’ก" }
                }));
                _conversationService.RemoveActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}");
                break;

            case ConstructType.Error:
                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = _senderInfo.senderConversationId },
                    message   = new { text = $"Something went wrong, please try again :(" }
                }));
                _conversationService.RemoveActiveConversation($"{_senderInfo.senderConversationId}~{_company.FbPageId}");
                break;

            case ConstructType.None:
                break;
            }

            foreach (var message in messageList)
            {
                await _fbApiClientService.PostMessageAsync(Utility.ParseDInfo(_company.FbPageToken, _applicationSettings.General.SysInfo), message);
            }
            return(messageList);
        }
Esempio n. 2
0
        public async Task UserOptinCaseNoification(Messaging message, Company company)
        {
            try
            {
                _company = company;

                var jiraCaseKey       = message.optin.payload.Replace(FacebookCustomPayload.CASE_GET_NOTIFIED, string.Empty);
                var oneTimeNotifToken = message.optin.one_time_notif_token;

                var user = _jiraUserMgmtService.GetUser(message.sender.id, _company.Id);
                var ticketUserCompany = _clientCompanyService.GetById(user.ClientCompanyId);

                // Get current ticket status
                CaseDetail caseDetail = null;
                try
                {
                    caseDetail = await _jiraCaseMgmtService.GetCaseStatusAsync(company, ticketUserCompany.TicketSysCompanyCode, jiraCaseKey);
                }
                catch { }

                // Jira no result

                TicketSysNotification ticketSysNotification = new TicketSysNotification
                {
                    TicketSysUserId   = user.Id,
                    OneTimeNotifToken = oneTimeNotifToken,
                    JiraCaseKey       = jiraCaseKey,
                    JiraCaseStatus    = caseDetail != null ? caseDetail.Status : null,
                };

                _userCaseNotifService.Create(ticketSysNotification);

                List <JObject> messageList = new List <JObject>();

                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = message.sender.id },
                    message   = new { text = $"All set! We'll send you a notification when there is an update! ๐Ÿ‘" }
                }));
                messageList.Add(JObject.FromObject(new
                {
                    recipient = new { id = message.sender.id },
                    message   = new { text = $"Thank you for using TicketBOT! Have a nice day! :)" }
                }));

                foreach (var msg in messageList)
                {
                    await _fbApiClientService.PostMessageAsync(Utility.ParseDInfo(_company.FbPageToken, _applicationSettings.General.SysInfo), msg);
                }
            }
            catch (Exception ex)
            {
                var errMsg = JObject.FromObject(new
                {
                    recipient = new { id = message.sender.id },
                    message   = new { text = $"DEBUG --> Error. Check exception" }
                });
                await _fbApiClientService.PostMessageAsync(Utility.ParseDInfo(_company.FbPageToken, _applicationSettings.General.SysInfo), errMsg);

                LoggingHelper.LogError(ex, _logger);
            }
        }