1 module telega.telegram.webhook;
2 
3 import std.typecons : Nullable;
4 import telega.botapi : BotApi, TelegramMethod, HTTPMethod;
5 import telega.telegram.basic : InputFile;
6 
7 struct WebhookInfo
8 {
9     string   url;
10     bool     has_custom_certificate;
11     uint     pending_update_count;
12     Nullable!uint     last_error_date;
13     Nullable!string   last_error_message;
14     Nullable!uint     max_connections;
15     Nullable!(string[]) allowed_updates;
16 }
17 
18 struct SetWebhookMethod
19 {
20     mixin TelegramMethod!"/setWebhook";
21 
22     string             url;
23     Nullable!InputFile certificate;
24     uint               max_connections;
25     string[]           allowed_updates;
26 }
27 
28 struct DeleteWebhookMethod
29 {
30     mixin TelegramMethod!"/deleteWebhook";
31 }
32 
33 struct GetWebhookInfoMethod
34 {
35     mixin TelegramMethod!("/getWebhookInfo", HTTPMethod.GET);
36 }
37 
38 bool setWebhook(BotApi api, string url)
39 {
40     SetWebhookMethod m = {
41         url : url
42     };
43 
44     return setWebhook(api, m);
45 }
46 
47 bool setWebhook(BotApi api, ref SetWebhookMethod m)
48 {
49     return api.callMethod!(bool, SetWebhookMethod)(m);
50 }
51 
52 bool deleteWebhook(BotApi api)
53 {
54     DeleteWebhookMethod m = DeleteWebhookMethod();
55 
56     return api.callMethod!(bool, DeleteWebhookMethod)(m);
57 }
58 
59 WebhookInfo getWebhookInfo(BotApi api)
60 {
61     GetWebhookInfoMethod m = GetWebhookInfoMethod();
62 
63     return api.callMethod!(WebhookInfo, GetWebhookInfoMethod)(m);
64 }
65 
66 unittest
67 {
68     class BotApiMock : BotApi
69     {
70         this(string token)
71         {
72             super(token);
73         }
74 
75         T callMethod(T, M)(M method)
76         {
77             T result;
78 
79             logDiagnostic("[%d] Requesting %s", requestCounter, method.name);
80 
81             return result;
82         }
83     }
84 
85     auto api = new BotApiMock(null);
86 
87     api.setWebhook("https://webhook.url");
88     api.deleteWebhook();
89     api.getWebhookInfo();
90 }