Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send sms using C# and twilio API

I'm trying to create a User interface where the user can enter her credential and phone number and send messages to some recipient using Twilio API

So as explained I created my account and initialized the Authentication key and token

    private void button1_Click(object sender, EventArgs e)
    {
         string ssid = twilioSSIDBox.Text;
         string token = twilioTokenBox.Text;
         string number = twilioNumberBox.Text;


        var client = new TwilioRestClient(Environment.GetEnvironmentVariable(ssid), Environment.GetEnvironmentVariable(token));
        client.SendMessage(number, "+158965220", "Teting API message!");

    }

After multiple test (hard coding the ssid and token and number ) and documentation consulting , the message is still not being sent on the visual studio platform and I'm not receiving any error message or anything

So my question is what I am missing? Do I need a certain library that allow visual studio to be able to send sms messages?

I'm using Visual studio 2015 and windows 10 platform

Thank you

like image 281
napi15 Avatar asked Feb 04 '23 23:02

napi15


1 Answers

I end up resolving my problems

so this was not working on VS2015 Platform :

    var client = new TwilioRestClient(Environment.GetEnvironmentVariable(ssid), Environment.GetEnvironmentVariable(token));
    client.SendMessage(number, "+158965220", "Teting API message!");

Even thought I already installed the Twilio api version 4.7.2 using nuget console

I did again the following :

Install-Package Twilio

than Visual studio suggest me to add reference to the API inside the code

 using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

Thanks to the following Example

this has worked :

 TwilioClient.Init(accountSid, authToken);

        var message = MessageResource.Create(
            to: new PhoneNumber(toNumber),
            from: new PhoneNumber(FromNumber),
            body: "Hello from C#");

So my conclusion is probably an outdated library because when I used NUGET to install:

Install-Package Twilio -Version 4.7.2

I have no idea why it wasn't working

P.S: I'm not sure if it was relevant but I also installed this using NUGET console it mighed helped or it might not but I felt like I had to mention it :

Install-Package RestSharp 
like image 100
napi15 Avatar answered Feb 07 '23 12:02

napi15