private void RemoveTelegramBot(Models.TelegramConversation bot) { if (Model.Telegram.Conversations.Contains(bot)) { Model.Telegram.Conversations.Remove(bot); SettingsChangedCommand.Execute(); TelegramSettingsChangedCommand.Execute(); } }
private void AddTelegramConversation() { if (string.IsNullOrEmpty(TelegramConversation.Name) || string.IsNullOrEmpty(TelegramConversation.BotToken)) { TelegramErrorMessage = "You have to provide both bot token and username/channel title"; return; } TelegramBotClient telegramBotClient = new TelegramBotClient(TelegramConversation.BotToken); Update[] updates; try { updates = telegramBotClient.GetUpdates(); } catch (WebException ex) { TelegramErrorMessage = ex.Message; return; } Update requiredUpdate = updates.FirstOrDefault(update => { StringComparison stringComparison = StringComparison.InvariantCultureIgnoreCase; string username = update.Message != null ? update.Message.Chat.Username : string.Empty; string channelTitle = update.ChannelPost != null ? update.ChannelPost.Chat.Title : string.Empty; if (!string.IsNullOrEmpty(username) && TelegramConversation.Name.Equals(username, stringComparison)) { return(true); } else if (!string.IsNullOrEmpty(channelTitle) && TelegramConversation.Name.Equals(channelTitle, stringComparison)) { return(true); } return(false); }); if (requiredUpdate == null) { TelegramErrorMessage = "There is no open/new conversation or chat between the bot and user/channel, please send a new test message to the bot and then try again"; return; } TelegramConversation.Id = requiredUpdate.Message != null ? requiredUpdate.Message.Chat.Id : requiredUpdate.ChannelPost.Chat.Id; if (Model.Telegram.Conversations.Contains(TelegramConversation)) { TelegramErrorMessage = "This conversation already added"; return; } Model.Telegram.Conversations.Add(TelegramConversation); TelegramConversation = new Models.TelegramConversation(); SettingsChangedCommand.Execute(); TelegramSettingsChangedCommand.Execute(); TelegramErrorMessage = string.Empty; }