4

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

napi15
  • 2,354
  • 2
  • 31
  • 55
  • 1
    Their docs seem pretty straight forward. https://www.twilio.com/docs/quickstart/csharp/sms/sending-via-rest – Nico Mar 06 '17 at 05:46
  • 1
    So, from where do you want to take the Twilio 'ssid' and 'token', from text boxes or from environment variables? – Alex Baban Mar 06 '17 at 08:19

2 Answers2

5

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 
napi15
  • 2,354
  • 2
  • 31
  • 55
3

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;
    }
tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83
  • Thank you for suggesting this , but as I mentioned , there is no error captured even in the try/catch bloc I can't just understand why it's not being sent – napi15 Mar 06 '17 at 07:24
  • 1
    @napi15 Check if the updated code helps you in any way. – tRuEsAtM Mar 06 '17 at 18:07
  • 1
    Yes , it helped me and the problem was that Twilio.SendMessage() ; do not work for no idea why , but when I used the var message = MessageResource.Create(to:Number ,From:Number :FromNumber, body:Message.toString(); .............the message went threw .....So my conclution was outdated code and outdated library are the reason – napi15 Mar 06 '17 at 19:52