Reputation: 103
I'm trying to figure out how I can [update] the IncomingPhoneNumber to remove the 'Demo' Url set for Voice and Sms when a phone number is purchased.
For example my VoiceUrl is set to "https://demo.twilio.com/welcome/voice/" in the Twilio UI console. I know I can just remove it from the console but I am hoping there is a way that I can set it from a REST Service such as through the IncomingPhoneNumberResource.Update?
Upvotes: 2
Views: 384
Reputation: 7164
Update:
I was working on a PR to make this experience better and stumbled on a solution that's already in the library.
There's a type Twilio.Types.EmptyUri
which inherits from System.Uri
.
When you pass an instance of the type to update URLs on a phone number, it'll empty/null the URL field.
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
var app = Host.CreateDefaultBuilder(args).Build();
var configuration = app.Services.GetRequiredService<IConfiguration>();
var twilioAccountSid = configuration["TwilioAccountSid"];
var twilioAuthToken = configuration["TwilioAuthToken"];
TwilioClient.Init(twilioAccountSid, twilioAuthToken);
var phoneNumber = (await IncomingPhoneNumberResource.ReadAsync(
phoneNumber: new PhoneNumber(configuration["TwilioPhoneNumber"]),
limit: 1
)).Single();
await IncomingPhoneNumberResource.UpdateAsync(
phoneNumber.Sid,
voiceUrl: new EmptyUri(),
smsUrl: new EmptyUri()
);
Old answer:
As you may have noticed, you cannot remove the URL using the IncomingPhoneNumberResource.Update
method.
The default value of the voiceUrl
and smsUrl
in the Twilio .NET library is null
, so if you explicitly pass in null
, the library won't know the difference and ignore it.
Luckily, the REST API is quite intuitive, so you can write your own code to make REST API calls, or use the RestClient
that comes with the Twilio .NET library.
Here's an example of both the IncomingPhoneNumberResource.Update
that doesn't work, and the code that does work using the RestClient
.
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Twilio;
using Twilio.Http;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
var app = Host.CreateDefaultBuilder(args).Build();
var configuration = app.Services.GetRequiredService<IConfiguration>();
var twilioAccountSid = configuration["TwilioAccountSid"];
var twilioAuthToken = configuration["TwilioAuthToken"];
TwilioClient.Init(twilioAccountSid, twilioAuthToken);
var phoneNumber = (await IncomingPhoneNumberResource.ReadAsync(
phoneNumber: new PhoneNumber(configuration["TwilioPhoneNumber"]),
limit: 1
)).Single();
// DOES NOT WORK, but wish it did
// this API call goes through, but does not empty the voiceUrl or smsUrl
await IncomingPhoneNumberResource.UpdateAsync(
phoneNumber.Sid,
voiceUrl: null, // new Uri("") throws an exception
smsUrl: null // new Uri("") throws an exception
);
// this does work!
var request = new Request(
method: Twilio.Http.HttpMethod.Post,
url: $"https://api.twilio.com/2010-04-01/Accounts/{twilioAccountSid}/IncomingPhoneNumbers/{phoneNumber.Sid}.json"
);
request.AddPostParam("VoiceUrl", null);
request.AddPostParam("SmsUrl", null);
await TwilioClient.GetRestClient().RequestAsync(request);
Upvotes: 4