public async void SendTextMessageToChat(string message) { await Bot.SendTextMessageAsync(TelegramUsers.Where(x => x.UserStatus == TelegramUserStatus.UserIsAdmin).Single().chatID, message); foreach (TelegramUser tUser in TelegramUsers.Where(x => x.UserStatus == TelegramUserStatus.UserIsRegistered)) { await Bot.SendTextMessageAsync(tUser.chatID, message); } }
private async void BotOnCallbackQueryReceived(object sender, CallbackQueryEventArgs callbackQueryEventArgs) { switch (callbackQueryEventArgs.CallbackQuery.Data) { case "userAdd": await Bot.AnswerCallbackQueryAsync( callbackQueryEventArgs.CallbackQuery.Id, string.Format("Received {0}", callbackQueryEventArgs.CallbackQuery.Data) ); long offlineUserID = TelegramUsers.Where(x => x.UserStatus == TelegramUserStatus.UserIsNotRegistered).Single().chatID; TelegramUsers.Remove(TelegramUsers.Where(x => x.UserStatus == TelegramUserStatus.UserIsNotRegistered).Single()); TelegramUsers.Add(new TelegramUser(TelegramUserStatus.UserIsRegistered, offlineUserID)); break; } }
private async void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs) { var message = messageEventArgs.Message; if (TelegramUsers.Count == 0) { TelegramUsers.Add(new TelegramUser(TelegramUserStatus.UserIsAdmin, message.Chat.Id)); await Bot.SendTextMessageAsync(TelegramUsers.Where(x => x.UserStatus == TelegramUserStatus.UserIsAdmin).Select(x => x.chatID).Single() , string.Format(resMan.getResource("telegramBotMessageWelcomeAdmin"), message.Chat.Id + ";" + message.Chat.FirstName)); } else if (TelegramUsers.Where(x => x.UserStatus == TelegramUserStatus.UserIsAdmin).Select(x => x.chatID == message.Chat.Id).Single()) { await Bot.SendTextMessageAsync(TelegramUsers.Where(x => x.UserStatus == TelegramUserStatus.UserIsAdmin).Select(x => x.chatID).Single() , string.Format(resMan.getResource("telegramBotMessageWelcomeBackAdmin"), message.Chat.Id + ";" + message.Chat.FirstName)); } else if (! (TelegramUsers.Contains(new TelegramUser(TelegramUserStatus.UserIsNotRegistered, message.Chat.Id)) || TelegramUsers.Contains(new TelegramUser(TelegramUserStatus.UserIsRegistered, message.Chat.Id)) || TelegramUsers.Contains(new TelegramUser(TelegramUserStatus.UserIsAdmin, message.Chat.Id)))) { TelegramUsers.Add(new TelegramUser(TelegramUserStatus.UserIsNotRegistered, message.Chat.Id)); var keyboard = new InlineKeyboardMarkup(new[] { new[] // first row { new InlineKeyboardButton(resMan.getResource("telegramBotButtonAddUser"), "userAdd"), new InlineKeyboardButton(resMan.getResource("telegramBotButtonBlockUser"), "userBlock"), } }); await Bot.SendTextMessageAsync( TelegramUsers.Where(x => x.UserStatus == TelegramUserStatus.UserIsAdmin).Select(x => x.chatID).Single() , string.Format(resMan.getResource("telegramBotMessageNewUserFound"), message.Chat.Id + ";" + message.Chat.FirstName), replyMarkup : keyboard); await Bot.SendTextMessageAsync( TelegramUsers.Where(x => x.UserStatus == TelegramUserStatus.UserIsNotRegistered).Select(x => x.chatID).Single(), resMan.getResource("telegramBotMessageWaitForAdministrator")); } else { } if (message == null || message.Type != MessageType.TextMessage) { return; } if (message.Text.StartsWith("/inline")) // send inline keyboard { await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.Typing); var keyboard = new InlineKeyboardMarkup(new[] { new[] // first row { new InlineKeyboardButton("1.1"), new InlineKeyboardButton("1.2"), }, new[] // second row { new InlineKeyboardButton("2.1"), new InlineKeyboardButton("2.2"), } }); await Task.Delay(500); // simulate longer running task await Bot.SendTextMessageAsync(message.Chat.Id, "Choose", replyMarkup : keyboard); } else if (message.Text.StartsWith("/keyboard")) // send custom keyboard { var keyboard = new ReplyKeyboardMarkup(new[] { new [] // first row { new KeyboardButton("1.1"), new KeyboardButton("1.2"), }, new [] // last row { new KeyboardButton("2.1"), new KeyboardButton("2.2"), } }); await Bot.SendTextMessageAsync(message.Chat.Id, "Choose", replyMarkup : keyboard); } else if (message.Text.StartsWith("/photo")) // send a photo { await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.UploadPhoto); const string file = @"<FilePath>"; var fileName = file.Split('\\').Last(); using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read)) { var fts = new FileToSend(fileName, fileStream); await Bot.SendPhotoAsync(message.Chat.Id, fts, "Nice Picture"); } } else if (message.Text.StartsWith("/request")) // request location or contact { var keyboard = new ReplyKeyboardMarkup(new [] { new KeyboardButton("Location") { RequestLocation = true }, new KeyboardButton("Contact") { RequestContact = true }, }); await Bot.SendTextMessageAsync(message.Chat.Id, "Who or Where are you?", replyMarkup : keyboard); } else { } }