Skip to content

Commit

Permalink
Webhook example updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Eptagone committed Oct 22, 2023
1 parent 3a0d45c commit 9463986
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,8 @@ public BotController(ILogger<BotController> logger, IConfiguration configuration
this._bot = bot;
}

[HttpGet("{webhookToken}")]
public IActionResult Get(string webhookToken)
{
if (this._configuration["Telegram:WebhookToken"] != webhookToken)
{
this._logger.LogWarning("Failed access!");
this.Unauthorized();
}
return this.Ok();
}

[HttpPost("{webhookToken}")]
public async Task<IActionResult> PostAsync(string webhookToken, [FromBody] Update update, CancellationToken cancellationToken)
[HttpPost]
public async Task<IActionResult> PostAsync([FromHeader(Name = "X-Telegram-Bot-Api-Secret-Token")] string webhookToken, [FromBody] Update update, CancellationToken cancellationToken)
{
if (this._configuration["Telegram:WebhookToken"] != webhookToken)
{
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Telegram.BotAPI;
using Telegram.BotAPI.AvailableMethods;
using Telegram.BotAPI.AvailableTypes;
using Telegram.BotAPI.GettingUpdates;

namespace HelloBotNET.Webhook
{
Expand All @@ -20,20 +19,10 @@ public HelloBotProperties(IConfiguration configuration)
{
var telegram = configuration.GetSection("Telegram"); // JSON: "Telegram"
var botToken = telegram["BotToken"]; // ENV: Telegram__BotToken, JSON: "Telegram:BotToken"

this.Api = new BotClient(botToken);
this.User = this.Api.GetMe();

this._commandHelper = new BotCommandHelper(this);

// Delete my old commands
this.Api.DeleteMyCommands();
// Set my commands
this.Api.SetMyCommands(
new BotCommand("hello", "Hello world!"));

// Delete webhook to use Long Polling
this.Api.DeleteWebhook();
}

public BotClient Api { get; }
Expand Down
4 changes: 4 additions & 0 deletions src/Telegram.BotAPI.Examples/HelloBotNET.Webhook/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License, See LICENCE in the project root for license information.

using HelloBotNET.Webhook;
using HelloBotNET.Webhook.Extensions;
using HelloBotNET.Webhook.Services;

var builder = WebApplication.CreateBuilder(args);
Expand All @@ -21,4 +22,7 @@

app.MapControllers();

// Register que webhook
app.UseTelegramWebhook();

app.Run();
11 changes: 9 additions & 2 deletions src/Telegram.BotAPI/Sugar Library/TelegramConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ namespace Telegram.BotAPI;
/// </summary>
public static class TelegramConstants
{

/// <summary>
/// Unique identifier for the anonymous bot.
/// </summary>
public const long GroupAnonymousBotId = 1087968824;
/// <summary>
/// Unique identifier for the telegram user.
/// </summary>
public const long TelegramId = 777000;
/// <summary>
/// Header name for the telegram bot api token in the request.
/// </summary>
public const string XTelegramBotApiSecretToken = "X-Telegram-Bot-Api-Secret-Token";

}

0 comments on commit 9463986

Please sign in to comment.