-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTwilioSmsSender.cs
56 lines (44 loc) · 1.92 KB
/
TwilioSmsSender.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Twilio.Clients;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
namespace Pact.Sms;
/// <summary>
/// An SMS & Voice service implementation using Twilio (https://www.twilio.com/)
/// For docs on implementing the voice endpoint, refer to: https://www.twilio.com/docs/voice/tutorials/how-to-respond-to-incoming-phone-calls-csharp
/// </summary>
public class TwilioSmsSender : ISmsSender
{
private readonly ILogger<TwilioSmsSender> _logger;
private readonly ITwilioRestClient _twilio;
private readonly SmsSettings _settings;
public TwilioSmsSender(IOptions<SmsSettings> smsSettings,
ILogger<TwilioSmsSender> logger,
ITwilioRestClient twilio)
{
_settings = smsSettings.Value;
_twilio = twilio;
_logger = logger;
}
///<inheritdoc/>
public bool SupportsVoice => true;
///<inheritdoc/>
public async Task SendVoiceAsync(string number, string callbackUrl)
{
_logger.LogInformation("> Voice Sending => {Number}, {CallbackUrl}", number, callbackUrl);
var to = new PhoneNumber(number);
var from = new PhoneNumber(_settings.From);
var call = await CallResource.CreateAsync(to, from, url: new Uri(callbackUrl), client: _twilio).ConfigureAwait(false);
_logger.LogInformation(">> Voice Sent => {Number}, {CallbackUrl}, {Sid}", number, callbackUrl, call?.Sid);
}
///<inheritdoc/>
public async Task SendSmsAsync(string number, string message)
{
_logger.LogInformation("> Sms Sending => {Number}, {Message}", number, message);
var to = new PhoneNumber(number);
var from = new PhoneNumber(_settings.From);
await MessageResource.CreateAsync(to, from: from, body: message, client: _twilio).ConfigureAwait(false);
_logger.LogInformation(">> Sms Sent => {Number}, {Message}", number, message);
}
}