Exemple #1
0
        private static async Task ForceCheckInviteLinksAsync(TelegramBotAbstract sender, MessageEventArgs e)
        {
            int?n = null;

            try
            {
                n = await InviteLinks.FillMissingLinksIntoDB_Async(sender, e);
            }
            catch (Exception e2)
            {
                await NotifyUtil.NotifyOwners(new ExceptionNumbered(e2), sender);
            }

            if (n == null)
            {
                return;
            }

            var text2 = new Language(new Dictionary <string, string>
            {
                { "en", "I have updated n=" + n + " links" },
                { "it", "Ho aggiornato n=" + n + " link" }
            });
            await SendMessage.SendMessageInPrivate(sender, e.Message.From.Id,
                                                   e.Message.From.LanguageCode,
                                                   e.Message.From.Username, text2,
                                                   ParseMode.Default,
                                                   e.Message.MessageId);
        }
Exemple #2
0
        private static async Task <MessageSentResult> TestTime(TelegramBotAbstract sender, MessageEventArgs e)
        {
            var sentDate = await DateTimeClass.AskDateAsync(e.Message.From.Id,
                                                            e.Message.Text,
                                                            e.Message.From.LanguageCode, sender, e.Message.From.Username);

            if (sentDate.Item2 != null)
            {
                await NotifyUtil.NotifyOwners(new ExceptionNumbered(sentDate.Item2), sender, 0, sentDate.Item3);

                return(null);
            }

            var sentDate2 = sentDate.Item1.GetDate();

            var dict = new Dictionary <string, string>
            {
                { "en", DateTimeClass.DateTimeToItalianFormat(sentDate2) }
            };
            var text = new Language(dict);

            return(await SendMessage.SendMessageInPrivate(sender, e.Message.From.Id,
                                                          e.Message.From.LanguageCode, e.Message.From.Username,
                                                          text, ParseMode.Default, e.Message.MessageId));
        }
        private static async Task MainMethod2(object sender, MessageEventArgs e)
        {
            TelegramBotClient   telegramBotClientBot = null;
            TelegramBotAbstract telegramBotClient    = null;

            try
            {
                if (sender is TelegramBotClient tmp)
                {
                    telegramBotClientBot = tmp;
                }

                if (telegramBotClientBot == null)
                {
                    return;
                }

                telegramBotClient = TelegramBotAbstract.GetFromRam(telegramBotClientBot);

                if (telegramBotClient == null)
                {
                    return;
                }

                await HandleMessageAsync(telegramBotClient, e);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);

                await NotifyUtil.NotifyOwners(exception, telegramBotClient);
            }
        }
        internal async Task <bool> Delete()
        {
            var bot = GlobalVariables.Bots[botId];

            if (bot == null)
            {
                return(false);
            }

            try
            {
                return(await bot.DeleteMessageAsync(chatId, messageId, chatType, accessHash));
            }
            catch (Exception e)
            {
                await NotifyUtil.NotifyOwners(e, bot);
            }

            return(false);
        }
Exemple #5
0
        private static async Task <SuccessWithException> BanUserAsync(
            TelegramBotAbstract sender, MessageEventArgs e,
            string[] stringInfo)
        {
            var r =
                await Groups.CheckIfAdminAsync(e.Message.From.Id, e.Message.From.Username, e.Message.Chat.Id, sender);

            if (!r.IsSuccess())
            {
                return(r);
            }

            if (e.Message.ReplyToMessage == null)
            {
                var userIdFound = await Info.GetTargetUserIdAsync(stringInfo[1], sender);

                if (userIdFound == null)
                {
                    var e2 = new Exception("Can't find userid (1)");
                    await NotifyUtil.NotifyOwners(new ExceptionNumbered(e2), sender);

                    return(new SuccessWithException(false, e2));
                }

                var targetId = userIdFound.GetID();
                if (targetId == null)
                {
                    var e2 = new Exception("Can't find userid (2)");
                    await NotifyUtil.NotifyOwners(new ExceptionNumbered(e2), sender);

                    return(new SuccessWithException(false, e2));
                }

                return(await RestrictUser.BanUserFromGroup(sender, e, targetId.Value, e.Message.Chat.Id, null));
            }

            var targetInt = e.Message.ReplyToMessage.From.Id;

            return(await RestrictUser.BanUserFromGroup(sender, e, targetInt, e.Message.Chat.Id, stringInfo));
        }
Exemple #6
0
        internal static async void DoThingsAsyncBotAsync(object obj)
        {
            var bot = await WebPost.GetAnonBotAsync();

            if (bot == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(ConfigAnon.password))
            {
                return;
            }

            try
            {
                await NotifyUtil.NotifyOwners(new Exception("Check anon message started."), bot);
            }
            catch
            {
                ;
            }

            if (dictionary_webpost == null)
            {
                dictionary_webpost = GetDictionary();
            }

            while (true)
            {
                lock (random)
                {
                    _ = IterationAsync2Async(bot);
                    Thread.Sleep(timesleep);
                }
            }
        }
        internal static async Task MainMethodAsync(TelegramBotAbstract telegramBotAbstract)
        {
            try
            {
                ;
                var links     = new List <string>();
                var groupsRaw = File.ReadAllText(@"C:\Users\eliam\Documents\groups.csv");
                var groups    = Regex.Split(groupsRaw, "\r\n|\r|\n");
                //using StreamWriter groupsFile = new StreamWriter(@"C:\Users\eliam\Documents\WriteLines.txt", append: true);
                //await groupsFile.WriteLineAsync("Nome Gruppo $ Link di Invito");
                using (var sw = File.AppendText(@"C:\Users\eliam\Documents\groupslist.txt"))
                {
                    sw.WriteLine("Nome Gruppo $ Link di Invito");
                }

                foreach (var group in groups)
                {
                    var toBeDone = true;
                    while (toBeDone)
                    {
                        if (group.Length > 0)
                        {
                            try
                            {
                                //    await telegramBotAbstract.FixTheFactThatSomeGroupsDoesNotHaveOurModerationBotAsync();
                                //file,,,
                                var name = group;
                                if (name.Length > 255)
                                {
                                    using (var sw = File.AppendText(@"C:\Users\eliam\Documents\errorlist.txt"))
                                    {
                                        sw.WriteLine(name + " FAILED");
                                    }
                                }

                                var desc = "Gruppo @polinetwork \nPer tutti i link: polinetwork.github.io";

                                var  members = new List <long>(); //ID members to insert
                                long?chatID  = null;
                                while (chatID == null)
                                {
                                    chatID = await telegramBotAbstract.CreateGroup(name, desc, members);
                                }
                                Thread.Sleep(1 * 1000 * 10);
                                var channel = await telegramBotAbstract.upgradeGroupIntoSupergroup(chatID);

                                if (channel == null)
                                {
                                    return;
                                }
                                //await telegramBotAbstract.EditDescriptionChannel(channel, desc);
                                Thread.Sleep(1 * 1000 * 10);
                                await telegramBotAbstract.addUserIntoChannel("@polinetwork3bot", channel);

                                var admins = new List <TLInputUser>();

                                var adminTags = new List <string>
                                {
                                    "polinetwork3bot"
                                };                       //tag members to set admins (MUST BE INSIDE THE members ARRAY)
                                foreach (var admin in adminTags)
                                {
                                    Thread.Sleep(1 * 1000 * 10);
                                    TLAbsInputPeer u =
                                        await UserbotPeer.GetPeerUserWithAccessHash(admin,
                                                                                    telegramBotAbstract._userbotClient);

                                    if (u is TLInputPeerUser u2)
                                    {
                                        var user1 = new TLInputUser {
                                            AccessHash = u2.AccessHash, UserId = u2.UserId
                                        };
                                        admins.Add(user1);
                                    }
                                }

                                foreach (var admin in admins)
                                {
                                    Thread.Sleep(1 * 1000 * 10);
                                    await telegramBotAbstract.PromoteChatMember(admin, channel.Id, channel.AccessHash);
                                }

                                Thread.Sleep(1 * 1000 * 10);
                                var link = await telegramBotAbstract.ExportChatInviteLinkAsync(channel.Id,
                                                                                               channel.AccessHash);

                                links.Add(link);
                                using (var sw = File.AppendText(@"C:\Users\eliam\Documents\groupslist.txt"))
                                {
                                    sw.WriteLine(group + " $ " + link);
                                }

                                Console.WriteLine("added: " + group + " $ " + link);
                                Thread.Sleep(5 * 1000 * 60);
                                toBeDone = false;
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message + " -> In Main Thread!");
                                Thread.Sleep(int.Parse(Regex.Match(e.Message, @"\d+").Value) * 1000);
                                await NotifyUtil.NotifyOwners(e, telegramBotAbstract);
                            }
                        }
                    }
                }

                Console.WriteLine("====== CREATION COMPLETE ======");
            }
            catch (Exception ignore)
            {
                ;
            }
        }
Exemple #8
0
        private static async Task MainMethod2(object sender, MessageEventArgs e)
        {
            TelegramBotClient   telegramBotClientBot = null;
            TelegramBotAbstract telegramBotClient    = null;

            try
            {
                if (sender is TelegramBotClient tmp)
                {
                    telegramBotClientBot = tmp;
                }

                if (telegramBotClientBot == null)
                {
                    return;
                }

                telegramBotClient = TelegramBotAbstract.GetFromRam(telegramBotClientBot);

                var toExit = await ModerationCheck.CheckIfToExitAndUpdateGroupList(telegramBotClient, e);

                if (toExit.Item1 == ToExit.EXIT)
                {
                    var itemToPrint     = MemberListToString(toExit.Item2);
                    var itemToPrint2    = ListIntToString(toExit.Item3);
                    var itemToPrint3    = StringToStringToBePrinted(toExit.Item4);
                    var itemToPrintFull = itemToPrint + "\n" + e?.Message?.Chat?.Title;
                    itemToPrintFull += "\n----\n" + itemToPrint2 + "\n----\nS:" + itemToPrint3;
                    itemToPrintFull += "\n----\n" + e?.Message?.Chat?.Id;
                    itemToPrintFull += "\n@@@@@@";

                    throw new ToExitException(itemToPrintFull);
                }

                var NotAuthorizedBotHasBeenAddedBool =
                    await ModerationCheck.CheckIfNotAuthorizedBotHasBeenAdded(e, telegramBotClient);

                if (NotAuthorizedBotHasBeenAddedBool != null && NotAuthorizedBotHasBeenAddedBool.Count > 0)
                {
                    foreach (var bot in NotAuthorizedBotHasBeenAddedBool)
                    {
                        await RestrictUser.BanUserFromGroup(telegramBotClient, e, bot, e.Message.Chat.Id, null);
                    }
                }

                //todo: send messagge "Bots not allowed here!"

                if (banMessageDetected(e))
                {
                    CommandDispatcher.banMessageActions(telegramBotClient, e);
                    return;
                }

                var toExitBecauseUsernameAndNameCheck =
                    await ModerationCheck.CheckUsernameAndName(e, telegramBotClient);

                if (toExitBecauseUsernameAndNameCheck)
                {
                    return;
                }

                var checkSpam = ModerationCheck.CheckSpam(e);
                if (checkSpam != SpamType.ALL_GOOD && checkSpam != SpamType.SPAM_PERMITTED)
                {
                    await ModerationCheck.AntiSpamMeasure(telegramBotClient, e, checkSpam);

                    return;
                }

                if (checkSpam == SpamType.SPAM_PERMITTED)
                {
                    await ModerationCheck.PermittedSpamMeasure(telegramBotClient, e, checkSpam);

                    return;
                }

                if (e.Message.Text != null && e.Message.Text.StartsWith("/"))
                {
                    await CommandDispatcher.CommandDispatcherMethod(telegramBotClient, e);
                }
                else
                {
                    await TextConversation.DetectMessage(telegramBotClient, e);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);

                await NotifyUtil.NotifyOwners(exception, telegramBotClient);
            }
        }
        private static async void FixThings()
        {
            try
            {
                var bot = GlobalVariables.Bots[768169879];
                if (bot == null)
                {
                    return;
                }

                try
                {
                    await bot.DeleteMessageAsync(-1001314601927, 32, ChatType.Channel, null);
                }
                catch
                {
                    ;
                }

                try
                {
                    await bot.DeleteMessageAsync(-1001314601927, 32, ChatType.Channel, null);
                }
                catch
                {
                    ;
                }

                /*
                 * try
                 * {
                 *  await bot.DeleteMessageAsync(-1001314601927, 30, Telegram.Bot.Types.Enums.ChatType.Supergroup, null);
                 * }
                 * catch
                 * {
                 *  ;
                 * }
                 *
                 * try
                 * {
                 *  await bot.DeleteMessageAsync(-1001314601927, 31, Telegram.Bot.Types.Enums.ChatType.Supergroup, null);
                 * }
                 * catch
                 * {
                 *  ;
                 * }
                 */

                var toSend = "<a href='tg://resolve?domain=-1393901944'>PoliAssociazioni 3</a>";
                var dict   = new Dictionary <string, string>
                {
                    { "it", toSend }
                };
                var text = new Language(dict);
                await bot.SendTextMessageAsync(GlobalVariables.Owners[0].Item1, text, ChatType.Private, "it",
                                               ParseMode.Html,
                                               null, "armef97", null, true);

                /*
                 * await bot.PromoteChatMember(userId: 149620444, //raif
                 *   chatId: -1001314601927);
                 */
            }
            catch (Exception e)
            {
                await NotifyUtil.NotifyOwners(e, GetFirstBot());
            }
        }
        private static async Task SendUsernameWarning(TelegramBotAbstract telegramBotClient,
                                                      bool username, bool name, string lang, string usernameOfUser,
                                                      long chatId, int userId, int?messageId, ChatType messageChatType,
                                                      string firstName, string lastName, User[] newChatMembers)
        {
            var s1I =
                "Imposta un username e un nome più lungo dalle impostazioni di telegram per poter scrivere in questo gruppo\n";

            if (username && !name)
            {
                s1I = "Imposta un username dalle impostazioni di telegram per poter scrivere in questo gruppo\n";
            }
            else if (!username && name)
            {
                s1I = "Imposta un nome più lungo " +
                      "dalle impostazioni di telegram\n";
            }

            var s1E = "Set an username and a longer first name from telegram settings to write in this group\n";

            if (username && !name)
            {
                s1E = "Set an username from telegram settings to write in this group\n";
            }
            else if (!username && name)
            {
                s1E = "Set a longer first name from telegram settings to write in this group\n";
            }

            var s2 = new Language(new Dictionary <string, string>
            {
                { "it", s1I },
                { "en", s1E }
            });

            var r1 = await SendMessage.SendMessageInPrivateOrAGroup(telegramBotClient, s2, lang,
                                                                    usernameOfUser, userId, firstName, lastName, chatId, messageChatType);

            const int MINUTES_WAIT = 2;

            if (r1.GetChatType() != ChatType.Private)
            {
                var r2 = r1.GetMessage();
                if (r2 != null)
                {
                    if (r2 is TLMessage r3)
                    {
                        lock (GlobalVariables.MessagesToDelete)
                        {
                            var timeUntilDelete = TimeSpan.FromMinutes(MINUTES_WAIT);
                            var TimeToDelete    = DateTime.Now + timeUntilDelete;

                            var toDelete = new MessageToDelete(r3, chatId, TimeToDelete, telegramBotClient.GetId(),
                                                               r1.GetChatType(), null);
                            GlobalVariables.MessagesToDelete.Add(toDelete);

                            FileSerialization.WriteToBinaryFile(Paths.Bin.MessagesToDelete,
                                                                GlobalVariables.MessagesToDelete);
                        }
                    }
                    else if (r2 is Message r4)
                    {
                        lock (GlobalVariables.MessagesToDelete)
                        {
                            var timeUntilDelete = TimeSpan.FromMinutes(MINUTES_WAIT);
                            var TimeToDelete    = DateTime.Now + timeUntilDelete;

                            var toDelete = new MessageToDelete(r4, chatId, TimeToDelete, telegramBotClient.GetId(),
                                                               r1.GetChatType(), null);
                            GlobalVariables.MessagesToDelete.Add(toDelete);

                            FileSerialization.WriteToBinaryFile(Paths.Bin.MessagesToDelete,
                                                                GlobalVariables.MessagesToDelete);
                        }
                    }
                    else
                    {
                        var e4 = "Attempted to add a message to be deleted in queue\n" + r2?.GetType() + " " + r2;
                        var e3 = new Exception(e4);
                        await NotifyUtil.NotifyOwners(e3, telegramBotClient);
                    }
                }
            }

            if (newChatMembers == null || newChatMembers.Length == 0)
            {
                await RestrictUser.Mute(60 * 5, telegramBotClient, chatId, userId, messageChatType);
            }

            if (messageId != null)
            {
                await telegramBotClient.DeleteMessageAsync(chatId, messageId.Value, messageChatType, null);
            }
        }