Skip to content

Instantly share code, notes, and snippets.

@ReubenBond
Created August 26, 2014 13:45
Show Gist options
  • Save ReubenBond/c8406be708057c3bb14e to your computer and use it in GitHub Desktop.
Save ReubenBond/c8406be708057c3bb14e to your computer and use it in GitHub Desktop.
Nexmo send SMS
public async Task SendSms(string phoneNumber, string body)
{
// Sanitize the phone number.
phoneNumber = Regex.Replace(phoneNumber, "[^0-9]", string.Empty);
// Build the request.
var builder = new UriBuilder("https://rest.nexmo.com/sms/json");
var parameters = new Dictionary<string, string>
{
{ "api_key", this.apiKey },
{ "api_secret", this.apiSecret },
{ "from", this.smsSenderName },
{ "to", phoneNumber },
{ "text", body }
};
builder.Query = string.Join("&", parameters.Select(_ => string.Format("{0}={1}", HttpUtility.UrlEncode(_.Key), HttpUtility.UrlEncode(_.Value))));
// Send the request and log the result.
var request = builder.Uri;
var result = await this.webClient.DownloadStringTaskAsync(request);
this.log.Info("{0} -> {1}", request, result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment