How can I call hadlers?
- type in telegram /myCommand
- press button in markup
- enter data if telegram is waiting for command (text, pics, etc.)
Example for /myCommand handler
Just type /myCommand in telegram and press enter and you will get to this handler.
[Command(Name = "myCommand", Permission = Permissions.Guest)]
public class MyCommand : ITelegramCommand<Message>
{
private readonly ITelegramBotClient _telegramBotClient;
public MyCommand(ITelegramBotClient telegramBotClient)
{
_telegramBotClient = telegramBotClient;
}
public async Task<ITelegramCommandExecutionResult> Execute(Message query)
{
await _telegramBotClient.SendTextMessageAsync(query.GetChatId(), "This is SimpleCommand handler");
return TelegramCommandExecutionResult.Break();
}
}
Example for button handler
lets create some button
public async Task<ITelegramCommandExecutionResult> Execute(Message query) { var chatId = query.GetChatId(); var builder = new InlineMarkupQueryBuilder(); builder.AddInlineKeyboardButton<MyButtonCallback>(new CallbackData() { CallbackText = "somDataHere", Text = "text" } ); await _telegramBotClient.SendTextMessageAsync(query.GetChatId(), $"call button handler", replyMarkup: builder.GetResult()); return TelegramCommandExecutionResult.Break(); }
and in click handler command (MyButtonCallBack) lets handle
[Command(Name = "buttonCallBack", Permission = Permissions.Callback)] public class MyButtonCallback : ITelegramCommand<CallbackQuery> { private readonly ITelegramBotClient _telegramBotClient; public MyButtonCallback(ITelegramBotClient telegramBotClient) { _telegramBotClient = telegramBotClient; } public async Task<ITelegramCommandExecutionResult> Execute(CallbackQuery query) { await _telegramBotClient.SendTextMessageAsync(query.GetChatId(), $"this is button handler"); return TelegramCommandExecutionResult.Break(); } }
Enter text data
in this case we use sessions that will wait by default for 600 sec.
[Command(Name = "start", Permission = Permissions.Guest)] public class StartCommand : ITelegramCommand<Message> { private readonly ITelegramBotClient _telegramBotClient; public StartCommand(ITelegramBotClient telegramBotClient) { _telegramBotClient = telegramBotClient; } public async Task<ITelegramCommandExecutionResult> Execute(Message query) { var chatId = query.GetChatId(); await _telegramBotClient.SendTextMessageAsync(query.GetChatId(), $"this is ahead example"); return TelegramCommandExecutionResult.Ahead<SomeAheadHandlerCommand, Message>(); } }
handler command
[Command(Name = "someAheadHandlerCommand", Permission = Permissions.Session)] public class SomeAheadHandlerCommand: ISessionTelegramCommand<Message, EmptyObject> { private readonly ITelegramBotClient _telegramBotClient; public SomeAheadHandlerCommand(ITelegramBotClient telegramBotClient) { _telegramBotClient = telegramBotClient; } public async Task<ITelegramCommandExecutionResult> Execute(Message query, EmptyObject sessionObject) { await _telegramBotClient.SendTextMessageAsync(query.GetChatId(), $"Received " + query.Text); return TelegramCommandExecutionResult.Break(); } }
Building chain from AheadCommands
foe entering data we lets build some chain for that
[Command(Name = "start", Permission = Permissions.Guest)] public class StartCommand : ITelegramCommand<Message> { private readonly ITelegramBotClient _telegramBotClient; public StartCommand(ITelegramBotClient telegramBotClient) { _telegramBotClient = telegramBotClient; } public async Task<ITelegramCommandExecutionResult> Execute(Message query) { var chatId = query.GetChatId(); await _telegramBotClient.SendTextMessageAsync(query.GetChatId(), $"this is ahead example"); return TelegramCommandExecutionResult.Ahead<SomeAheadHandlerCommand, Message, long>(111); } }
someAheadHandlerCommand
[Command(Name = "someAheadHandlerCommand", Permission = Permissions.Session)] public class SomeAheadHandlerCommand: ISessionTelegramCommand<Message, long> { private readonly ITelegramBotClient _telegramBotClient; public SomeAheadHandlerCommand(ITelegramBotClient telegramBotClient) { _telegramBotClient = telegramBotClient; } public async Task<ITelegramCommandExecutionResult> Execute(Message query, long sessionObject) { await _telegramBotClient.SendTextMessageAsync(query.GetChatId(), $"Received1 " + sessionObject.ToString()); return TelegramCommandExecutionResult.Ahead<OtherAheadHandlerCommand, Message, long>(222); } }
otherAheadHandlerCommand
[Command(Name = "otherAheadHandlerCommand", Permission = Permissions.Session)] public class OtherAheadHandlerCommand: ISessionTelegramCommand<Message, long> { private readonly ITelegramBotClient _telegramBotClient; public OtherAheadHandlerCommand(ITelegramBotClient telegramBotClient) { _telegramBotClient = telegramBotClient; } public async Task<ITelegramCommandExecutionResult> Execute(Message query, long sessionObject) { await _telegramBotClient.SendTextMessageAsync(query.GetChatId(), $"Received2 " + sessionObject.ToString()); return TelegramCommandExecutionResult.Break(); } }
How to transfer data between session commands ?
session data saved in COMMAND_SESSION table in SESSION_DATA field.On each Ahead program will wait for 10 minutes by default to enter data. Break() will end session.
sending
return TelegramCommandExecutionResult.Ahead<SomeAheadHandlerCommand, Message, long>(111);
receiving
public async Task<ITelegramCommandExecutionResult> Execute(Message query, long sessionObject) { await _telegramBotClient.SendTextMessageAsync(query.GetChatId(), $"Received1 " + sessionObject.ToString()); }
Building chain from Buttons (CallbackQueries)
StartCommand
[Command(Name = "start", Permission = Permissions.Guest)] public class StartCommand : ITelegramCommand<Message> { private readonly ITelegramBotClient _telegramBotClient; public StartCommand(ITelegramBotClient telegramBotClient) { _telegramBotClient = telegramBotClient; } public async Task<ITelegramCommandExecutionResult> Execute(Message query) { var chatId = query.GetChatId(); var builder = new InlineMarkupQueryBuilder(); builder.AddInlineKeyboardButton<MyButtonCallback>(new CallbackData() { CallbackText = "Hello from start command", Text = "Next" }); await _telegramBotClient.SendTextMessageAsync(query.GetChatId(), $"this is button chain example", replyMarkup: builder.GetResult()); return TelegramCommandExecutionResult.Break(); } }
MyButtonCallback
private readonly ITelegramBotClient _telegramBotClient; public MyButtonCallback(ITelegramBotClient telegramBotClient) { _telegramBotClient = telegramBotClient; } public async Task<ITelegramCommandExecutionResult> Execute(CallbackQuery query) { var data = this.ExtractData(query); var builder = new InlineMarkupQueryBuilder(); builder.AddInlineKeyboardButton<MyOtherButtonCallback>(new CallbackData() { CallbackText = "Hello from MyButtonCallback", Text = "Next" }); await _telegramBotClient.SendTextMessageAsync(query.GetChatId(), $"Received1 {data}", replyMarkup: builder.GetResult()); return TelegramCommandExecutionResult.Break(); }
MyOtherButtonCallback
[Command(Name = "otherButtonCallBack", Permission = Permissions.Callback)] public class MyOtherButtonCallback : ITelegramCommand<CallbackQuery> { private readonly ITelegramBotClient _telegramBotClient; public MyOtherButtonCallback(ITelegramBotClient telegramBotClient) { _telegramBotClient = telegramBotClient; } public async Task<ITelegramCommandExecutionResult> Execute(CallbackQuery query) { var data = this.ExtractData(query); await _telegramBotClient.SendTextMessageAsync(query.GetChatId(), $"Received2 {data}"); return TelegramCommandExecutionResult.Break(); } }
How to transfer data between button CallbackQueries ?
sending
builder.AddInlineKeyboardButton<MyOtherButtonCallback>(new CallbackData() { CallbackText = "Hello from MyButtonCallback", // <<< here our data, some id or etc... Text = "Next" });
receiving inside command
var data = this.ExtractData(query);
Reactions. What is it and how to use it?
public class StartCommand : ITelegramCommand<Message>
{
private readonly ITelegramBotClient _telegramBotClient;
public StartCommand(ITelegramBotClient telegramBotClient)
{
_telegramBotClient = telegramBotClient;
}
public async Task<ITelegramCommandExecutionResult> Execute(Message query)
{
var chatId = query.GetChatId();
var builder = new InlineMarkupQueryBuilder();
builder.AddInlineKeyboardButton<MySessionButtonCallback>(new CallbackData()
{
CallbackText = "hi, there",
Text = "ReactionExampleButton"
});
await _telegramBotClient.SendTextMessageAsync(query.GetChatId(), $"press button", replyMarkup: builder.GetResult());
return TelegramCommandExecutionResult.Ahead<SomeAheadHandlerCommand, Message>();
}
}
comand where we will wait for reaction or enter some data
[Command(Name = "someAheadHandlerCommand", Permission = Permissions.Session)]
public class SomeAheadHandlerCommand: ISessionTelegramCommand<Message, EmptyObject>,
IReaction<MySessionButtonCallback, CallbackQuery>
{
private readonly ITelegramBotClient _telegramBotClient;
public SomeAheadHandlerCommand(ITelegramBotClient telegramBotClient)
{
_telegramBotClient = telegramBotClient;
}
public async Task<ITelegramCommandExecutionResult> Execute(Message query, EmptyObject sessionObject)
{
await _telegramBotClient.SendTextMessageAsync(query.GetChatId(), $"Received1 {query.Text} enter smth. else");
return TelegramCommandExecutionResult.Ahead<OtherAheadHandlerCommand,Message>();
}
}
Waiting for some set of actions, using reactions
we will wait for some definite reactions, other ones will be ignored, and exception will be thrown, TelegramDomainException will Freeze the session and send replyMarkup
[Command(Name = "start", Permission = Permissions.Guest)] public class StartCommand : ITelegramCommand<Message> { private readonly ITelegramBotClient _telegramBotClient; public StartCommand(ITelegramBotClient telegramBotClient) { _telegramBotClient = telegramBotClient; } public async Task<ITelegramCommandExecutionResult> Execute(Message query) { var chatId = query.GetChatId(); var builder = new InlineMarkupQueryBuilder(); builder.AddInlineKeyboardButton<MySessionButtonCallback>(new CallbackData() { CallbackText = "hi, there", Text = "ReactionExampleButton" }); await _telegramBotClient.SendTextMessageAsync(query.GetChatId(), $"press button", replyMarkup: builder.GetResult()); return TelegramCommandExecutionResult.Ahead<WaitReactionCommand, Message>(); } }
waitReactionComand
[Command(Name = "waitcommandexample", Permission = Permissions.Session)] public class WaitReactionCommand : ISessionTelegramCommand<Message, EmptyObject>, IReaction<MySessionButtonCallback, CallbackQuery> { public async Task<ITelegramCommandExecutionResult> Execute(Message query, EmptyObject sessionObject) { throw new TelegramDomainException("this is exception"); } }
How to send message from code ?
await _telegramBotClient.SendTextMessageAsync(chatId, text: "this is start command");
How to delete message ?
await _telegramBotClient.DeleteMessageAsync(query.GetChatId(), query.MessageId);
When we type Ahead, it means, that we wait forsome action and prepared some action.
How to send hint
await _telegramBotClient.AnswerCallbackQueryAsync(query.Id, "Вариант ответа записан");