public async Task TestCallbackQuery() { var replyMarkup = new InlineKeyboardMarkup( new InlineKeyboardButton[][] { new InlineKeyboardButton[] { "one", "two" }, new InlineKeyboardButton[] { "three", "four" }, } ); await Bot.SendTextMessageAsync( ChatId, "Read inline query test.", replyMarkup : replyMarkup ); var readUpd = await BotExt.ReadUpdateAsync(async (newCtx, currentCtx) => { if (newCtx.Update.Type == UpdateType.CallbackQuery) { return(UpdateValidatorResult.Valid); } return(UpdateValidatorResult.ContinueWaiting); }); await SendTextMessageAsync($"You chosen '{readUpd.CallbackQuery.Data}'."); }
public async Task Hi(string str) { //Use dependency injection just like you do in ASP.NET. //Use extensions to make your life eathier. //Some of extension services are included as controller properties: // * logger // * BotExt (improved bot) // * mvc features // ... //Access current telegram Update as property of controller. //Extension too. Send message to current chat. await SendTextMessageAsync("Hi."); if (IsModelStateValid) { await SendTextMessageAsync( $"Hi. You passed '{str}' with command. Note, default model binder split all command parameters by space," + $"like in CLI, so you can't pass string with command using default model binder." ); } await UpdateContext.SendTextMessageAsync("Send message with reply to bot."); //Improved bot extension. Allow to await new messages from current chat, just like in console applications. var message = await BotExt.ReadMessageAsync(ReadCallbackFromType.CurrentUserReply); await SendTextMessageAsync($"You send: {message.Text}"); //Use UpdateContext to get more info about current "request". }
public async Task ReadMsgTest() { await UpdateContext.SendTextMessageAsync("Send me a message."); var message = await BotExt.ReadMessageAsync(ReadCallbackFromType.CurrentUser); await SendTextMessageAsync($"You send: {message.Text}"); }
public async Task ForceExitTest() { await SendTextMessageAsync("Send me /cancel"); await BotExt.ReadMessageAsync(ManagedKeyboard.DefaultUpdatesValidator); await SendTextMessageAsync("This will not be send."); }
public async Task TestRestoreMarkup() { await SendTextMessageAsync($"Send me something like:\n\n" + $"`Code str` \n```Code``` \n*Bold* \n_Italic_ \n[google link](https://google.com)"); var readMsg = await BotExt.ReadMessageAsync(); var markdownText = readMsg.RestoreMarkdown(); await SendTextMessageAsync(markdownText, parseMode : ParseMode.MarkdownV2); }
/// <summary> /// BotExtensions based on UpdateContext and can't work without it. /// Thats why it here, not in BotContext. /// </summary> public static BotExt BotExt(this UpdateContext @this) { const string ctxPropName = "_BotExt"; try { BotExt botExt = null; if (@this.Properties.TryGetValue(ctxPropName, out var val)) { try { botExt = (BotExt)val; } catch (Exception ex) { throw new InvalidCastException( $"Can`t cast UpdateContext[\"{ctxPropName}\"] value '{val}' to type '{typeof(BotExt)}'.", ex ); } } else { botExt = new BotExt(@this.Services.GetRequiredService <IBotExtSingleton>(), @this); @this.Properties[ctxPropName] = botExt; } if (botExt == null) { throw new NullReferenceException("Found BotExt value is null."); } return(botExt); } catch (Exception ex) { throw new TelegramAspException( "BotExt resolve exception. Maybe middleware ImprovedBot wasn`t registered.", ex ); } }
public async Task NewBot() { await Bot.SendTextMessageAsync(ChatId, "Enter bot name: "); Message msg = await BotExt.ReadMessageAsync(); var name = msg.Text; await Bot.SendTextMessageAsync(ChatId, "Enter bot nikname: "); msg = await BotExt.ReadMessageAsync(); var nick = msg.Text; //Creating bot started... //If operation is too long, you can use CancellationToken to calcel it when cancellation requested, just like ReadMessageAsync do. UpdateProcessingAborted.ThrowIfCancellationRequested(); //Creating bot finished... await Bot.SendTextMessageAsync(ChatId, "Bot created."); //In mvc middleware called by default if found command or read-callback. //Next middleware can ignore it. UpdateContext.Processed(); }