Knocks/BackEnd/Knoks.Framework/Services/TwilioPhoneService.cs

42 lines
1.3 KiB
C#

using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using Twilio.Clients;
using Twilio.Rest.Lookups.V1;
namespace Knoks.Framework.Services
{
public class TwilioPhoneService : IPhoneService
{
private ILogger<TwilioPhoneService> _logger;
public TwilioRestClient _twilioRestClient { get; }
public TwilioPhoneService(ILogger<TwilioPhoneService> logger, TwilioPhoneSettings settings)
{
_logger = logger;
_twilioRestClient = new TwilioRestClient(settings.AccountSid, settings.AuthToken);
}
public async Task<PhoneNumber> Lookup(string number, string countryCode = null)
{
try
{
var res = await PhoneNumberResource.FetchAsync(new Twilio.Types.PhoneNumber(number), countryCode, client: _twilioRestClient);
return new PhoneNumber
{
OriginalFormat = number,
NationalFormat = res.NationalFormat,
E164Format = res.PhoneNumber.ToString(),
CountryCode2 = res.CountryCode
};
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
return null;
}
}
}
}