|
| 1 | +using System.Security.Cryptography; |
| 2 | +using System.Text; |
1 | 3 | using Termii; |
2 | 4 |
|
| 5 | +var action = Environment.GetEnvironmentVariable("TERMII_EXAMPLE_ACTION"); |
| 6 | + |
| 7 | +if (string.Equals(action, "webhook-signature", StringComparison.OrdinalIgnoreCase)) |
| 8 | +{ |
| 9 | + const string payload = """{"type":"delivery_report","message_id":"msg-123","status":"delivered"}"""; |
| 10 | + const string secretKey = "example-webhook-secret"; |
| 11 | + var signature = CreateWebhookSignature(payload, secretKey); |
| 12 | + var verified = TermiiWebhookSignature.Verify(payload, signature, secretKey); |
| 13 | + |
| 14 | + Console.WriteLine($"Webhook signature verified: {verified}"); |
| 15 | + return; |
| 16 | +} |
| 17 | + |
3 | 18 | var apiKey = Environment.GetEnvironmentVariable("TERMII_API_KEY"); |
4 | 19 |
|
5 | 20 | if (string.IsNullOrWhiteSpace(apiKey)) |
6 | 21 | { |
7 | | - Console.WriteLine("Set TERMII_API_KEY to run the Termii examples."); |
| 22 | + Console.WriteLine("Set TERMII_API_KEY to run live Termii examples."); |
| 23 | + Console.WriteLine("Set TERMII_EXAMPLE_ACTION=webhook-signature to run the offline webhook signature example."); |
8 | 24 | return; |
9 | 25 | } |
10 | 26 |
|
|
15 | 31 |
|
16 | 32 | Console.WriteLine($"Termii client configured for {client.Options.BaseUrl}."); |
17 | 33 |
|
18 | | -var action = Environment.GetEnvironmentVariable("TERMII_EXAMPLE_ACTION"); |
19 | 34 | var phoneNumber = Environment.GetEnvironmentVariable("TERMII_EXAMPLE_PHONE_NUMBER"); |
20 | 35 |
|
21 | 36 | if (string.Equals(action, "balance", StringComparison.OrdinalIgnoreCase)) |
|
26 | 41 | return; |
27 | 42 | } |
28 | 43 |
|
| 44 | +if (string.Equals(action, "phonebooks-list", StringComparison.OrdinalIgnoreCase)) |
| 45 | +{ |
| 46 | + var phonebooks = await client.Campaigns.GetPhonebooksAsync(new GetPhonebooksRequest |
| 47 | + { |
| 48 | + Page = 0, |
| 49 | + Size = 10, |
| 50 | + }); |
| 51 | + |
| 52 | + Console.WriteLine($"Phonebooks returned: {phonebooks.Content.Count}"); |
| 53 | + return; |
| 54 | +} |
| 55 | + |
| 56 | +if (string.Equals(action, "campaigns-list", StringComparison.OrdinalIgnoreCase)) |
| 57 | +{ |
| 58 | + var campaigns = await client.Campaigns.GetCampaignsAsync(new GetCampaignsRequest |
| 59 | + { |
| 60 | + Page = 0, |
| 61 | + Size = 10, |
| 62 | + }); |
| 63 | + |
| 64 | + Console.WriteLine($"Campaigns returned: {campaigns.Content.Count}"); |
| 65 | + return; |
| 66 | +} |
| 67 | + |
29 | 68 | if (string.Equals(action, "number-message", StringComparison.OrdinalIgnoreCase)) |
30 | 69 | { |
31 | 70 | if (string.IsNullOrWhiteSpace(phoneNumber)) |
|
44 | 83 | return; |
45 | 84 | } |
46 | 85 |
|
47 | | -Console.WriteLine("Set TERMII_EXAMPLE_ACTION=balance or TERMII_EXAMPLE_ACTION=number-message to run a live example."); |
| 86 | +Console.WriteLine("Set TERMII_EXAMPLE_ACTION to one of: balance, phonebooks-list, campaigns-list, number-message, webhook-signature."); |
| 87 | + |
| 88 | +static string CreateWebhookSignature(string payload, string secretKey) |
| 89 | +{ |
| 90 | + using var hmac = new HMACSHA512(Encoding.UTF8.GetBytes(secretKey)); |
| 91 | + var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(payload)); |
| 92 | + var builder = new StringBuilder(hash.Length * 2); |
| 93 | + |
| 94 | + foreach (var value in hash) |
| 95 | + { |
| 96 | + builder.Append(value.ToString("x2")); |
| 97 | + } |
| 98 | + |
| 99 | + return builder.ToString(); |
| 100 | +} |
0 commit comments