<jupyter_start><jupyter_text>Twilio Notebook demo

To get started with this worksheet you will need a free [Twilio account](https://www.twilio.com/try-twilio?utm_source=devnet&utm_medium=worksheet&utm_campaign=csharp).

Next, you will need to save your account Sid and auth token, found on the home screen of the Twilio console, to your environment variables. If you are unsure of how to do this, then you can follow the steps in this handy [blog](https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html).

*Note: If you are running this notebook from MyBinder.org you will need to enter your account Sid and auth token below as environment variables won't work*

Once you have your account set up, it's time to purchase an SMS and voice enabled [Twilio number](https://www.twilio.com/console/phone-numbers/search).
You can then add your Twilio number and your own mobile telephone number to the variables `twilioNumber` and `myNumber` respectively below.<jupyter_code>var accountSid = Environment.GetEnvironmentVariable("ACCOUNT_SID");
var authToken = Environment.GetEnvironmentVariable("AUTH_TOKEN");
var twilioNumber = "";
var myNumber = "";

display("account sid: " + accountSid);<jupyter_output><empty_output><jupyter_text>If you run the code above and succefully display your account Sid, you'll know that you have successfully set up your environment variables. If you don't see your account Sid, then you may need to reboot your computer.  Send a message with Twilio

The code below will send a text message to your mobile number from your new Twilio number. We have imported the Twilio NuGet package, initialised the Twilio client and finally, sent an SMS with the message `"Ahoy from Jupyter!"` using the `MessageResourse` method.

Try updating the `body` value and send yourself something else!<jupyter_code>#r "nuget:twilio, 5.48.0"
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

public class TwilioController
{
   
    public string SendMessage()
    {
        TwilioClient.Init(accountSid, authToken);

        var message =  MessageResource.Create(
            from: new PhoneNumber(twilioNumber),
            to: new PhoneNumber(myNumber),
            body: "Ahoy from Jupyter!" );        
       
       return message.Sid;
    }
    

}

var controller = new TwilioController();

var messageSid = controller.SendMessage();

display("Message sid: " + messageSid);<jupyter_output><empty_output><jupyter_text>Make a voice call with Twilio

The code to make a voice call is very similar to the code to send an SMS. We initialise the Twilio client in the same way, but this time we call the `CallResource` method.

Note that we also need to convert our telephone numbers from type `string` to type `PhoneNumber`.

This code uses the `url` parameter to inform Twilio what it should do when the call connects to user. Follow the [URL](http://demo.twilio.com/docs/voice.xml) to see what it returns.<jupyter_code>#r "nuget:twilio, 5.48.0"
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

public class TwilioController
{

    public string MakeCall()
    {
        TwilioClient.Init(accountSid, authToken);


        var call = CallResource.Create(
            to: PhoneNumber(myNumber),
            from: PhoneNumber(twilioNumber),
            url: new Uri("http://demo.twilio.com/docs/voice.xml"));
       
       return call.Sid.ToString();
    }    

}
var controller = new TwilioController();

var callSid = controller.MakeCall();

display("Call sid: " + callSid);<jupyter_output><empty_output><jupyter_text>We can also use [TwiML](https://www.twilio.com/docs/voice/twiml) to tell Twilio what to do. In this case we would use the `twiml` parameter instead of the `url`.
Try updating the method above with the following TwiML.<jupyter_code>var call = CallResource.Create(            
            to: PhoneNumber(myNumber),
            from: PhoneNumber(twilioNumber),
            twiml: Twiml("<Response><Say>Ahoy, World!</Say></Response>");<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08734793187347932, 'fraction_numerical': 0.00535279805352798, 'mean_word_length': 3.7307249712313, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '78097', 'license': 'MIT', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1176, 'n_words': 434, 'path': 'notebooks/TwilioBasics.ipynb', 'repo_name': 'Layla-P/TwilioWorksheet', 'size': '4110'}