Example #1
0
        public static void UserLeftAction(Message message)
        {
            if (Utils.BotTools.IsUserOperator(message.LeftChatMember.Id))
            {
                if (!CacheData.ActiveSupport
                    .Contains(message.Chat.Id))
                {
                    CacheData.ActiveSupport.Remove(message.Chat.Id);
                    CacheData.CurrentChatAdmins.Remove(message.Chat.Id);

                    Manager.BotClient.SendTextMessageAsync(
                        chatId: message.Chat.Id,
                        parseMode: ParseMode.Markdown,
                        text: String.Format(
                            "Support session *{0}* ended since operator left the chat.",
                            message.LeftChatMember.Username)
                        );
                    MessageQueueManager.EnqueueLog(new ChatMessage()
                    {
                        ParseMode = ParseMode.Markdown,
                        Text      = String.Format(
                            "*[Log]*" +
                            "Support session ended since operator *{0}* left the chat." +
                            "\nChatId: `{1}`" +
                            "\nChat: `{2}`" +
                            "\nUserId: `{3}`" +
                            "\n\n*hash_code:* #UB{4}-{5}",
                            message.LeftChatMember.Username,
                            message.Chat.Id,
                            message.Chat.Title,
                            message.LeftChatMember.Id,
                            message.Chat.Id.ToString().Replace("-", ""),
                            Guid.NewGuid())
                    });
                }
            }
        }
Example #2
0
        private static async void BotClient_OnMessage(object sender, MessageEventArgs e)
        {
            if (e == null)
            {
                return;
            }

            if (e.Message.Date < DateTime.Now.AddDays(-1))
            {
                return;
            }

            await Task.Run(() => CacheData.IncrementHandledMessages());

            if (CacheData.Groups.Keys.Contains(e.Message.Chat.Id))
            {
                if (CacheData.Groups[e.Message.Chat.Id].State !=
                    Models.Group.TelegramGroup.Status.Active &&
                    e.Message.Text != "/enable")
                {
                    return;
                }
            }

            Data.Utils.Logging.AddLog(new Models.SystemLog()
            {
                LoggerName = CacheData.LoggerName,
                Date       = DateTime.Now,
                Function   = "Unifiedban.Bot.Manager.BotClient_OnMessage",
                Level      = Models.SystemLog.Levels.Debug,
                Message    = "Message received",
                UserId     = -1
            });

            await Task.Run(() => Functions.CacheUsername(e.Message));

            if (e.Message.MigrateToChatId != 0)
            {
                Functions.MigrateToChatId(e.Message);
            }

            bool isPrivateChat = e.Message.Chat.Type == ChatType.Private ||
                                 e.Message.Chat.Type == ChatType.Channel;

            if (isPrivateChat)
            {
                MessageQueueManager.AddChatIfNotPresent(e.Message.Chat.Id);
            }

            bool justAdded = false;

            if (e.Message.NewChatMembers != null)
            {
                justAdded = e.Message.NewChatMembers.SingleOrDefault(x => x.Id == MyId) != null;
            }

            if (!justAdded && !isPrivateChat &&
                !CacheData.Groups.ContainsKey(e.Message.Chat.Id))
            {
                string logMessage = String.Format(
                    "*[Alert]*\n" +
                    "Group *{0}* left due to missing group record in database.\n" +
                    "⚠ do not open links you don't know ⚠\n" +
                    "\nChat: `{1}`" +
                    "\n\n*hash_code:* #UB{2}-{3}",
                    e.Message.Chat.Title,
                    e.Message.Chat.Id,
                    e.Message.Chat.Id.ToString().Replace("-", ""),
                    Guid.NewGuid());
                MessageQueueManager.EnqueueLog(new Models.ChatMessage()
                {
                    ParseMode = ParseMode.Markdown,
                    Text      = logMessage
                });

                await BotClient.SendTextMessageAsync(e.Message.Chat.Id,
                                                     "We're sorry but an error has occurred while retrieving this chat on our database.\n" +
                                                     "Please add again the bot if you want to continue to use it.\n" +
                                                     "For any doubt reach us in our support group @unifiedban_group");

                await BotClient.LeaveChatAsync(e.Message.Chat.Id);

                return;
            }

            if (!string.IsNullOrEmpty(e.Message.Text) &&
                !Utils.UserTools.KickIfInBlacklist(e.Message))
            {
                bool isCommand = false;
                if (e.Message.Text.StartsWith('/'))
                {
                    isCommand = Command.Parser.Parse(e.Message).Result;
                }

                if (e.Message.ReplyToMessage != null && !isCommand && !isPrivateChat)
                {
                    if (e.Message.ReplyToMessage.From.Id == MyId)
                    {
                        CommandQueueManager.ReplyMessage(e.Message);
                        return;
                    }
                }

                if (!Utils.ChatTools.HandleSupportSessionMsg(e.Message) && !isCommand &&
                    e.Message.From.Id != 777000 && !isPrivateChat)  // Telegram's official updateServiceNotification
                {
                    Controls.Manager.DoCheck(e.Message);
                }
            }
            if (e.Message.NewChatMembers != null)
            {
                Functions.UserJoinedAction(e.Message);
            }
            if (e.Message.LeftChatMember != null)
            {
                Functions.UserLeftAction(e.Message);
            }

            if (!String.IsNullOrEmpty(e.Message.MediaGroupId) ||
                e.Message.Photo != null ||
                e.Message.Document != null)
            {
                Controls.Manager.DoMediaCheck(e.Message);
            }

            if (!isPrivateChat && e.Message.NewChatTitle != null)
            {
                CacheData.Groups[e.Message.Chat.Id].Title = e.Message.NewChatTitle;
            }
        }