Exemple #1
0
        public async Task <IActionResult> Create([Bind("MMCId,MustContainWord")] MessageMustContain messageMustContain)
        {
            if (ModelState.IsValid)
            {
                int id      = int.Parse(HttpContext.Session.GetString("id"));
                var channel = _context.Channels.Where(c => c.ChannelId == id).FirstOrDefault();
                _context.Entry(channel).Collection(c => c.MustContainWords).Load();
                channel.MustContainWords.Add(messageMustContain);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Edit", "TelegramChannels", new { id = id }));
            }
            return(View(messageMustContain));
        }
Exemple #2
0
        public async Task <IActionResult> Create([Bind("MRWId,Word,ReplaceTo")] MessageReplaceWord messageReplaceWord)
        {
            if (ModelState.IsValid)
            {
                int id      = int.Parse(HttpContext.Session.GetString("id"));
                var channel = _context.Channels.Where(c => c.ChannelId == id).FirstOrDefault();
                _context.Entry(channel).Collection(c => c.ReplaceWords).Load();
                channel.ReplaceWords.Add(messageReplaceWord);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Edit", "TelegramChannels", new { id = id }));
            }
            return(BadRequest());
        }
        // GET: TelegramChannels/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var telegramChannel = await _context.Channels.FindAsync(id);

            _context.Entry(telegramChannel).Collection(c => c.ReplaceWords).Load();
            _context.Entry(telegramChannel).Collection(c => c.MustContainWords).Load();
            _context.Entry(telegramChannel).Collection(c => c.MustnotContainFilters).Load();

            if (telegramChannel == null)
            {
                return(NotFound());
            }

            return(View(telegramChannel));
        }
Exemple #4
0
        //Collector Unit...
        public async void GetNewses()
        {
            PublishedNewsExpirer expirer = new PublishedNewsExpirer(5, _Context);



            Settings settings;

            while (true)
            {
                expirer.Expirer();


                if (mustwait)
                {
                    mustwait = false;
                    Thread.Sleep(mustwaittilme);
                }

                if (!mustwait)
                {
                    Thread.Sleep(25000);
                    //Program.keepconnected_timer.Stop();
                    var dialogs = new TLDialogs();
                    try
                    {
                        if (mustwait)
                        {
                            continue;
                        }

                        //Geting dialogs from Telegram Servers
                        dialogs = await client.GetUserDialogsAsync() as TLDialogs
                        ;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        continue;
                    }

                    //Program.keepconnected_timer.Start();
                    settings = _Context.Settings.AsNoTracking().FirstOrDefault();

                    var channels = _Context.Channels.ToList();


                    try
                    {
                        foreach (var channel in channels)
                        {
                            var publishednewses             = _Context.PublishedNewses.AsNoTracking();
                            var RepetitiousNewstextDetector = new RepetitiousNewsTextDetector(publishednewses, 47);

                            IEnumerable <TLMessage> messages = null;
                            try
                            {
                                messages = TelegramClientManager
                                           .GetChannelUnreadmessages(channel.ChannelUserName, dialogs)
                                           .Result;
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                                continue;
                            }

                            if (messages == null)
                            {
                                continue;
                            }

                            var photomessages = messages.Where(m => m.Media != null && m.Media is TLMessageMediaPhoto)
                                                .Select(m => m.Media).OfType <TLMessageMediaPhoto>().ToList();
                            var textmessages = messages
                                               .Where(m => m.Media == null) /*.Where("")* for ensure message in not forwarded*/
                                               .Select(m => m.Message).ToList();

                            messages = null;

                            //Filters

                            _Context.Entry(channel).Collection(channel => channel.MustContainWords).Load();



                            #region MustCuntainFiltering

                            if (channel.MustContainWords.Count > 0)
                            {
                                List <string> filtered_textmessages = new List <string>();
                                foreach (var textmmesage in textmessages)
                                {
                                    foreach (var word in channel.MustContainWords)
                                    {
                                        if (textmmesage.Contains(word.MustContainWord))
                                        {
                                            filtered_textmessages.Add(textmmesage);
                                            break;
                                        }
                                    }
                                }

                                textmessages = filtered_textmessages.Distinct().ToList();

                                List <TLMessageMediaPhoto> filtered_photomessages = new List <TLMessageMediaPhoto>();
                                foreach (var photomessage in photomessages)
                                {
                                    //var photoMedia = (TLMessageMediaPhoto)photomessage.Media;
                                    foreach (var word in channel.MustContainWords)
                                    {
                                        if (photomessage.Caption.Contains(word.MustContainWord))
                                        {
                                            filtered_photomessages.Add(photomessage);
                                            break;
                                        }
                                    }
                                }

                                photomessages = filtered_photomessages.Distinct().ToList();
                            }

                            #endregion

                            #region MustNotContainFilter


                            if (channel.MustnotContainFilters != null)
                            {
                                foreach (var textmessage in textmessages)
                                {
                                    foreach (var mustnotcontain in channel.MustnotContainFilters)
                                    {
                                        if (textmessage.Contains(mustnotcontain.MustnotContainWord))
                                        {
                                            textmessages.Remove(textmessage);
                                        }
                                    }
                                }

                                foreach (var photomessage in photomessages)
                                {
                                    foreach (var mustnotcontain in channel.MustnotContainFilters)
                                    {
                                        if (photomessage.Caption.Contains(mustnotcontain.MustnotContainWord))
                                        {
                                            photomessages.Remove(photomessage);
                                        }
                                    }
                                }
                            }

                            #endregion



                            #region Removing Link&Tag on photos



                            foreach (var photo in photomessages)
                            {
                                photo.Caption = photo.Caption.Replace("💸", "");
                                photo.Caption = photo.Caption.Replace("➖", "");
                                photo.Caption = photo.Caption.Replace("🆔", "");
                                photo.Caption = photo.Caption.Replace("👇🏼", "");
                                photo.Caption = photo.Caption = Regex.Replace(photo.Caption, "</?(a|A).*?>", "");

                                var splitedwordlist = photo.Caption.Split(' ').ToList();

                                for (int i = 0; i < splitedwordlist.Count; i++)
                                {
                                    //removing instagram taglinks
                                    if (splitedwordlist[i].Contains("@"))
                                    {
                                        if (splitedwordlist[i].Contains("\n"))
                                        {
                                            var linksplitedwordlist = splitedwordlist[i].Split('\n').ToList();
                                            splitedwordlist[i] = null;
                                            foreach (var li in linksplitedwordlist.ToList())
                                            {
                                                if (li.Contains("@") || li == "")
                                                {
                                                    linksplitedwordlist.Remove(li);
                                                }
                                            }

                                            string filteredword = null;
                                            foreach (var li in linksplitedwordlist)
                                            {
                                                filteredword += li + " ";
                                            }

                                            if (filteredword == null)
                                            {
                                                splitedwordlist.Remove(splitedwordlist[i]);
                                                continue;
                                            }

                                            splitedwordlist[i] = filteredword;
                                        }
                                        else
                                        {
                                            splitedwordlist.Remove(splitedwordlist[i]);
                                        }
                                    }

                                    if (!(i < splitedwordlist.Count))
                                    {
                                        break;
                                    }

                                    if (splitedwordlist[i].Contains("://") || splitedwordlist[i].Contains(".com") ||
                                        splitedwordlist[i].Contains(".ir") || splitedwordlist[i].Contains(".org") ||
                                        splitedwordlist[i].Contains(".net") || splitedwordlist[i].Contains(".ai"))
                                    {
                                        if (splitedwordlist[i].Contains("\n"))
                                        {
                                            var linksplitedwordlist = splitedwordlist[i].Split('\n').ToList();
                                            splitedwordlist[i] = null;
                                            foreach (var li in linksplitedwordlist.ToList())
                                            {
                                                if (li.Contains("://") || li.Contains(".me") || li.Contains(".ir") ||
                                                    li.Contains(".com") || li.Contains(".org") || li.Contains(".net") ||
                                                    li == "" || li.Contains(".ai") || li.Contains("💸") || li.Contains("👇🏼") || li.Contains("➖") || li.Contains("🆔"))
                                                {
                                                    linksplitedwordlist.Remove(li);
                                                }
                                            }

                                            string filteredword = null;
                                            foreach (var li in linksplitedwordlist)
                                            {
                                                filteredword += li + " ";
                                            }

                                            if (filteredword == null)
                                            {
                                                splitedwordlist.Remove(splitedwordlist[i]);
                                                continue;
                                            }

                                            splitedwordlist[i] = filteredword;
                                        }
                                        else
                                        {
                                            var linksplitedwordlist = splitedwordlist[i].Split(' ').ToList();
                                            splitedwordlist[i] = null;
                                            foreach (var li in linksplitedwordlist.ToList())
                                            {
                                                if (li.Contains("://") || li.Contains(".me") || li.Contains(".ir") || li.Contains(".ai") || li.Contains("💸") || li.Contains("👇🏼") || li.Contains("➖") || li.Contains("🆔"))
                                                {
                                                    linksplitedwordlist.Remove(li);
                                                }
                                            }

                                            string filteredword = null;
                                            foreach (var li in linksplitedwordlist)
                                            {
                                                filteredword += li + " ";
                                            }

                                            splitedwordlist[i] = filteredword;
                                        }
                                    }
                                    if (!(i < splitedwordlist.Count))
                                    {
                                        break;
                                    }
                                    if (splitedwordlist[i].Contains("www."))
                                    {
                                        if (!(i < splitedwordlist.Count))
                                        {
                                            break;
                                        }

                                        if (splitedwordlist[i].Contains("\n"))
                                        {
                                            var linksplitedwordlist = splitedwordlist[i].Split('\n').ToList();
                                            splitedwordlist[i] = null;
                                            foreach (var li in linksplitedwordlist.ToList())
                                            {
                                                if (!(i < splitedwordlist.Count) || li == null)
                                                {
                                                    break;
                                                }
                                                if (li.Contains("www."))
                                                {
                                                    linksplitedwordlist.Remove(li);
                                                }
                                            }

                                            foreach (var li in linksplitedwordlist)
                                            {
                                                splitedwordlist[i] += li + " ";
                                            }
                                        }
                                        else
                                        {
                                            splitedwordlist.Remove(splitedwordlist[i]);
                                        }
                                    }
                                }

                                string filteredcaption = null;
                                foreach (var split in splitedwordlist.ToList())
                                {
                                    filteredcaption += split + " ";
                                }

                                Regex.Replace(photo.Caption, "</?(a|A).*?>", "");

                                photo.Caption = filteredcaption;
                            }

                            #endregion


                            #region Removing Link&Tag on textMessages


                            for (int j = 0; j < textmessages.Count; j++)
                            {
                                textmessages[j] = Regex.Replace(textmessages[j], "</?(a|A).*?>", "");

                                var splitedwordlist = textmessages[j].Split(' ').ToList();



                                for (int i = 0; i < splitedwordlist.Count; i++)
                                {
                                    if (!(i < splitedwordlist.Count))
                                    {
                                        break;
                                    }
                                    //removing telegram taglinks
                                    if (splitedwordlist[i].Contains("@"))
                                    {
                                        if (splitedwordlist[i].Contains("\n"))
                                        {
                                            var linksplitedwordlist = splitedwordlist[i].Split('\n').ToList();
                                            splitedwordlist[i] = null;
                                            foreach (var li in linksplitedwordlist.ToList())
                                            {
                                                if (li.Contains("@") || li == "")
                                                {
                                                    linksplitedwordlist.Remove(li);
                                                }
                                            }

                                            string filteredword = null;
                                            foreach (var li in linksplitedwordlist)
                                            {
                                                filteredword += li + " ";
                                            }

                                            if (filteredword == null)
                                            {
                                                splitedwordlist.Remove(splitedwordlist[i]);
                                                continue;
                                            }

                                            splitedwordlist[i] = filteredword;
                                        }
                                        else
                                        {
                                            splitedwordlist.Remove(splitedwordlist[i]);
                                        }
                                    }

                                    if (i >= splitedwordlist.Count)
                                    {
                                        i--;
                                    }

                                    if (splitedwordlist[i].Contains("://") || splitedwordlist[i].Contains(".com") ||
                                        splitedwordlist[i].Contains(".ir") || splitedwordlist[i].Contains(".org") ||
                                        splitedwordlist[i].Contains(".net") || splitedwordlist[i].Contains(".ai") ||
                                        splitedwordlist[i].Contains("💸") || splitedwordlist[i].Contains("👇🏼") ||
                                        splitedwordlist[i].Contains("➖") || splitedwordlist[i].Contains("🆔"))
                                    {
                                        if (splitedwordlist[i].Contains("\n"))
                                        {
                                            var linksplitedwordlist = splitedwordlist[i].Split('\n').ToList();
                                            splitedwordlist[i] = null;
                                            foreach (var li in linksplitedwordlist.ToList())
                                            {
                                                if (li.Contains("://") || li.Contains(".me") || li.Contains(".ir") ||
                                                    li.Contains(".com") || li.Contains(".org") || li.Contains(".ai") ||
                                                    li.Contains(".net") ||
                                                    li == "" || li.Contains("💸") || li.Contains("👇🏼") ||
                                                    li.Contains("➖") || li.Contains("🆔"))
                                                {
                                                    linksplitedwordlist.Remove(li);
                                                }
                                            }

                                            string filteredword = null;
                                            foreach (var li in linksplitedwordlist)
                                            {
                                                filteredword += li + " ";
                                            }

                                            if (filteredword == null)
                                            {
                                                splitedwordlist.Remove(splitedwordlist[i]);
                                                continue;
                                            }

                                            splitedwordlist[i] = filteredword;
                                        }
                                        else
                                        {
                                            var linksplitedwordlist = splitedwordlist[i].Split(' ').ToList();
                                            splitedwordlist[i] = null;
                                            foreach (var li in linksplitedwordlist.ToList())
                                            {
                                                if (li.Contains("://") || li.Contains(".me") || li.Contains(".ir") ||
                                                    li == "" || li.Contains(".ai"))
                                                {
                                                    linksplitedwordlist.Remove(li);
                                                }
                                            }

                                            string filteredword = null;
                                            foreach (var li in linksplitedwordlist)
                                            {
                                                filteredword += li + " ";
                                            }

                                            splitedwordlist[i] = filteredword;
                                        }
                                    }


                                    if (splitedwordlist[i].Contains("www."))
                                    {
                                        if (splitedwordlist[i].Contains("\n"))
                                        {
                                            var linksplitedwordlist = splitedwordlist[i].Split('\n').ToList();
                                            splitedwordlist[i] = null;
                                            foreach (var li in linksplitedwordlist.ToList())
                                            {
                                                if (li.Contains("www."))
                                                {
                                                    linksplitedwordlist.Remove(li);
                                                }
                                            }

                                            foreach (var li in linksplitedwordlist)
                                            {
                                                splitedwordlist[i] += li + " ";
                                            }
                                        }
                                        else
                                        {
                                            splitedwordlist.Remove(splitedwordlist[i]);
                                        }
                                    }
                                }

                                string filteredcaption = null;
                                foreach (var split in splitedwordlist.ToList())
                                {
                                    filteredcaption += split + " ";
                                }


                                textmessages[j] = filteredcaption;
                            }


                            #endregion



                            _Context.Entry(channel).Collection(channel => channel.ReplaceWords).Load();


                            #region WordReplacement Filter on photos

                            foreach (var photo in photomessages)
                            {
                                photo.Caption = photo.Caption.Replace("➖", "");


                                foreach (var replaceWord in channel.ReplaceWords)
                                {
                                    photo.Caption = photo.Caption.Replace(replaceWord.Word, replaceWord.ReplaceTo);
                                }



                                var splitedwordlist = photo.Caption.Split(' ').ToList();
                                for (int i = 0; i < splitedwordlist.Count; i++)
                                {
                                    foreach (var replaceWord in channel.ReplaceWords)
                                    {
                                        if (splitedwordlist[i] == replaceWord.Word)
                                        {
                                            splitedwordlist[i] = replaceWord.ReplaceTo;
                                        }
                                    }
                                }

                                string filteredcaption = null;
                                foreach (var split in splitedwordlist.ToList())
                                {
                                    filteredcaption += split + " ";
                                }


                                photo.Caption = filteredcaption;
                            }


                            #endregion


                            #region WordReplacement Filter on textMessages

                            for (int i = 0; i < textmessages.Count; i++)
                            {
                                textmessages[i] = textmessages[i].Replace("➖", "");

                                foreach (var replaceWord in channel.ReplaceWords)
                                {
                                    textmessages[i] = textmessages[i].Replace(replaceWord.Word, replaceWord.ReplaceTo);
                                }
                            }



                            for (int j = 0; j < textmessages.Count; j++)
                            {
                                var splitedwordlist = textmessages[j].Split(' ').ToList();
                                for (int i = 0; i < splitedwordlist.Count; i++)
                                {
                                    foreach (var replaceWord in channel.ReplaceWords)
                                    {
                                        if (splitedwordlist[i] == replaceWord.Word)
                                        {
                                            splitedwordlist[i] = replaceWord.ReplaceTo;
                                        }
                                    }
                                }

                                string filteredcaption = null;
                                foreach (var split in splitedwordlist.ToList())
                                {
                                    filteredcaption += split + " ";
                                }


                                textmessages[j] = filteredcaption;
                            }

                            #endregion

                            if (mustwait)
                            {
                                mustwait = false;
                                Thread.Sleep(mustwaittilme);
                            }

                            #region Sending Text Messages to the Destination

                            //for test



                            foreach (var message in textmessages)
                            {
                                var news = new News()
                                {
                                    //Todo: Conjunction word must remove

                                    TextMessage = message,

                                    DateAdded = DateTime.Now
                                };
                                var textRepetitousresult =
                                    RepetitiousNewstextDetector.IsNewstextRepetitous(news);
                                if (textRepetitousresult)
                                {
                                    continue; //Avoid sending
                                }



                                _Bot.SendTextMessage(settings.StartDescription + "\n" + message + "\n" + "\n" +
                                                     settings.EndDescription);
                                //Calling marker
                                MarkasPublished(news);
                                Thread.Sleep(2560);
                            }


                            #endregion

                            if (mustwait)
                            {
                                mustwait = false;
                                Thread.Sleep(mustwaittilme);
                            }

                            #region Sending Photo Messages to the Destination



                            foreach (var photo in photomessages)
                            {
                                byte[] media = null;
                                try
                                {
                                    //Downloading media content
                                    media = TelegramClientManager.DownloadPhotoFile((TLPhoto)photo.Photo).Result;
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(e);
                                    Thread.Sleep(10000);
                                    continue;
                                }

                                var news = new News()
                                {
                                    //Todo: Cunjunction word must remove

                                    TextMessage = photo.Caption != null ? photo.Caption : null,

                                    Mediahash = media != null?hasher.Getfilehash(media) : null,

                                                    DateAdded = DateTime.Now
                                };

                                #region RepetituousNewsFilter

                                //Check if photo of current news already published , remove that

                                var mediaresult =
                                    RepetitiousNewsMediaDetector.IsmediaRepetitious(hasher.Getfilehash(media),
                                                                                    publishednewses);



                                if (mediaresult == true)
                                {
                                    continue; //Avoid sending
                                    Console.WriteLine("***********************REPE media NEWS********************");
                                }



                                //Text repetitious detector
                                var textRepetitousresult =
                                    RepetitiousNewstextDetector.IsNewstextRepetitous(news);
                                if (textRepetitousresult)
                                {
                                    continue; //Avoid sending
                                    Console.WriteLine("***********************REPE text NEWS********************");
                                }



                                #endregion



                                Thread.Sleep(5000);
                                var mediafilestream = new MemoryStream(media);

                                //Mark as a published news


                                //Calling marker
                                MarkasPublished(news);



                                _Bot.SendPhotoMessage(mediafilestream,
                                                      settings.StartDescription + "\n" + photo.Caption + "\n" + "\n" +
                                                      settings.EndDescription);
                                Thread.Sleep(1500);
                            }

                            #endregion
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }
        }