Last active
August 29, 2015 14:12
-
-
Save mobernberger/cdd0eb1cbadfe83c6990 to your computer and use it in GitHub Desktop.
netmftwilio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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