public static void Initialize(string apikey) { if (CacheData.FatalError) { return; } if (String.IsNullOrEmpty(apikey)) { Data.Utils.Logging.AddLog(new Models.SystemLog() { LoggerName = CacheData.LoggerName, Date = DateTime.Now, Function = "Unifiedban Terminal Startup", Level = Models.SystemLog.Levels.Fatal, Message = "API KEY must be set!", UserId = -1 }); CacheData.FatalError = true; return; } APIKEY = apikey; instanceId = Guid.NewGuid().ToString(); currentHostname = System.Net.Dns.GetHostName(); Commands.Initialize(); BotClient = new TelegramBotClient(APIKEY); var me = BotClient.GetMeAsync().Result; Username = me.Username; MyId = me.Id; Data.Utils.Logging.AddLog(new Models.SystemLog() { LoggerName = CacheData.LoggerName, Date = DateTime.Now, Function = "Unifiedban Terminal Startup", Level = Models.SystemLog.Levels.Warn, Message = $"Hello, World! I am user {me.Id} and my name is {me.FirstName}.", UserId = -1 }); MessageQueueManager.AddChatIfNotPresent(CacheData.ControlChatId); BotClient.OnMessage += BotClient_OnMessage; BotClient.OnCallbackQuery += BotClient_OnCallbackQuery; Console.Title = $"Unifiedban - Username: {me.Username} - Instance ID: {instanceId}"; Data.Utils.Logging.AddLog(new Models.SystemLog() { LoggerName = CacheData.LoggerName, Date = DateTime.Now, Function = "Unifiedban Terminal Startup", Level = Models.SystemLog.Levels.Info, Message = "Bot Client initialized", UserId = -2 }); }
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; } }