4
votes

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

2
Their docs seem pretty straight forward. twilio.com/docs/quickstart/csharp/sms/sending-via-rest - Nico
So, from where do you want to take the Twilio 'ssid' and 'token', from text boxes or from environment variables? - Alex Baban

2 Answers

5
votes

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 
3
votes

See if you can get any help from the below code:

/// <summary>
    /// Sends alert as SMS 
    /// </summary>
    /// <param name="details"></param>
    /// <returns></returns>
    public static Message SendSms(DeliveryDetails details)
    {
        var messageResult = new Message();
        try
        {
            if (details?.ToNumber != null)
            {
                var toNumberList = details.ToNumber.ToList();
                if (toNumberList.Count > 0)
                {
                    foreach (var toNumber in toNumberList)
                    {
                        messageResult = Twilio.SendMessage(FromNumber, toNumber, $"{details.Subject}\n\n{details.Message}");

                        if (messageResult == null)
                        {
                            logger.Error(string.Format(
                                "Error connecting to Twilio, message sending failed to {0}",
                                toNumber));
                        }
                        else if (messageResult.RestException != null)
                        {
                            logger.Error(string.Format("Twilio Error Message Description - {0}",
                                messageResult.RestException.Message));
                        }
                        else
                        {
                            logger.Info(String.Format("SMS {0} deliverd to {1}", messageResult.Body, messageResult.To));
                        }
                    }
                }
                else
                {
                    logger.Error("ToNumber List Empty");
                }
            }
            else
            {
                logger.Error("ToNumber List Null");
            }
        }
        catch (Exception e)
        {
            logger.Error(String.Format("An error occurred while sending the message\n{0}", e.Message));
        }

        return messageResult;
    }