-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: internal update processing redesign * feat: initial webhook implementation * refactor: IUpdateSource -> IUpdateTarget, move queue to instance also return option to turn webhooks on/off in config * Properly stop * fix: wait until update thread stops & code cleanup * feat(webhook): secret token support --------- Co-authored-by: Alexander <[email protected]>
- Loading branch information
Showing
21 changed files
with
452 additions
and
118 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
17 changes: 17 additions & 0 deletions
17
TGBotFramework/BotFramework.Webhook/BotFramework.Webhook.csproj
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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>latestMajor</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Watson" Version="6.0.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\BotFramework\BotFramework.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
18 changes: 18 additions & 0 deletions
18
TGBotFramework/BotFramework.Webhook/ServiceCollectionExtensions.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,18 @@ | ||
using BotFramework.Abstractions.UpdateProvider; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace BotFramework.Webhook; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static void AddTelegramBotWebhookUpdateProvider(this IServiceCollection collection) | ||
{ | ||
collection.AddSingleton<IWebhookProvider, WebhookUpdateProvider>(); | ||
} | ||
|
||
public static void AddTelegramBotWebhook(this IServiceCollection collection) | ||
{ | ||
collection.AddTelegramBotWebhookUpdateProvider(); | ||
collection.AddTelegramBot(); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
TGBotFramework/BotFramework.Webhook/WebhookUpdateProvider.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,85 @@ | ||
using System; | ||
using System.Net; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using BotFramework.Abstractions.UpdateProvider; | ||
using BotFramework.Config; | ||
using Microsoft.Extensions.Options; | ||
using Newtonsoft.Json; | ||
using Telegram.Bot; | ||
using Telegram.Bot.Types; | ||
using WatsonWebserver.Core; | ||
|
||
namespace BotFramework.Webhook; | ||
|
||
public class WebhookUpdateProvider : IWebhookProvider | ||
{ | ||
private const string SecretTokenHeader = "X-Telegram-Bot-Api-Secret-Token"; | ||
|
||
private readonly ITelegramBotClient _client; | ||
private readonly IUpdateTarget _updateTarget; | ||
private readonly BotConfig _config; | ||
|
||
private readonly string? _secretToken; | ||
|
||
private readonly CancellationTokenSource _canRunTokenSource = new (); | ||
|
||
public WebhookUpdateProvider(ITelegramBotClient client, IUpdateTarget updateTarget, IOptions<BotConfig> config) | ||
{ | ||
_client = client; | ||
_updateTarget = updateTarget; | ||
_config = config.Value; | ||
|
||
_secretToken = _config.Webhook.SecretToken; | ||
if (string.IsNullOrWhiteSpace(_secretToken)) | ||
{ | ||
_secretToken = null; | ||
} | ||
} | ||
|
||
public async Task StartAsync(CancellationToken token) | ||
{ | ||
token.ThrowIfCancellationRequested(); | ||
var server = new WatsonWebserver.Webserver(GetWebserverSettings(), Route); | ||
_ = server.StartAsync(_canRunTokenSource.Token); | ||
|
||
await _client.SetWebhookAsync(_config.Webhook.Url, secretToken: _secretToken, cancellationToken: token); | ||
} | ||
|
||
public async Task StopAsync(CancellationToken token) | ||
{ | ||
_canRunTokenSource.Cancel(); | ||
await _client.DeleteWebhookAsync(cancellationToken: token); | ||
} | ||
|
||
private async Task Route(HttpContextBase ctx) | ||
{ | ||
var request = ctx.Request; | ||
if (_secretToken != null && request.RetrieveHeaderValue(SecretTokenHeader) != _secretToken) | ||
{ | ||
ctx.Response.StatusCode = (int)HttpStatusCode.Forbidden; | ||
|
||
await ctx.Response.Send("Missing or invalid secret_token"); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
var obj = JsonConvert.DeserializeObject<Update>(request.DataAsString); | ||
if (obj != null) | ||
{ | ||
_updateTarget.Push(obj); | ||
} | ||
} catch(Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
} | ||
|
||
await ctx.Response.Send(); | ||
} | ||
|
||
private WebserverSettings GetWebserverSettings() | ||
{ | ||
return new WebserverSettings(_config.Webhook.Host, _config.Webhook.Port); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
TGBotFramework/BotFramework/Abstractions/UpdateProvider/IUpdateProvider.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,10 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace BotFramework.Abstractions.UpdateProvider; | ||
|
||
public interface IUpdateProvider | ||
{ | ||
Task StartAsync(CancellationToken token); | ||
Task StopAsync(CancellationToken token); | ||
} |
8 changes: 8 additions & 0 deletions
8
TGBotFramework/BotFramework/Abstractions/UpdateProvider/IUpdateTarget.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,8 @@ | ||
using Telegram.Bot.Types; | ||
|
||
namespace BotFramework.Abstractions.UpdateProvider; | ||
|
||
public interface IUpdateTarget | ||
{ | ||
public void Push(Update update); | ||
} |
6 changes: 6 additions & 0 deletions
6
TGBotFramework/BotFramework/Abstractions/UpdateProvider/IWebhookProvider.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,6 @@ | ||
namespace BotFramework.Abstractions.UpdateProvider; | ||
|
||
public interface IWebhookProvider: IUpdateProvider | ||
{ | ||
|
||
} |
Oops, something went wrong.