コード例 #1
0
        public async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> argument)
        {
            var activity = await argument as Activity;

            await PrivacyPolicyAsync(context);

            User user = await GetUser(context, argument);

            Order         order           = StorageManager.Instance.Get(user.RowKey);
            var           customerName    = user.PartitionKey;
            bool          IsSubscriped    = order != null;
            string        messageRecieved = activity.Text.ToLower();
            List <string> welcomeMessages = new List <string>()
            {
                "hi", "hello", "welcome", "dear"
            };
            var match = welcomeMessages.FirstOrDefault(stringToCheck => stringToCheck.Contains(messageRecieved));

            if (messageRecieved.Equals("privacy"))
            {
                await context.PostAsync($"Hi {customerName} kindly check our cosnumer terms and privacy policy \r\nif you continue to use our service you agree and bind to them");

                await context.PostAsync($"Privacy policy : http://uscisbot2017.azurewebsites.net/privacy/PrivacyPolicy.txt");

                await context.PostAsync($"Consumer Terms : http://uscisbot2017.azurewebsites.net/privacy/ConsumerTerms.txt");
            }
            else if (messageRecieved.Contains("help") || messageRecieved.Equals("?"))
            {
                await SendWelcomeMessage(context, customerName);

                await context.PostAsync($"simply type the following:");

                await context.PostAsync($"status for [name] cases [list of cases separated by ;] ");

                await context.PostAsync($"example : status for {customerName} cases 1234;1234");

                await context.PostAsync($"avilable commands : list , clear , status , help, privacy");
            }
            else if (messageRecieved.Equals("clear"))
            {
                user.Cases = null;
                StorageManager.Instance.Update(user);
                await context.PostAsync("cleared all tracking data !");
            }
            else if (messageRecieved.Equals("list"))
            {
                if (!string.IsNullOrWhiteSpace(user.Cases))
                {
                    string decrypt = AESCrypto.DecryptText(user.Cases, user.RowKey);
                    var    dic     = ToDictionary(decrypt);
                    foreach (var item in dic)
                    {
                        await context.PostAsync(item.Key + " :");

                        string[] arr = item.Value.Split(new string[] { "$$$" }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var r in arr)
                        {
                            await context.PostAsync(r);
                        }
                    }
                    //don't show offer if subscriped
                    if (!IsSubscriped)
                    {
                        await this.PaymentMessageAsync(context, argument);
                    }
                }
                else
                {
                    await context.PostAsync("nothing there ! , type help for more details");
                }
            }
            else if (messageRecieved.Contains("status") || messageRecieved.Equals("check"))
            {
                Dictionary <string, string> dic = new Dictionary <string, string>();
                if (!string.IsNullOrWhiteSpace(user.Cases))
                {
                    dic = ToDictionary(AESCrypto.DecryptText(user.Cases, user.RowKey));
                }

                List <string> statuses = new List <string>();
                if (messageRecieved.Equals("check"))
                {
                    statuses = new List <string>(dic.Keys);
                }
                else
                {
                    statuses.Add(messageRecieved);
                }
                foreach (var status in statuses)
                {
                    string[] arr = status.Split(new string[] { "cases" }, StringSplitOptions.RemoveEmptyEntries);
                    if (arr.Length < 2)
                    {
                        await context.PostAsync($"try this : status for {customerName} cases 1234;1234");

                        return;
                    }
                    string        name      = arr[0].Replace("status for", "").Trim();
                    string        cases     = arr[1].Trim();
                    List <string> casesList = cases.Split(';').ToList();

                    List <string> casesResultList = new List <string>();
                    string        casesResult     = string.Empty;

                    foreach (var c in casesList)
                    {
                        string uscisstatus, summary = string.Empty;
                        USCIS.UscisService.GetCaseStatus(c, out uscisstatus, out summary);
                        casesResult += $"{uscisstatus}$$${summary}$$$";
                        casesResultList.Add($"{uscisstatus}\r\n{summary}");
                    }

                    if (dic.ContainsKey(status))
                    {
                        string dicResult = dic[status];
                        if (casesResult.Equals(dicResult))
                        {
                            await context.PostAsync($"{status} \r\n No new updates for your cases , will keep an eye on it !");

                            continue;
                        }
                        else
                        {
                            dic[status] = casesResult;
                        }
                    }
                    else
                    {
                        dic.Add(status, casesResult);
                    }

                    user.Cases = AESCrypto.EncryptText(ToString(dic), user.RowKey);

                    // send result
                    await context.PostAsync($"{name} status :");

                    foreach (var c in casesResultList)
                    {
                        await context.PostAsync(c);
                    }

                    await context.PostAsync($"I will keep an eye for {name} with cases {cases} and update you if there are any changes." + (IsSubscriped ? "" : " , if you buy our offer !"));

                    //don't show offer if subscriped
                    if (!IsSubscriped)
                    {
                        await this.PaymentMessageAsync(context, argument);
                    }

                    StorageManager.Instance.Update(user);
                }
            }
            else if (match != null)
            {
                await SendWelcomeMessage(context, customerName);

                await context.PostAsync($"what I can do for you today ? type help for more info.");
            }
            else// chat mode
            {
                string result = QnAService.GetAnswer(messageRecieved);
                if (result.Equals("No good match found in the KB"))
                {
                    result = "Still building my knowledge ask questions related to immigration , will troll with you once I became 1 year old";
                }

                await context.PostAsync(result);
            }
        }