コード例 #1
0
 internal MessengerMessage(FB_Message message)
 {
     Text      = message.text;
     ThreadId  = message.thread_id;
     AuthorId  = message.author;
     Timestamp = message.timestamp?.FromUnixEpoch(true) ?? default;
     Id        = message.uid;
     IsRead    = message.is_read;
     IsUnsent  = message.unsent;
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: FiFiIT/Console_Messenger
        private static string PrintMSG(FB_Message msg)
        {
            string restult = string.Empty;

            double timestamp = double.Parse(msg.timestamp);
            var    time      = new System.DateTime(1970, 1, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var    msgTime   = time.AddMilliseconds(timestamp).ToShortTimeString();
            var    from      = Helpers.GetUser(msg.author);

            restult = $"{msgTime} {from}: {msg.text}";

            return(restult);
        }
コード例 #3
0
        protected override DataTemplate SelectTemplateCore(object item)
        {
            FB_Message message = item as FB_Message;

            if (message.is_from_me)
            {
                return(OwnMessageTemplate);
            }
            else
            {
                return(UserMessageTemplate);
            }
        }
コード例 #4
0
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            FrameworkElement elemnt  = container as FrameworkElement;
            FB_Message       message = item as FB_Message;

            if (message.is_from_me)
            {
                return(elemnt.FindResource("OwnMessageTemplate") as DataTemplate);
            }
            else
            {
                return(elemnt.FindResource("UserMessageTemplate") as DataTemplate);
            }
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: FiFiIT/Console_Messenger
        private static void Client_UpdateEvent(object sender, UpdateEventArgs e)
        {
            FB_Message msg = e.Payload as FB_Message;

            if (msg != null)
            {
                string fromGroup = String.Empty;
                if (!msg.thread_id.Equals(CurrentThreadID))
                {
                    fromGroup = $"{Threads.FirstOrDefault(t => t.uid.Equals(msg.thread_id)).name} ";
                }
                ClearCurrentLine();
                Console.WriteLine($"\r{fromGroup}{PrintMSG(msg)}");
                Console.Write(builder.ToString());
            }
        }
コード例 #6
0
 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)
 {
     Console.WriteLine(string.Format("Got new message from {0}: {1}", author_id, message));
     await Task.Yield();
 }
コード例 #7
0
 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)
 {
     UpdateEvent?.Invoke(this, new UpdateEventArgs(UpdateStatus.NEW_MESSAGE, message_object));
     await Task.Yield();
 }
コード例 #8
0
        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();
            }
        }
コード例 #9
0
        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}");
            }
        }
コード例 #10
0
        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)
        {
            Console.WriteLine(string.Format("Got new 1 message from {0}: {1}. Thread_type = {2} ", author_id, message, thread_type.ToString()));
            //Quang viet
            string x = await send(new FB_Message("Đây là tin nhắn tự động từ project mới. Vui lòng không trả lời lại"), author_id, ThreadType.USER);

            await Task.Yield();
        }