Skip to content

Commit

Permalink
Update Examples for v22.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wiz0u committed Nov 4, 2024
1 parent e9e363b commit e5b59e2
Show file tree
Hide file tree
Showing 25 changed files with 93 additions and 92 deletions.
2 changes: 1 addition & 1 deletion Console.Advanced/Abstract/ReceiverServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task ReceiveAsync(CancellationToken stoppingToken)
DropPendingUpdates = true,
};

var me = await _botClient.GetMeAsync(stoppingToken);
var me = await _botClient.GetMe(stoppingToken);
_logger.LogInformation("Start receiving updates for {BotName}", me.Username ?? "My Awesome Bot");

// Start receiving updates
Expand Down
2 changes: 1 addition & 1 deletion Console.Advanced/Console.Advanced.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Telegram.Bot" Version="21.7.1" />
<PackageReference Include="Telegram.Bot" Version="22.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Console.Advanced/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Make sure that your .csproj contains these items (versions may vary):

```xml
<ItemGroup>
<PackageReference Include="Telegram.Bot" Version="21.3.0" />
<PackageReference Include="Telegram.Bot" Version="22.0.0" />
</ItemGroup>
```

Expand Down
32 changes: 16 additions & 16 deletions Console.Advanced/Services/UpdateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private async Task OnMessage(Message msg)
"/throw" => FailingHandler(msg),
_ => Usage(msg)
});
logger.LogInformation("The message was sent with id: {SentMessageId}", sentMessage.MessageId);
logger.LogInformation("The message was sent with id: {SentMessageId}", sentMessage.Id);
}

async Task<Message> Usage(Message msg)
Expand All @@ -76,15 +76,15 @@ async Task<Message> Usage(Message msg)
/poll_anonymous - send an anonymous poll
/throw - what happens if handler fails
""";
return await bot.SendTextMessageAsync(msg.Chat, usage, parseMode: ParseMode.Html, replyMarkup: new ReplyKeyboardRemove());
return await bot.SendMessage(msg.Chat, usage, parseMode: ParseMode.Html, replyMarkup: new ReplyKeyboardRemove());
}

async Task<Message> SendPhoto(Message msg)
{
await bot.SendChatActionAsync(msg.Chat, ChatAction.UploadPhoto);
await bot.SendChatAction(msg.Chat, ChatAction.UploadPhoto);
await Task.Delay(2000); // simulate a long task
await using var fileStream = new FileStream("Files/bot.gif", FileMode.Open, FileAccess.Read);
return await bot.SendPhotoAsync(msg.Chat, fileStream, caption: "Read https://telegrambots.github.io/book/");
return await bot.SendPhoto(msg.Chat, fileStream, caption: "Read https://telegrambots.github.io/book/");
}

// Send inline keyboard. You can process responses in OnCallbackQuery handler
Expand All @@ -95,45 +95,45 @@ async Task<Message> SendInlineKeyboard(Message msg)
.AddNewRow()
.AddButton("WithCallbackData", "CallbackData")
.AddButton(InlineKeyboardButton.WithUrl("WithUrl", "https://github.com/TelegramBots/Telegram.Bot"));
return await bot.SendTextMessageAsync(msg.Chat, "Inline buttons:", replyMarkup: inlineMarkup);
return await bot.SendMessage(msg.Chat, "Inline buttons:", replyMarkup: inlineMarkup);
}

async Task<Message> SendReplyKeyboard(Message msg)
{
var replyMarkup = new ReplyKeyboardMarkup(true)
.AddNewRow("1.1", "1.2", "1.3")
.AddNewRow().AddButton("2.1").AddButton("2.2");
return await bot.SendTextMessageAsync(msg.Chat, "Keyboard buttons:", replyMarkup: replyMarkup);
return await bot.SendMessage(msg.Chat, "Keyboard buttons:", replyMarkup: replyMarkup);
}

async Task<Message> RemoveKeyboard(Message msg)
{
return await bot.SendTextMessageAsync(msg.Chat, "Removing keyboard", replyMarkup: new ReplyKeyboardRemove());
return await bot.SendMessage(msg.Chat, "Removing keyboard", replyMarkup: new ReplyKeyboardRemove());
}

async Task<Message> RequestContactAndLocation(Message msg)
{
var replyMarkup = new ReplyKeyboardMarkup(true)
.AddButton(KeyboardButton.WithRequestLocation("Location"))
.AddButton(KeyboardButton.WithRequestContact("Contact"));
return await bot.SendTextMessageAsync(msg.Chat, "Who or Where are you?", replyMarkup: replyMarkup);
return await bot.SendMessage(msg.Chat, "Who or Where are you?", replyMarkup: replyMarkup);
}

async Task<Message> StartInlineQuery(Message msg)
{
var button = InlineKeyboardButton.WithSwitchInlineQueryCurrentChat("Inline Mode");
return await bot.SendTextMessageAsync(msg.Chat, "Press the button to start Inline Query\n\n" +
return await bot.SendMessage(msg.Chat, "Press the button to start Inline Query\n\n" +
"(Make sure you enabled Inline Mode in @BotFather)", replyMarkup: new InlineKeyboardMarkup(button));
}

async Task<Message> SendPoll(Message msg)
{
return await bot.SendPollAsync(msg.Chat, "Question", PollOptions, isAnonymous: false);
return await bot.SendPoll(msg.Chat, "Question", PollOptions, isAnonymous: false);
}

async Task<Message> SendAnonymousPoll(Message msg)
{
return await bot.SendPollAsync(chatId: msg.Chat, "Question", PollOptions);
return await bot.SendPoll(chatId: msg.Chat, "Question", PollOptions);
}

static Task<Message> FailingHandler(Message msg)
Expand All @@ -145,8 +145,8 @@ static Task<Message> FailingHandler(Message msg)
private async Task OnCallbackQuery(CallbackQuery callbackQuery)
{
logger.LogInformation("Received inline keyboard callback from: {CallbackQueryId}", callbackQuery.Id);
await bot.AnswerCallbackQueryAsync(callbackQuery.Id, $"Received {callbackQuery.Data}");
await bot.SendTextMessageAsync(callbackQuery.Message!.Chat, $"Received {callbackQuery.Data}");
await bot.AnswerCallbackQuery(callbackQuery.Id, $"Received {callbackQuery.Data}");
await bot.SendMessage(callbackQuery.Message!.Chat, $"Received {callbackQuery.Data}");
}

#region Inline Mode
Expand All @@ -159,13 +159,13 @@ private async Task OnInlineQuery(InlineQuery inlineQuery)
new InlineQueryResultArticle("1", "Telegram.Bot", new InputTextMessageContent("hello")),
new InlineQueryResultArticle("2", "is the best", new InputTextMessageContent("world"))
];
await bot.AnswerInlineQueryAsync(inlineQuery.Id, results, cacheTime: 0, isPersonal: true);
await bot.AnswerInlineQuery(inlineQuery.Id, results, cacheTime: 0, isPersonal: true);
}

private async Task OnChosenInlineResult(ChosenInlineResult chosenInlineResult)
{
logger.LogInformation("Received inline result: {ChosenInlineResultId}", chosenInlineResult.ResultId);
await bot.SendTextMessageAsync(chosenInlineResult.From.Id, $"You chose result with Id: {chosenInlineResult.ResultId}");
await bot.SendMessage(chosenInlineResult.From.Id, $"You chose result with Id: {chosenInlineResult.ResultId}");
}

#endregion
Expand All @@ -181,7 +181,7 @@ private async Task OnPollAnswer(PollAnswer pollAnswer)
var answer = pollAnswer.OptionIds.FirstOrDefault();
var selectedOption = PollOptions[answer];
if (pollAnswer.User != null)
await bot.SendTextMessageAsync(pollAnswer.User.Id, $"You've chosen: {selectedOption.Text} in poll");
await bot.SendMessage(pollAnswer.User.Id, $"You've chosen: {selectedOption.Text} in poll");
}

private Task UnknownUpdateHandlerAsync(Update update)
Expand Down
2 changes: 1 addition & 1 deletion Console/Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Telegram.Bot" Version="21.7.1" />
<PackageReference Include="Telegram.Bot" Version="22.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
31 changes: 16 additions & 15 deletions Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

using var cts = new CancellationTokenSource();
var bot = new TelegramBotClient(token, cancellationToken: cts.Token);
var me = await bot.GetMeAsync();
await bot.DropPendingUpdatesAsync();
var me = await bot.GetMe();
await bot.DeleteWebhook(); // you may comment this line if you find it unnecessary
await bot.DropPendingUpdates(); // you may comment this line if you find it unnecessary
bot.OnError += OnError;
bot.OnMessage += OnMessage;
bot.OnUpdate += OnUpdate;
Expand Down Expand Up @@ -58,7 +59,7 @@ async Task OnCommand(string command, string args, Message msg)
switch (command)
{
case "/start":
await bot.SendTextMessageAsync(msg.Chat, """
await bot.SendMessage(msg.Chat, """
<b><u>Bot menu</u></b>:
/photo [url] - send a photo <i>(optionally from an <a href="https://picsum.photos/310/200.jpg">url</a>)</i>
/inline_buttons - send inline buttons
Expand All @@ -71,13 +72,13 @@ await bot.SendTextMessageAsync(msg.Chat, """
break;
case "/photo":
if (args.StartsWith("http"))
await bot.SendPhotoAsync(msg.Chat, args, caption: "Source: " + args);
await bot.SendPhoto(msg.Chat, args, caption: "Source: " + args);
else
{
await bot.SendChatActionAsync(msg.Chat, ChatAction.UploadPhoto);
await bot.SendChatAction(msg.Chat, ChatAction.UploadPhoto);
await Task.Delay(2000); // simulate a long task
await using var fileStream = new FileStream("bot.gif", FileMode.Open, FileAccess.Read);
await bot.SendPhotoAsync(msg.Chat, fileStream, caption: "Read https://telegrambots.github.io/book/");
await bot.SendPhoto(msg.Chat, fileStream, caption: "Read https://telegrambots.github.io/book/");
}
break;
case "/inline_buttons":
Expand All @@ -86,22 +87,22 @@ await bot.SendTextMessageAsync(msg.Chat, """
.AddNewRow()
.AddButton("WithCallbackData", "CallbackData")
.AddButton(InlineKeyboardButton.WithUrl("WithUrl", "https://github.com/TelegramBots/Telegram.Bot"));
await bot.SendTextMessageAsync(msg.Chat, "Inline buttons:", replyMarkup: inlineMarkup);
await bot.SendMessage(msg.Chat, "Inline buttons:", replyMarkup: inlineMarkup);
break;
case "/keyboard":
var replyMarkup = new ReplyKeyboardMarkup(true)
var replyMarkup = new ReplyKeyboardMarkup()
.AddNewRow("1.1", "1.2", "1.3")
.AddNewRow().AddButton("2.1").AddButton("2.2");
await bot.SendTextMessageAsync(msg.Chat, "Keyboard buttons:", replyMarkup: replyMarkup);
await bot.SendMessage(msg.Chat, "Keyboard buttons:", replyMarkup: replyMarkup);
break;
case "/remove":
await bot.SendTextMessageAsync(msg.Chat, "Removing keyboard", replyMarkup: new ReplyKeyboardRemove());
await bot.SendMessage(msg.Chat, "Removing keyboard", replyMarkup: new ReplyKeyboardRemove());
break;
case "/poll":
await bot.SendPollAsync(msg.Chat, "Question", ["Option 0", "Option 1", "Option 2"], isAnonymous: false, allowsMultipleAnswers: true);
await bot.SendPoll(msg.Chat, "Question", ["Option 0", "Option 1", "Option 2"], isAnonymous: false, allowsMultipleAnswers: true);
break;
case "/reaction":
await bot.SetMessageReactionAsync(msg.Chat, msg.MessageId, [""], false);
await bot.SetMessageReaction(msg.Chat, msg.Id, [""], false);
break;
}
}
Expand All @@ -118,12 +119,12 @@ async Task OnUpdate(Update update)

async Task OnCallbackQuery(CallbackQuery callbackQuery)
{
await bot.AnswerCallbackQueryAsync(callbackQuery.Id, $"You selected {callbackQuery.Data}");
await bot.SendTextMessageAsync(callbackQuery.Message!.Chat, $"Received callback from inline button {callbackQuery.Data}");
await bot.AnswerCallbackQuery(callbackQuery.Id, $"You selected {callbackQuery.Data}");
await bot.SendMessage(callbackQuery.Message!.Chat, $"Received callback from inline button {callbackQuery.Data}");
}

async Task OnPollAnswer(PollAnswer pollAnswer)
{
if (pollAnswer.User != null)
await bot.SendTextMessageAsync(pollAnswer.User.Id, $"You voted for option(s) id [{string.Join(',', pollAnswer.OptionIds)}]");
await bot.SendMessage(pollAnswer.User.Id, $"You voted for option(s) id [{string.Join(',', pollAnswer.OptionIds)}]");
}
2 changes: 1 addition & 1 deletion FSharp.Example/FSharp.Example.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Telegram.Bot" Version="21.3.0" />
<PackageReference Include="Telegram.Bot" Version="22.0.0" />
</ItemGroup>
</Project>
24 changes: 12 additions & 12 deletions FSharp.Example/Services/Internal/UpdateHandlerFuncs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ open FSharp.Example.Util
module UpdateHandlerFuncs =
module private BotTextMessages =
let sendInlineKeyboard (botClient:ITelegramBotClient) (logger: ILogger) (cts: CancellationToken) (message:Message) =
botClient.SendChatActionAsync(
botClient.SendChatAction(
chatId = message.Chat.Id,
action = ChatAction.Typing,
cancellationToken = cts) |> Async.AwaitTask |> ignore
Expand All @@ -46,7 +46,7 @@ module UpdateHandlerFuncs =
}
}

botClient.SendTextMessageAsync(
botClient.SendMessage(
chatId = message.Chat.Id,
text = "Choose",
replyMarkup = InlineKeyboardMarkup(inlineKeyboard),
Expand All @@ -64,23 +64,23 @@ module UpdateHandlerFuncs =
},
ResizeKeyboard = true)

botClient.SendTextMessageAsync(
botClient.SendMessage(
chatId = message.Chat.Id,
text = "Choose",
replyMarkup = replyKeyboardMarkup,
cancellationToken = cts)
|> Async.AwaitTask |> Async.Ignore

let removeKeyboard (botClient:ITelegramBotClient) (logger: ILogger) (cts: CancellationToken) (message:Message) =
botClient.SendTextMessageAsync(
botClient.SendMessage(
chatId = message.Chat.Id,
text = "Removing keyboard",
replyMarkup = ReplyKeyboardRemove(),
cancellationToken = cts)
|> Async.AwaitTask |> Async.Ignore

let sendFile (botClient:ITelegramBotClient) (logger: ILogger) (cts: CancellationToken) (message:Message) =
botClient.SendChatActionAsync(
botClient.SendChatAction(
message.Chat.Id,
ChatAction.UploadPhoto,
cancellationToken = cts)
Expand All @@ -94,7 +94,7 @@ module UpdateHandlerFuncs =
InputFileStream(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read),
fileName)

botClient.SendPhotoAsync(
botClient.SendPhoto(
chatId = message.Chat.Id,
photo = inputStream,
caption = "Nice Picture",
Expand All @@ -109,7 +109,7 @@ module UpdateHandlerFuncs =
},
ResizeKeyboard = true)

botClient.SendTextMessageAsync(
botClient.SendMessage(
chatId = message.Chat.Id,
text = "Who or Where are you?",
replyMarkup = requestReplyKeyboard,
Expand All @@ -120,7 +120,7 @@ module UpdateHandlerFuncs =
let inlineKeyboard =
InlineKeyboardMarkup (InlineKeyboardButton.WithSwitchInlineQueryCurrentChat("Inline Mode"))

botClient.SendTextMessageAsync(
botClient.SendMessage(
chatId = message.Chat.Id,
text = "Press the button to start Inline Query",
replyMarkup = inlineKeyboard,
Expand All @@ -141,7 +141,7 @@ module UpdateHandlerFuncs =
"/inline_mode - send keyboard with inline query\n" +
"/throw - Simulate a failed bot message handler\n"

botClient.SendTextMessageAsync(
botClient.SendMessage(
chatId = message.Chat.Id,
text = usage,
replyMarkup = ReplyKeyboardRemove(),
Expand Down Expand Up @@ -169,9 +169,9 @@ module UpdateHandlerFuncs =
| _ -> async { return () }

let botOnCallbackQueryReceived (botClient:ITelegramBotClient) (logger: ILogger) (cts: CancellationToken) (query:CallbackQuery) =
botClient.AnswerCallbackQueryAsync(query.Id, $"Received {query.Data}") |> Async.AwaitTask |> ignore
botClient.AnswerCallbackQuery(query.Id, $"Received {query.Data}") |> Async.AwaitTask |> ignore

botClient.SendTextMessageAsync(
botClient.SendMessage(
chatId = query.Message.Chat.Id,
text = $"Received {query.Data}",
cancellationToken = cts) |> Async.AwaitTask |> Async.Ignore
Expand All @@ -188,7 +188,7 @@ module UpdateHandlerFuncs =
inputMessageContent = InputTextMessageContent("hello"))
}

botClient.AnswerInlineQueryAsync(inlinequery.Id,
botClient.AnswerInlineQuery(inlinequery.Id,
results |> Seq.cast,
isPersonal = true,
cacheTime = 0,
Expand Down
2 changes: 1 addition & 1 deletion FSharp.Example/Services/ReceiverService.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type ReceiverService<'T when 'T :> IUpdateHandler>(botClient: ITelegramBotClient
)

try
let! me = botClient.GetMeAsync(cts) |> Async.AwaitTask
let! me = botClient.GetMe(cts) |> Async.AwaitTask
let username =
match me.Username with
| null -> "My Awesome Bot"
Expand Down
2 changes: 1 addition & 1 deletion InlineQueries/InlineQueries.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Telegram.Bot" Version="21.7.1" />
<PackageReference Include="Telegram.Bot" Version="22.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions InlineQueries/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// start the bot
using var cts = new CancellationTokenSource();
var bot = new TelegramBotClient(token, cancellationToken: cts.Token);
var me = await bot.GetMeAsync();
var me = await bot.GetMe();
bot.OnUpdate += OnUpdate;

Console.WriteLine($"Start listening for @{me.Username}");
Expand Down Expand Up @@ -73,7 +73,7 @@ async Task OnInlineQuery(ITelegramBotClient botClient, InlineQuery inlineQuery)
});
index++;
}
await botClient.AnswerInlineQueryAsync(inlineQuery.Id, results);
await botClient.AnswerInlineQuery(inlineQuery.Id, results);
}

// for this method to be called, you need to enable "Inline feedback" in BotFather (100% if you want to know all your users choices)
Expand Down
Loading

0 comments on commit e5b59e2

Please sign in to comment.