コード例 #1
0
        public void Execute(Message message)
        {
            if (!Utils.BotTools.IsUserOperator(message.From.Id) &&
                !Utils.ChatTools.IsUserAdmin(message.Chat.Id, message.From.Id))
            {
                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp        = DateTime.UtcNow,
                    Chat             = message.Chat,
                    ReplyToMessageId = message.MessageId,
                    Text             = CacheData.GetTranslation("en", "error_not_auth_command")
                });
                return;
            }

            string btnList = "This is the list of welcome buttons:";

            BusinessLogic.ButtonLogic buttonLogic = new BusinessLogic.ButtonLogic();
            foreach (Button btn in buttonLogic
                     .GetByChat(CacheData.Groups[message.Chat.Id]
                                .GroupId))
            {
                btnList += Environment.NewLine;
                btnList += "* " + btn.Name + " -> " + btn.Content;
            }

            MessageQueueManager.EnqueueMessage(
                new Models.ChatMessage()
            {
                Timestamp             = DateTime.UtcNow,
                Chat                  = message.Chat,
                Text                  = btnList,
                DisableWebPagePreview = true
            });
        }
コード例 #2
0
ファイル: Functions.cs プロジェクト: unified-ban/Terminal
        public static void UserJoinedAction(Message message)
        {
            if (message.NewChatMembers.SingleOrDefault(x => x.Id == Manager.MyId) != null &&
                (message.Chat.Type == ChatType.Group ||
                 message.Chat.Type == ChatType.Supergroup))
            {
                Data.Utils.Logging.AddLog(new SystemLog()
                {
                    LoggerName = CacheData.LoggerName,
                    Date       = DateTime.Now,
                    Function   = "Unifiedban.Bot.Manager.BotClient_OnMessage",
                    Level      = SystemLog.Levels.Info,
                    Message    = $"I have been added to Group {message.Chat.Title} ID {message.Chat.Id}",
                    UserId     = -1
                });

#if DEBUG
                if (!CacheData.BetaAuthChats.Contains(message.Chat.Id))
                {
                    Manager.BotClient.SendTextMessageAsync(
                        chatId: message.Chat.Id,
                        text: "⚠️Error registering new group. Not authorized to Beta channel.\n" +
                        "Join our support group to verify if you could join it: @unifiedban_group"
                        );
                    Manager.BotClient.SendTextMessageAsync(
                        chatId: CacheData.ControlChatId,
                        parseMode: ParseMode.Markdown,
                        text: String.Format(
                            "*[Log]*\n" +
                            "⚠️Error registering new group. Not authorized to Beta channel.\n" +
                            "\nChat: {0}" +
                            "\nChatId: {1}" +
                            "\n\n*hash_code:* #UB{2}-{3}",
                            message.Chat.Title,
                            message.Chat.Id,
                            message.Chat.Id.ToString().Replace("-", ""),
                            Guid.NewGuid())
                        );
                    return;
                }
#endif

                if (!CacheData.Groups.ContainsKey(message.Chat.Id))
                {
                    bool registered = RegisterGroup(message);
                    if (!registered)
                    {
                        Manager.BotClient.SendTextMessageAsync(
                            chatId: CacheData.ControlChatId,
                            parseMode: ParseMode.Markdown,
                            text: String.Format(
                                "*[Log]*\n" +
                                "⚠️Error registering new group.\n" +
                                "\nChat: {0}" +
                                "\nChatId: {1}" +
                                "\n\n*hash_code:* #UB{2}-{3}",
                                message.Chat.Title,
                                message.Chat.Id,
                                message.Chat.Id.ToString().Replace("-", ""),
                                Guid.NewGuid())
                            );
                    }
                }
            }

            if (!CacheData.Groups.ContainsKey(message.Chat.Id))
            {
                return;
            }

            bool blacklistEnabled = false;
            ConfigurationParameter blacklistConfig = CacheData.GroupConfigs[message.Chat.Id]
                                                     .Where(x => x.ConfigurationParameterId == "Blacklist")
                                                     .FirstOrDefault();
            if (blacklistConfig != null)
            {
                if (blacklistConfig.Value.ToLower() == "true")
                {
                    blacklistEnabled = true;
                }
            }

            bool rtlNameCheckEnabled = false;
            ConfigurationParameter rtlNameCheckConfig = CacheData.GroupConfigs[message.Chat.Id]
                                                        .Where(x => x.ConfigurationParameterId == "RTLNameFilter")
                                                        .FirstOrDefault();
            if (rtlNameCheckConfig != null)
            {
                if (rtlNameCheckConfig.Value.ToLower() == "true")
                {
                    rtlNameCheckEnabled = true;
                }
            }

            bool captchaEnabled = false;
            ConfigurationParameter captchaConfig = CacheData.GroupConfigs[message.Chat.Id]
                                                   .Where(x => x.ConfigurationParameterId == "Captcha")
                                                   .FirstOrDefault();
            if (captchaConfig != null)
            {
                if (captchaConfig.Value.ToLower() == "true")
                {
                    captchaEnabled = true;
                }
            }

            bool welcomeMessageEnabled = false;
            ConfigurationParameter welcomeMessageConfig = CacheData.GroupConfigs[message.Chat.Id]
                                                          .Where(x => x.ConfigurationParameterId == "WelcomeMessage")
                                                          .FirstOrDefault();
            if (welcomeMessageConfig != null)
            {
                if (welcomeMessageConfig.Value.ToLower() == "true")
                {
                    welcomeMessageEnabled = true;
                }
            }

            foreach (User member in message.NewChatMembers)
            {
                if (member.Id == Manager.MyId ||
                    member.Id == 777000 || // Telegram's official updateServiceNotification
                    member.IsBot)
                {
                    continue;
                }

                if (blacklistEnabled)
                {
                    if (Utils.UserTools.KickIfIsInBlacklist(message, member))
                    {
                        continue;
                    }
                }

                if (rtlNameCheckEnabled)
                {
                    Filters.FilterResult rtlCheck = RTLNameFilter.DoCheck(message,
                                                                          member.FirstName + " " + member.LastName);
                    if (rtlCheck.Result == Filters.IFilter.FilterResultType.positive)
                    {
                        try
                        {
                            Manager.BotClient.RestrictChatMemberAsync(
                                message.Chat.Id,
                                member.Id,
                                new ChatPermissions()
                            {
                                CanSendMessages       = false,
                                CanAddWebPagePreviews = false,
                                CanChangeInfo         = false,
                                CanInviteUsers        = false,
                                CanPinMessages        = false,
                                CanSendMediaMessages  = false,
                                CanSendOtherMessages  = false,
                                CanSendPolls          = false
                            }
                                );
                        }
                        catch (Exception ex)
                        {
                            Data.Utils.Logging.AddLog(new SystemLog()
                            {
                                LoggerName = CacheData.LoggerName,
                                Date       = DateTime.Now,
                                Function   = "UserJoinedAction -> rtlNameCheckEnabled",
                                Level      = SystemLog.Levels.Error,
                                Message    = ex.Message,
                                UserId     = -1
                            });

                            if (ex.InnerException != null)
                            {
                                Data.Utils.Logging.AddLog(new SystemLog()
                                {
                                    LoggerName = CacheData.LoggerName,
                                    Date       = DateTime.Now,
                                    Function   = "UserJoinedAction -> rtlNameCheckEnabled",
                                    Level      = SystemLog.Levels.Error,
                                    Message    = ex.Message,
                                    UserId     = -1
                                });
                            }
                        }

                        continue;
                    }
                }

                try
                {
                    bool pluginCheckOk = true;
                    foreach (var plugin in CacheData.PreCaptchaAndWelcomePlugins)
                    {
                        if (!plugin.Execute(message, member, MessageQueueManager.EnqueueMessage))
                        {
                            pluginCheckOk = false;
                            Manager.BotClient.KickChatMemberAsync(message.Chat.Id, member.Id);
                            if (message.Chat.Type == ChatType.Supergroup)
                            {
                                Manager.BotClient.UnbanChatMemberAsync(message.Chat.Id, member.Id);
                            }
                            break;
                        }
                    }

                    if (!pluginCheckOk)
                    {
                        continue;
                    }

                    if (CacheData.TrustFactors.ContainsKey(member.Id))
                    {
                        int points = CacheData.TrustFactors[member.Id].Points;
                        if (points < 71)
                        {
                            Manager.BotClient.SendTextMessageAsync(
                                chatId: message.Chat,
                                parseMode: ParseMode.Markdown,
                                text: String.Format(
                                    "*[Alert]*\n" +
                                    "⚠️ Trust factor of user {0}:{1} is below security threshold\n" +
                                    "\n*Trust factor:* {2}/100",
                                    member.Id,
                                    member.Username,
                                    points)
                                );
                        }
                    }
                    if (captchaEnabled && CacheData.Operators
                        .SingleOrDefault(x => x.TelegramUserId == member.Id) == null)
                    {
                        Manager.BotClient.RestrictChatMemberAsync(
                            message.Chat.Id,
                            member.Id,
                            new ChatPermissions()
                        {
                            CanSendMessages       = false,
                            CanAddWebPagePreviews = false,
                            CanChangeInfo         = false,
                            CanInviteUsers        = false,
                            CanPinMessages        = false,
                            CanSendMediaMessages  = false,
                            CanSendOtherMessages  = false,
                            CanSendPolls          = false
                        }
                            ).Wait();

                        string name = member.Username != null ? "@" + member.Username : member.FirstName;
                        MessageQueueManager.EnqueueMessage(
                            new Models.ChatMessage()
                        {
                            Timestamp   = DateTime.UtcNow,
                            Chat        = message.Chat,
                            ParseMode   = ParseMode.Default,
                            Text        = $"Please {name} certify to be a human.\nIf you don't click this button you are not going to be unlocked.",
                            ReplyMarkup = new InlineKeyboardMarkup(
                                InlineKeyboardButton.WithCallbackData(
                                    CacheData.GetTranslation("en", "captcha_iamhuman", true),
                                    $"/Captcha " + member.Id
                                    )
                                )
                        });

                        continue;
                    }

                    if (welcomeMessageEnabled)
                    {
                        BusinessLogic.ButtonLogic           buttonLogic = new BusinessLogic.ButtonLogic();
                        List <List <InlineKeyboardButton> > buttons     = new List <List <InlineKeyboardButton> >();
                        int btnCount   = 0;
                        int depthLevel = 0;
                        buttons.Add(new List <InlineKeyboardButton>());

                        foreach (Button btn in buttonLogic
                                 .GetByChat(CacheData.Groups[message.Chat.Id]
                                            .GroupId))
                        {
                            if (btnCount == 2)
                            {
                                btnCount = 0;
                                buttons.Add(new List <InlineKeyboardButton>());
                                depthLevel++;
                            }
                            buttons[depthLevel].Add(InlineKeyboardButton.WithUrl(btn.Name, btn.Content));
                            btnCount++;
                        }

                        MessageQueueManager.EnqueueMessage(
                            new Models.ChatMessage()
                        {
                            Timestamp = DateTime.UtcNow,
                            Chat      = message.Chat,
                            ParseMode = ParseMode.Html,
                            Text      = Utils.Parsers.VariablesParser(
                                CacheData.Groups[message.Chat.Id].WelcomeText,
                                message, member),
                            ReplyMarkup = new InlineKeyboardMarkup(
                                buttons
                                )
                        });
                    }

                    foreach (var plugin in CacheData.PostCaptchaAndWelcomePlugins)
                    {
                        if (!plugin.Execute(message, member, MessageQueueManager.EnqueueMessage))
                        {
                            Manager.BotClient.KickChatMemberAsync(message.Chat.Id, member.Id);
                            if (message.Chat.Type == ChatType.Supergroup)
                            {
                                Manager.BotClient.UnbanChatMemberAsync(message.Chat.Id, member.Id);
                            }
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Data.Utils.Logging.AddLog(new SystemLog()
                    {
                        LoggerName = CacheData.LoggerName,
                        Date       = DateTime.Now,
                        Function   = "UserJoinedAction",
                        Level      = SystemLog.Levels.Error,
                        Message    = ex.Message,
                        UserId     = -1
                    });

                    if (ex.InnerException != null)
                    {
                        Data.Utils.Logging.AddLog(new SystemLog()
                        {
                            LoggerName = CacheData.LoggerName,
                            Date       = DateTime.Now,
                            Function   = "UserJoinedAction",
                            Level      = SystemLog.Levels.Error,
                            Message    = ex.Message,
                            UserId     = -1
                        });
                    }
                }
            }
        }
コード例 #3
0
        public void Execute(CallbackQuery callbackQuery)
        {
            long captchaId = Convert.ToInt64(callbackQuery.Data.Split(" ")[1]);

            if (captchaId != callbackQuery.From.Id)
            {
                return;
            }

            Manager.BotClient.DeleteMessageAsync(
                callbackQuery.Message.Chat.Id,
                callbackQuery.Message.MessageId);

            Manager.BotClient.RestrictChatMemberAsync(
                callbackQuery.Message.Chat.Id,
                callbackQuery.From.Id,
                Manager.BotClient.GetChatAsync(callbackQuery.Message.Chat.Id).Result.Permissions);

            string name = callbackQuery.From.Username != null ? "@" + callbackQuery.From.Username : callbackQuery.From.FirstName;

            bool welcomeMessage = false;
            ConfigurationParameter welcomeMessageConfig = CacheData.GroupConfigs[callbackQuery.Message.Chat.Id]
                                                          .Where(x => x.ConfigurationParameterId == "WelcomeMessage")
                                                          .FirstOrDefault();

            if (welcomeMessageConfig != null)
            {
                if (welcomeMessageConfig.Value.ToLower() == "true")
                {
                    welcomeMessage = true;
                }
            }

            if (welcomeMessage)
            {
                BusinessLogic.ButtonLogic           buttonLogic = new BusinessLogic.ButtonLogic();
                List <List <InlineKeyboardButton> > buttons     = new List <List <InlineKeyboardButton> >();
                foreach (Button btn in buttonLogic
                         .GetByChat(CacheData.Groups[callbackQuery.Message.Chat.Id]
                                    .GroupId))
                {
                    buttons.Add(new List <InlineKeyboardButton>());
                    buttons[buttons.Count - 1].Add(InlineKeyboardButton.WithUrl(btn.Name, btn.Content));
                }

                MessageQueueManager.EnqueueMessage(
                    new Models.ChatMessage()
                {
                    Timestamp = DateTime.UtcNow,
                    Chat      = callbackQuery.Message.Chat,
                    ParseMode = ParseMode.Html,
                    Text      = Utils.Parsers.VariablesParser(
                        CacheData.Groups[callbackQuery.Message.Chat.Id].WelcomeText,
                        callbackQuery),
                    ReplyMarkup = new InlineKeyboardMarkup(
                        buttons
                        )
                });

                return;
            }

            MessageQueueManager.EnqueueMessage(
                new Models.ChatMessage()
            {
                Timestamp = DateTime.UtcNow,
                Chat      = callbackQuery.Message.Chat,
                ParseMode = ParseMode.Markdown,
                Text      = $"Thank you `{name}`!\nYou are now fully unlocked.",
            });
        }