private static void Bot_OnMessage(object sender, MessageEventArgs e) { if (e.Message.Text != null) { Botlog.Write($"Received a text message in chat {e.Message.Chat.Id}."); } }
private static async void BotClientOnOnUpdate(object sender, UpdateEventArgs e) { long?chatId = null; try { Botlog.Write($"Got query: {e.Update.Type}"); if (e.Update.Message != null) { chatId = e.Update.Message.Chat?.Id; var chatRoom = GetOrCreate(e.Update.Message.Chat); chatRoom?.ChatIo.HandleUpdate(e.Update); } else if (e.Update.CallbackQuery != null) { chatId = e.Update.CallbackQuery.Message.Chat?.Id; var chatRoom = GetOrCreate(e.Update.CallbackQuery.Message.Chat); chatRoom?.ChatIo.HandleUpdate(e.Update); await _botClient.AnswerCallbackQueryAsync(e.Update.CallbackQuery.Id); } } catch (Exception error) { Botlog.Error(chatId, $"BotClientOnOnUpdate Failed: {e.Update.Type} {error}"); } }
public async Task <Update> WaitUserInput() { _waitInputCompletionSource = new TaskCompletionSource <Update>(); Botlog.Write("Wait for any"); var result = await _waitInputCompletionSource.Task; Botlog.Write("Got any"); return(result); }
public async Task <string> WaitUserTextInput() { Botlog.Write("Wait for text"); _waitMessageCompletionSource = new TaskCompletionSource <string>(); var result = await _waitMessageCompletionSource.Task; Botlog.Write("Got text"); return(result); }
static async void Bot_OnMessage(object sender, MessageEventArgs e) { if (e.Message.Text != null) { Botlog.Write($"Received a text message in chat {e.Message.Chat.Id}."); //await botClient.SendTextMessageAsync( // chatId: e.Message.Chat, // text: "You said:\n" + e.Message.Text //); } }
static async void BotClientOnOnUpdate(object?sender, UpdateEventArgs e) { Botlog.Write($"Got query: {e.Update.Type}"); if (e.Update.Message != null) { var chatRoom = GetOrCreate(e.Update.Message.Chat); chatRoom?.Chat.HandleUpdate(e.Update); } else if (e.Update.CallbackQuery != null) { var chatRoom = GetOrCreate(e.Update.CallbackQuery.Message.Chat); chatRoom?.Chat.HandleUpdate(e.Update); await _botClient.AnswerCallbackQueryAsync(e.Update.CallbackQuery.Id); } }
static ChatRoomFlow GetOrCreate(Telegram.Bot.Types.Chat chat) { if (_chats.TryGetValue(chat.Id, out var existedChatRoom)) { return(existedChatRoom); } var newChat = new Chat(_botClient, chat); var newChatRoom = new ChatRoomFlow(newChat) { YaDictionaryApi = _yapiDicClient, YaTranslateApi = _yapiTransClient, WordsService = _wordsService }; var task = newChatRoom.Run(); task.ContinueWith((t) => Botlog.Write($"faulted {t.Exception}"), TaskContinuationOptions.OnlyOnFaulted); _chats.TryAdd(chat.Id, newChatRoom); return(null); }
internal void HandleUpdate(Update args) { var msg = args.Message?.Text; if (!string.IsNullOrWhiteSpace(msg)) { if (msg[0] == '/') { if (!_menuItems.Contains(msg)) { SendMessageAsync($"Invalid command '{msg}'").Wait(); return; } var textSrc = _waitMessageCompletionSource; var objSrc = _waitInputCompletionSource; _waitMessageCompletionSource = null; _waitInputCompletionSource = null; textSrc?.SetException(new ProcessInterruptedWithMenuCommand(msg)); objSrc?.SetException(new ProcessInterruptedWithMenuCommand(msg)); return; } if (_waitMessageCompletionSource != null) { Botlog.Write("Set text result"); var src = _waitMessageCompletionSource; _waitMessageCompletionSource = null; src?.SetResult(args.Message.Text); return; } } if (_waitInputCompletionSource != null) { Botlog.Write("Set any result"); var src = _waitInputCompletionSource; _waitInputCompletionSource = null; src?.SetResult(args); } }