protected override async Task onMessage(string mid = null, string author_id = null, string message = null, FB_Message message_object = null, string thread_id = null, ThreadType?thread_type = null, long ts = 0, JToken metadata = null, JToken msg = null)
        {
            //authorId: Nguoi Gui
            //threaId: Nguoi Nhan
            try
            {
                if (thread_type == ThreadType.ROOM || thread_type == ThreadType.GROUP)
                {
                    return;
                }
                IFirebaseClient client = FirebaseHelper.SetFirebaseClientForChat();
                //Check Remove blockall
                if (message.ToLower() == "removestopall")
                {
                    await client.DeleteAsync("ListBlockUser/" + thread_id);

                    return;
                }

                if (author_id == GetUserUid())
                {
                    await client.SetAsync("ListBlockUser/" + GetUserUid(), new MessageFirsebase
                    {
                        Id       = GetUserUid(),
                        BlockAll = true
                    });

                    return;
                }

                //Check ListBlockUser
                FirebaseResponse firebaseResponse = await client.GetAsync("ListBlockUser");

                Dictionary <string, MessageFirsebase> listBlockUser = JsonConvert.DeserializeObject <Dictionary <string, MessageFirsebase> >(firebaseResponse.Body);
                var isBlockedAll = listBlockUser != null && listBlockUser.Values.Any(x => x.BlockAll == true && x.Id == thread_id);
                if (isBlockedAll)
                {
                    return;
                }


                var firebaseGet = await client.GetAsync("ListUser/" + author_id);

                if (firebaseGet == null)
                {
                    if (!await CheckUserInListSimsimi(client, thread_id))
                    {
                        await Add5MinuteUser(client, author_id, author_id, thread_id, message, false);
                    }
                }
                else
                {
                    //Thơi gian hien tai < Thoi gian Block
                    var response = firebaseGet.ResultAs <MessageFirsebase>();
                    if (DateTime.UtcNow < response?.BlockUntil)
                    {
                        if (message.ToLower() == "stopall")
                        {
                            await client.SetAsync("ListBlockUser/" + thread_id, new MessageFirsebase
                            {
                                Id       = thread_id,
                                BlockAll = true
                            });

                            return;
                        }
                        else if (message.ToLower() == "stophour")
                        {
                            await client.SetAsync("ListUser/" + thread_id, new MessageFirsebase
                            {
                                BlockUntil = DateTime.UtcNow.AddHours(1)
                            });

                            return;
                        }
                        else if (message.ToLower() == "trolyao")
                        {
                            await client.SetAsync("ListSimsimiUser/" + thread_id, new MessageFirsebase
                            {
                                Id = thread_id
                            });
                            await send(new FB_Message { text = "Chào bạn. Mình là Trợ lý ảo của Quang.\nNhững tin nhắn này được trả lời tự động. Mục đích vui là chính :D" }, author_id, ThreadType.USER);
                        }
                        if (await CheckUserInListSimsimi(client, thread_id))
                        {
                            string answer = await SimsimiHelper.SendSimsimi(message);

                            string text = "Trợ lý ảo:\n" + answer;
                            await send(new FB_Message { text = text }, author_id, ThreadType.USER);
                        }
                    }
                    else
                    {
                        if (message.ToLower() == "stopall")
                        {
                            await client.SetAsync("ListBlockUser/" + thread_id, new MessageFirsebase
                            {
                                Id       = thread_id,
                                BlockAll = true
                            });

                            return;
                        }
                        else if (message.ToLower() == "stophour")
                        {
                            await client.SetAsync("ListUser/" + thread_id, new MessageFirsebase
                            {
                                Id         = thread_id,
                                BlockUntil = DateTime.UtcNow.AddHours(1)
                            });

                            return;
                        }
                        else if (message.ToLower() == "trolyao")
                        {
                            await client.SetAsync("ListSimsimiUser/" + thread_id, new MessageFirsebase
                            {
                                Id = thread_id
                            });
                            await send(new FB_Message { text = "Chào bạn. Mình là Trợ lý ảo của Quang.\nNhững tin nhắn này được trả lời tự động. Mục đích vui là chính :D" }, author_id, ThreadType.USER);
                        }


                        //Co the send message o day
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine(string.Format($"{DateTime.Now}: Got new 1 message from author_id = {author_id}: {message}. Thread_type = {thread_type.ToString()} \nthread_id={thread_id}"));
                        if (author_id != GetUserUid())
                        {
                            Console.ForegroundColor = ConsoleColor.Blue;
                            if (await CheckUserInListSimsimi(client, thread_id))
                            {
                                string answer = await SimsimiHelper.SendSimsimi(message);

                                string text = "Trợ lý ảo:\n" + answer;
                                await send(new FB_Message { text = text }, author_id, ThreadType.USER);
                            }
                            else
                            {
                                string text = "Đây là trợ lý ảo của Quang.\nHiện tại Quang không đang online nên không thể trả lời bạn ngay được.\nTrong khi chờ đợi, các bạn có thể gõ tin nhắn \"trolyao\" để trao đổi với trợ lý ảo";

                                await send(new FB_Message { text = text }, author_id, ThreadType.USER);
                            }



                            #region Ít dùng
                            //using (FileStream stream = File.OpenRead(@"the girl with katana.jpg"))
                            //{
                            //    await sendLocalFiles(
                            //        file_paths: new Dictionary<string, Stream>() { { @"the girl with katana.jpg", stream } },
                            //        message: null,
                            //        thread_id: author_id,
                            //        thread_type: ThreadType.USER);
                            //}
                            //await send(new FB_Message { text = "Đáp lại tin nhắn----" }, author_id, ThreadType.USER);
                            //await send(new FB_Message { text = await Covid19Helper.GetDetail() }, author_id, ThreadType.USER);

                            #endregion
                        }

                        ///////////////////
                        if (!await CheckUserInListSimsimi(client, thread_id))
                        {
                            await Add5MinuteUser(client, author_id, author_id, thread_id, message, false);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"{DateTime.Now}: Có lỗi xảy ra. Exception = {e.Message}");
            }
        }
        protected override async Task onMessage(string mid = null, string author_id = null, string message = null, FB_Message message_object = null, string thread_id = null, ThreadType?thread_type = null, long ts = 0, JToken metadata = null, JToken msg = null)
        {
            //authorId: người gửi
            //thread_id: thread hiện tại
            var userId = this.GetUserUid();

            if (userId != author_id)
            {
                var specialMessage = await FacebookToolHelper.CheckSpecialMessage(message, thread_id);

                switch (specialMessage)
                {
                case Facebook.DAL.Enum.EnumFeature.NoSpecialFeature:
                case Facebook.DAL.Enum.EnumFeature.StopAll:
                case Facebook.DAL.Enum.EnumFeature.RemoveStopAll:
                case Facebook.DAL.Enum.EnumFeature.Stop5Min:
                    return;

                case Facebook.DAL.Enum.EnumFeature.TroLyAo:
                    await this.send(new FB_Message
                    {
                        text = ListHelper <string> .GetRandomItemInListObject(Constant.ListConfirmAgreeUseTroLyAoMessage)
                    }, thread_id, ThreadType.USER);

                    return;

                case Facebook.DAL.Enum.EnumFeature.GirlXinh:
                    string fileNameWithExtension = DownloadHelper.DownloadImageFromUrl(DownloadHelper.RandomImageIdGirl());
                    using (FileStream stream = File.OpenRead(fileNameWithExtension))
                    {
                        await sendLocalFiles(
                            file_paths : new Dictionary <string, Stream>()
                        {
                            { fileNameWithExtension, stream }
                        },
                            message : null,
                            thread_id : author_id,
                            thread_type : ThreadType.USER);
                    }
                    return;

                case Facebook.DAL.Enum.EnumFeature.TruyenCuoi:
                    List <string> listTruyenCuoi = await FacebookToolHelper.GetListTruyenCuoi();

                    await this.send(new FB_Message
                    {
                        text = ListHelper <string> .GetRandomItemInListObject(listTruyenCuoi)
                    }, thread_id, ThreadType.USER);

                    return;

                case Facebook.DAL.Enum.EnumFeature.TinTuc:
                    List <NewfeedRss> newfeedRsses = RSSHelper.GetTinMoiNhat();
                    foreach (var newfeed in newfeedRsses)
                    {
                        await this.send(new FB_Message
                        {
                            text = $"{ newfeed.Title} \n{newfeed.Link}"
                        }, thread_id, ThreadType.USER);
                    }
                    return;

                case Facebook.DAL.Enum.EnumFeature.NoiTu:
                    await FacebookToolHelper.SetUserNoiTu(author_id);

                    await this.send(new FB_Message
                    {
                        text = $"Bắt đầu chơi nối từ tiếng việt thôi!!!"
                    }, thread_id, ThreadType.USER);

                    return;

                case Facebook.DAL.Enum.EnumFeature.StopNoiTu:
                    await FacebookToolHelper.SetUserStopNoiTu(author_id);

                    await this.send(new FB_Message
                    {
                        text = $"Đã dừng chơi nối từ tiếng việt!"
                    }, thread_id, ThreadType.USER);

                    return;

                case Facebook.DAL.Enum.EnumFeature.NoiTuTiengAnh:
                    await FacebookToolHelper.SetUserNoiTuTiengAnh(author_id);

                    await this.send(new FB_Message
                    {
                        text = $"Bắt đầu chơi nối từ tiếng Anh thôi!!!"
                    }, thread_id, ThreadType.USER);

                    return;

                case Facebook.DAL.Enum.EnumFeature.StopNoiTuTiengAnh:
                    await FacebookToolHelper.SetUserStopNoiTuTiengAnh(author_id);

                    await this.send(new FB_Message
                    {
                        text = $"Đã dừng chơi nối từ tiếng Anh!"
                    }, thread_id, ThreadType.USER);

                    return;

                default:
                    break;
                }
                ConsoleLogHelper.WriteToConsole($"Got new message from {author_id}: {message}");
                var isBlock = await FacebookToolHelper.CheckIsBlockOrNot(author_id);

                if (isBlock)
                {
                    return;
                }
                else
                {
                    //Co the send message o day
                    var agreeSimsimi = await FacebookToolHelper.CheckUserAgreeSimsimi(thread_id);

                    if (await FacebookToolHelper.CheckIsNoiTuTiengVietOrNot(thread_id))
                    {
                        var    dic         = TuDienHelper.GenerateVietNameseDictionary();
                        var    lastWord    = message.LastWord();
                        var    listNewWord = dic.Keys.Where(x => String.Equals(x.FirstWord(), lastWord, StringComparison.OrdinalIgnoreCase) && x.Contains(" ")).ToList();
                        string textSend    = listNewWord.Count == 0 ? "Chịu thua rồi, ko tìm thấy từ nào cả :((" : $"Nối từ: {ListHelper<string>.GetRandomItemInListObject(listNewWord)}";
                        await this.send(new FB_Message
                        {
                            text = textSend
                        }, thread_id, ThreadType.USER);

                        return;
                    }
                    else if (await FacebookToolHelper.CheckIsNoiTuTiengAnhOrNot(thread_id))
                    {
                        var    dic         = TuDienHelper.GenerateVietNameseDictionary();
                        var    lastWord    = message.LastWord();
                        var    listNewWord = dic.Keys.Where(x => String.Equals(x.FirstWord(), lastWord, StringComparison.OrdinalIgnoreCase) && x.Contains(" ")).ToList();
                        string textSend    = listNewWord.Count == 0 ? "Chịu thua rồi, ko tìm thấy từ nào cả :((" : $"Nối từ: {ListHelper<string>.GetRandomItemInListObject(listNewWord)}";
                        await this.send(new FB_Message
                        {
                            text = textSend
                        }, thread_id, ThreadType.USER);

                        return;
                    }
                    else if (!agreeSimsimi)
                    {
                        await this.send(new FB_Message
                        {
                            text = ListHelper <string> .GetRandomItemInListObject(Constant.ListTroLyAoMessage)
                        }, thread_id, ThreadType.USER);

                        await FacebookToolHelper.AddUser10Min(thread_id);
                    }
                    else
                    {
                        string simsimiMessage = await SimsimiHelper.SendSimsimi(message);

                        await this.send(new FB_Message
                        {
                            text = "Trợ lý ảo: " + simsimiMessage
                        }, thread_id, ThreadType.USER);

                        ConsoleLogHelper.WriteToConsole($"Send message = {simsimiMessage} to {thread_id}");
                    }
                }
            }
            else
            {
                //Tự mình gửi đi
                var specialMessage = await FacebookToolHelper.CheckSpecialMessage(message, thread_id);

                switch (specialMessage)
                {
                case EnumFeature.NoiTu:
                    await FacebookToolHelper.SetUserNoiTu(thread_id);

                    break;

                case EnumFeature.StopNoiTu:
                    await FacebookToolHelper.SetUserStopNoiTu(thread_id);

                    break;
                }
                await Task.Yield();
            }
        }
示例#3
0
 static async void TestFunction()
 {
     //string result = await Covid19Helper.GetDetail();
     string result = await SimsimiHelper.SendSimsimi("Xin chào bạn, Làm quen nhé");
 }