//Get Schedule For Day public void GetScheduleForDay(CallbackQueryEventArgs callbackQueryEventArgs) { var args = ArgParser.ParseCallbackData(callbackQueryEventArgs.CallbackQuery.Data); var directionArg = args.GetValueOrDefault(Commands.Direction); var direction = directionArg == Way.ToCTIR.ToString() ? Way.ToCTIR : Way.ToRzeszow; DateTime selectedDay = DateTime.Parse(args.GetValueOrDefault(Commands.Date)); //Get schedule for date var text = ScheduleHelper.GenerateSchedule(selectedDay, direction, User.GetLanguage); //Send menu to get back var inlineKeyboard = new InlineKeyboardMarkup(new[] { new [] // First row { // Bus stations button InlineKeyboardButton.WithCallbackData(Local.BusStations[User.GetLanguage], Commands.GetBusStation), }, new [] // Second row { // Weather button InlineKeyboardButton.WithCallbackData(Local.Weather[User.GetLanguage], $"{Commands.RefreshWeather}"), // Bus schedule url InlineKeyboardButton.WithUrl(Local.URLSchedule[User.GetLanguage], "https://wsiz.edu.pl/studia/informator/rozklad-jazdy-uczelnianego-autobusu"), }, // Third row TemplateModelsBuilder.WaysForBus() }); SendMessageAsync(ChatId, text, ParseMode.Markdown, replyMarkup: inlineKeyboard); DeleteMessageAsync(ChatId, MessageId); }
//Change language public void ChangeLanguage(CallbackQueryEventArgs callbackQueryEventArgs) { var args = ArgParser.ParseCallbackData(callbackQueryEventArgs.CallbackQuery.Data); var lang = args.GetValueOrDefault(Commands.Language); LocalLanguage newLanguage = LocalLanguage.English; if (lang == "ua") { newLanguage = LocalLanguage.Ukrainian; } else if (lang == "pl") { newLanguage = LocalLanguage.Polish; } //Save User Language if (User.Language != newLanguage) { User.Language = newLanguage; ApplicationData.SaveUsers(); } string messageText = Local.StartString[User.GetLanguage] + $"Bot version `{ApplicationData.BotVersion}`"; var inlineKeyboard = TemplateModelsBuilder.BuildStartMenuMarkup(); EditMessageTextAsync(ChatId, MessageId, messageText, replyMarkup: inlineKeyboard, parseMode: ParseMode.Markdown); }
public void Users(MessageEventArgs messageEventArgs) { var users = TemplateModelsBuilder.GetTopUsers(); var inlineKeyboard = TemplateModelsBuilder.UsersStatsMarkup(); SendMessageAsync(ChatId, users, ParseMode.Markdown, replyMarkup: inlineKeyboard); }
public async void Weather(MessageEventArgs messageEventArgs) { var weather = await WeatherHelper.GetWeather(User.GetLanguage); var inlineKeyboard = TemplateModelsBuilder.RefreshWeather(User.GetLanguage); SendMessageAsync(ChatId, weather, ParseMode.Markdown, replyMarkup: inlineKeyboard); }
public void Start(MessageEventArgs messageEventArgs) { string messageText = Local.StartString[0] + $"Bot version `{ApplicationData.BotVersion}`"; var inlineKeyboard = TemplateModelsBuilder.BuildStartMenuMarkup(); SendMessageAsync(ChatId, messageText, ParseMode.Markdown, replyMarkup: inlineKeyboard); }
public void RefreshUsers(CallbackQueryEventArgs callbackQueryEventArgs) { var Users = TemplateModelsBuilder.GetTopUsers(); Users += $"\n`{DateTime.UtcNow}`"; var inlineKeyboard = TemplateModelsBuilder.UsersStatsMarkup(); EditMessageTextAsync(ChatId, MessageId, Users, ParseMode.Markdown, replyMarkup: inlineKeyboard); }
//Refresh weather public async void RefreshWeather(CallbackQueryEventArgs callbackQueryEventArgs) { var args = ArgParser.ParseCallbackData(callbackQueryEventArgs.CallbackQuery.Data); bool isEdit = args.ContainsKey(Commands.IsEdit); var weather = await WeatherHelper.GetWeather(User.GetLanguage); var inlineKeyboard = TemplateModelsBuilder.RefreshWeather(User.GetLanguage); if (isEdit) { EditMessageTextAsync(ChatId, MessageId, weather, ParseMode.Markdown, replyMarkup: inlineKeyboard); } else { SendMessageAsync(ChatId, weather, ParseMode.Markdown, replyMarkup: inlineKeyboard); } }
public void Stats(MessageEventArgs messageEventArgs) { string stats = $"*Monthly Users:* `{ ApplicationData.Users.Set().Count(u => u.ActiveAt > DateTime.UtcNow.AddDays(-30))}`\n"; stats += $"*Weekly Users:* `{ApplicationData.Users.Set().Count(u => u.ActiveAt > DateTime.UtcNow.AddDays(-7))}`\n\n"; stats += (ApplicationData.Users.Set().Count() > 0 ? "*Activity Days* list:\n" : "*Activity Days* list is empty"); foreach (var stat in ApplicationData.Stats.Set().OrderByDescending(s => s.Date).Where(s => s.Date > DateTime.UtcNow.AddDays(-7))) { stats += $"{stat.Date.ToString("dd/MM/yy")} - `{stat.UserId.Count}` - `{stat.ActiveClicks}`\n"; } stats += "\n*Languages:*\n"; stats += $"🇬🇧 - {ApplicationData.Users.Set().Count(u => u.Language == LocalLanguage.English)}"; stats += $" 🇺🇦 - {ApplicationData.Users.Set().Count(u => u.Language == LocalLanguage.Ukrainian)}"; stats += $" 🇮🇩 - {ApplicationData.Users.Set().Count(u => u.Language == LocalLanguage.Polish)}"; var inlineKeyboard = TemplateModelsBuilder.StatsMarkup(); SendMessageAsync(ChatId, stats, ParseMode.Markdown, replyMarkup: inlineKeyboard); }
//Bus stations public void GetBusStation(CallbackQueryEventArgs callbackQueryEventArgs) { var args = ArgParser.ParseCallbackData(callbackQueryEventArgs.CallbackQuery.Data); var busStation = args.GetValueOrDefault(Commands.BusStation); //List All stations if (busStation == null) { var stationsInline = TemplateModelsBuilder.BusStations(); EditMessageTextAsync(ChatId, MessageId, Local.PickBusStation[User.GetLanguage], ParseMode.Markdown, replyMarkup: stationsInline); } //Get station info else { var stationId = int.Parse(busStation); var station = (Local.PointNames)stationId; var inlineKeyboard = new InlineKeyboardMarkup(new[] { new [] //First row { InlineKeyboardButton.WithCallbackData(Local.BusStations[User.GetLanguage], Commands.GetBusStation), }, new [] { InlineKeyboardButton.WithCallbackData(Local.Menu[User.GetLanguage], Commands.GetStartMenu), }, TemplateModelsBuilder.WaysForBus() }); DeleteMessageAsync(ChatId, MessageId); SendMessageAsync(ChatId, Local.BusPointNames[station], ParseMode.Markdown); SendLocationAsync(ChatId, Local.BusPoints[station][0], Local.BusPoints[station][1]); SendMessageAsync(ChatId, Local.ReturnBack[User.GetLanguage], ParseMode.Markdown, replyMarkup: inlineKeyboard); } }
private async void Invoker(MessageEventArgs messageEventArgs = null, CallbackQueryEventArgs callbackQueryEventArgs = null) { // Get message data var chatId = messageEventArgs != null ? messageEventArgs.Message.Chat.Id : callbackQueryEventArgs.CallbackQuery.Message.Chat.Id; var messageId = messageEventArgs != null ? messageEventArgs.Message.MessageId : callbackQueryEventArgs.CallbackQuery.Message.MessageId; var sender = messageEventArgs != null ? messageEventArgs.Message.From : callbackQueryEventArgs.CallbackQuery.From; var user = ApplicationData.GetUser(sender.Id); // Get controller type Type controllerType = messageEventArgs != null ? typeof(CommandController) : typeof(CallbackController); // Get method name from message string methodName = ""; if (messageEventArgs != null) { methodName = (string)ArgParser.ParseCommand(messageEventArgs.Message.Text).GetValueOrDefault(Commands.MethodName); } else { methodName = (string)ArgParser.ParseCallbackData(callbackQueryEventArgs.CallbackQuery.Data).GetValueOrDefault(Commands.MethodName); } // Find method in controller MethodInfo method = controllerType.GetMethod(methodName); if (method != null) { // Check user access by role if (!BaseController.ValidateAccess(method, user)) { return; } try { // Get and send chatAction from attributes var chatAction = BaseController.GetChatActionAttributes(method); if (chatAction.HasValue) { await Bot.SendChatActionAsync(chatId, chatAction.Value); } // Cast controller object var controller = Activator.CreateInstance(controllerType); // Set params ((BaseController)controller).Bot = Bot; ((BaseController)controller).ChatId = chatId; ((BaseController)controller).MessageId = messageId; ((BaseController)controller).User = user; // Invoke method if (messageEventArgs != null) { method.Invoke(controller, parameters: new object[] { messageEventArgs }); } else { method.Invoke(controller, parameters: new object[] { callbackQueryEventArgs }); } } catch (Exception ex) { Log.Error(ex, "Invoker error"); // Send log to admin long?adminId = ApplicationData.Users.Set().FirstOrDefault(x => x.UserAccess == UserAccess.Admin)?.Id; if (adminId.HasValue) { SendMessageAsync(adminId.Value, ex.ToString()); SendMessageAsync(adminId.Value, callbackQueryEventArgs?.CallbackQuery.Data ?? "F"); } } } else { // Cant method did not exists InfluxDBLiteClient.Query("bots,botname=wsizbusbot,actiontype=backenderror action=true"); // Send log to admin long?adminId = ApplicationData.Users.Set().FirstOrDefault(x => x.UserAccess == UserAccess.Admin)?.Id; if (adminId.HasValue) { SendMessageAsync(adminId.Value, callbackQueryEventArgs != null ? $"Cant find method for: {callbackQueryEventArgs.CallbackQuery.Data}" : $"Cant find method for: {messageEventArgs.Message.Text}"); } SendMessageAsync(chatId, Local.ErrorMessage[user.GetLanguage], ParseMode.Markdown, replyMarkup: TemplateModelsBuilder.BuildStartMenuMarkup()); // todo // delete callback keyboard under the message? } }
//Start menu public void GetStartMenu(CallbackQueryEventArgs callbackQueryEventArgs) { var inlineKeyboard = TemplateModelsBuilder.BuildStartMenuMarkup(); EditMessageTextAsync(ChatId, MessageId, Local.StartString[User.GetLanguage], replyMarkup: inlineKeyboard); }