-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
63 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Telegram.BotAPI.Examples/HelloBotNET.Webhook/Extensions/ApplicationBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2023 Quetzal Rivera. | ||
// Licensed under the MIT License, See LICENCE in the project root for license information. | ||
|
||
using Telegram.BotAPI.AvailableTypes; | ||
using Telegram.BotAPI.AvailableMethods; | ||
using Telegram.BotAPI.GettingUpdates; | ||
|
||
namespace HelloBotNET.Webhook.Extensions; | ||
|
||
/// <summary> | ||
/// Extension methods for <see cref="IApplicationBuilder"/>. | ||
/// </summary> | ||
public static class ApplicationBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Registers the Telegram Webhook. | ||
/// </summary> | ||
/// <param name="app">The <see cref="IApplicationBuilder"/> instance this method extends.</param> | ||
/// <returns>The <see cref="IApplicationBuilder"/> instance this method extends.</returns> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
public static IApplicationBuilder UseTelegramWebhook(this IApplicationBuilder app) | ||
{ | ||
if (app is null) | ||
{ | ||
throw new ArgumentNullException(nameof(app)); | ||
} | ||
|
||
var configuration = app.ApplicationServices.GetRequiredService<IConfiguration>(); | ||
var bot = app.ApplicationServices.GetRequiredService<HelloBotProperties>(); | ||
|
||
var webhookToken = configuration["Telegram:WebhookToken"]; // ENV: Telegram__WebhookToken, JSON: "Telegram:WebhookToken" | ||
var webhookUrl = configuration["Telegram:WebhookUrl"]; // ENV: Telegram__WebhookUrl, JSON: "Telegram:WebhookUrl" | ||
|
||
// Delete my old commands | ||
bot.Api.DeleteMyCommands(); | ||
// Set my commands | ||
bot.Api.SetMyCommands( | ||
new BotCommand("hello", "Hello world!")); | ||
|
||
// Delete webhook | ||
bot.Api.DeleteWebhook(); | ||
|
||
// Set webhook | ||
bot.Api.SetWebhook(webhookUrl + "/bot", secretToken: webhookToken); | ||
|
||
return app; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters