Skip to content

Commit

Permalink
Updated/simplified documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
wiz0u committed Jun 26, 2024
1 parent e9cda06 commit bccabf0
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 239 deletions.
2 changes: 1 addition & 1 deletion Examples/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ root = true
# All Files
[*]
charset = utf-8
end_of_line = lf
#end_of_line = lf
indent_style = space
indent_size = 4
insert_final_newline = true
Expand Down
14 changes: 5 additions & 9 deletions Examples/1/ExampleBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ internal class ExampleBot
private async Task BookExamples()
{
// ANCHOR: example-bot
var botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}");
using CancellationTokenSource cts = new();

using CancellationTokenSource cts = new ();
var botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}", cancellationToken: cts.Token);

// StartReceiving does not block the caller thread. Receiving is done on the ThreadPool.
ReceiverOptions receiverOptions = new ()
var receiverOptions = new ReceiverOptions()
{
AllowedUpdates = Array.Empty<UpdateType>() // receive all update types except ChatMember related updates
};

botClient.StartReceiving(
updateHandler: HandleUpdateAsync,
pollingErrorHandler: HandlePollingErrorAsync,
receiverOptions: receiverOptions,
cancellationToken: cts.Token
receiverOptions: receiverOptions
);

var me = await botClient.GetMeAsync();
Expand All @@ -52,10 +51,7 @@ async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, Cancel
Console.WriteLine($"Received a '{messageText}' message in chat {chatId}.");

// Echo received message text
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "You said:\n" + messageText,
cancellationToken: cancellationToken);
Message sentMessage = await botClient.SendTextMessageAsync(chatId, "You said:\n" + messageText);
}

Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
Expand Down
112 changes: 39 additions & 73 deletions Examples/2/ReplyMarkup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,138 +10,104 @@ namespace BookExamples.Chapter2;

internal class ReplyMarkup
{
private readonly ITelegramBotClient botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}");
private readonly ChatId chatId = 12345;
private readonly CancellationToken cancellationToken = new CancellationTokenSource().Token;
public readonly ITelegramBotClient botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}");
public readonly ChatId chatId = 12345;

private async Task SingleRowMarkup()
{
// ANCHOR: single-row
ReplyKeyboardMarkup replyKeyboardMarkup = new(new[]
var buttons = new KeyboardButton[]
{
new KeyboardButton[] { "Help me", "Call me ☎️" },
})
{
ResizeKeyboard = true
"Help me", "Call me ☎️",
};

Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "Choose a response",
replyMarkup: replyKeyboardMarkup,
cancellationToken: cancellationToken);
var sent = await botClient.SendTextMessageAsync(chatId, "Choose a response",
replyMarkup: new ReplyKeyboardMarkup(buttons) { ResizeKeyboard = true });
// ANCHOR_END: single-row
}

private async Task MultipleRowMarkup()
{
// ANCHOR: multiple-row
ReplyKeyboardMarkup replyKeyboardMarkup = new(new[]
var buttons = new KeyboardButton[][]
{
new KeyboardButton[] { "Help me" },
new KeyboardButton[] { "Call me ☎️" },
})
{
ResizeKeyboard = true
new KeyboardButton[] { "Call me ☎️", "Write me ✉️" },
};

Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "Choose a response",
replyMarkup: replyKeyboardMarkup,
cancellationToken: cancellationToken);
var sent = await botClient.SendTextMessageAsync(chatId, "Choose a response",
replyMarkup: new ReplyKeyboardMarkup(buttons) { ResizeKeyboard = true });
// ANCHOR_END: multiple-row
}

private async Task RequestInfo()
{
// ANCHOR: request-info
ReplyKeyboardMarkup replyKeyboardMarkup = new(new[]
var buttons = new[]
{
KeyboardButton.WithRequestLocation("Share Location"),
KeyboardButton.WithRequestContact("Share Contact"),
});
};

Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "Who or Where are you?",
replyMarkup: replyKeyboardMarkup,
cancellationToken: cancellationToken);
var sent = await botClient.SendTextMessageAsync(chatId, "Who or Where are you?",
replyMarkup: new ReplyKeyboardMarkup(buttons));
// ANCHOR_END: request-info
}

private async Task RemoveKeyboard()
{
// ANCHOR: remove-keyboard
Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "Removing keyboard",
replyMarkup: new ReplyKeyboardRemove(),
cancellationToken: cancellationToken);
var sent = await botClient.SendTextMessageAsync(chatId, "Removing keyboard",
replyMarkup: new ReplyKeyboardRemove());
// ANCHOR_END: remove-keyboard
}

private async Task CallbackButtons()
{
// ANCHOR: callback-buttons
InlineKeyboardMarkup inlineKeyboard = new(new[]
var buttons = new InlineKeyboardButton[][]
{
// first row
new []
new[] // first row
{
InlineKeyboardButton.WithCallbackData(text: "1.1", callbackData: "11"),
InlineKeyboardButton.WithCallbackData(text: "1.2", callbackData: "12"),
InlineKeyboardButton.WithCallbackData("1.1", "11"),
InlineKeyboardButton.WithCallbackData("1.2", "12"),
},
// second row
new []
new[] // second row
{
InlineKeyboardButton.WithCallbackData(text: "2.1", callbackData: "21"),
InlineKeyboardButton.WithCallbackData(text: "2.2", callbackData: "22"),
InlineKeyboardButton.WithCallbackData("2.1", "21"),
InlineKeyboardButton.WithCallbackData("2.2", "22"),
},
});
};

Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "A message with an inline keyboard markup",
replyMarkup: inlineKeyboard,
cancellationToken: cancellationToken);
var sent = await botClient.SendTextMessageAsync(chatId, "A message with an inline keyboard markup",
replyMarkup: new InlineKeyboardMarkup(buttons));
// ANCHOR_END: callback-buttons
}

private async Task UrlButtons()
{
// ANCHOR: url-buttons
InlineKeyboardMarkup inlineKeyboard = new(new[]
var buttons = new[]
{
InlineKeyboardButton.WithUrl(
text: "Link to the Repository",
url: "https://github.com/TelegramBots/Telegram.Bot")
});

Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "A message with an inline keyboard markup",
replyMarkup: inlineKeyboard,
cancellationToken: cancellationToken);
InlineKeyboardButton.WithUrl("Repositoy Link", "https://github.com/TelegramBots/Telegram.Bot")
};

var sent = await botClient.SendTextMessageAsync(chatId, "A message with an inline keyboard markup",
replyMarkup: new InlineKeyboardMarkup(buttons));
// ANCHOR_END: url-buttons
}

private async Task SwitchToInline()
{
// ANCHOR: switch-to-inline
InlineKeyboardMarkup inlineKeyboard = new(new[]
var buttons = new[]
{
InlineKeyboardButton.WithSwitchInlineQuery(
text: "switch_inline_query"),
InlineKeyboardButton.WithSwitchInlineQueryCurrentChat(
text: "switch_inline_query_current_chat"),
});

Message sentMessage = await botClient.SendTextMessageAsync(
chatId: chatId,
text: "A message with an inline keyboard markup",
replyMarkup: inlineKeyboard,
cancellationToken: cancellationToken);
InlineKeyboardButton.WithSwitchInlineQuery("switch_inline_query"),
InlineKeyboardButton.WithSwitchInlineQueryCurrentChat("switch_inline_query_current_chat"),
};

var sent = await botClient.SendTextMessageAsync(chatId, "A message with an inline keyboard markup",
replyMarkup: new InlineKeyboardMarkup(buttons));
// ANCHOR_END: switch-to-inline
}
}
Loading

0 comments on commit bccabf0

Please sign in to comment.