Skip to content

Instantly share code, notes, and snippets.

@mobernberger
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mobernberger/cdd0eb1cbadfe83c6990 to your computer and use it in GitHub Desktop.
Save mobernberger/cdd0eb1cbadfe83c6990 to your computer and use it in GitHub Desktop.
netmftwilio
This is a quick implementation of the Twilio REST Api to send a message from the .NET Micro Framework.
You have the TwilioAccount Class, then the TwilioClient itself, which takes the parameter for the FROM Number, TO Number and the Body. The final File is a quick Test implementation.
var account = new TwilioAccount("<Your Account SID>", "<Your Auth Token>");
var twilio = new TwilioClient(account);
twilio.SendSmsMessage("<Your mobile Twilio Number>", "<The target Number>", "Test Message with body");
public class TwilioAccount
{
public string AccountSid { get; private set; }
public string AuthToken { get; private set; }
public TwilioAccount(string accountSid, string authToken)
{
AccountSid = accountSid;
AuthToken = authToken;
}
}
public class TwilioClient
{
#region fields
private readonly TwilioAccount _account;
private readonly string _apiAddress = "";
#endregion
public TwilioClient(TwilioAccount account)
{
_account = account;
_apiAddress = "https://api.twilio.com/2010-04-01/Accounts/" + _account.AccountSid + "/Messages.json";
}
public bool SendSmsMessage(string from, string to, string message)
{
var request = (HttpWebRequest)WebRequest.Create(_apiAddress);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Credentials = new NetworkCredential(_account.AccountSid, _account.AuthToken);
var body = "From=" + HttpUtility.UrlEncode(from) + "&To=" + HttpUtility.UrlEncode(to) + "&Body=" +
HttpUtility.UrlEncode(message);
var buffer = Encoding.UTF8.GetBytes(body);
request.ContentLength = buffer.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
}
var response = (HttpWebResponse) request.GetResponse();
return response.StatusCode == HttpStatusCode.Created;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment