Skip to content

Instantly share code, notes, and snippets.

@devinrader
Created August 1, 2014 18:22
Show Gist options
  • Save devinrader/f99aec53be10bf7c12be to your computer and use it in GitHub Desktop.
Save devinrader/f99aec53be10bf7c12be to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Twilio;
namespace ConsoleApplication17
{
class Program
{
static void Main(string[] args)
{
M m = new M();
Task task = m.Run();
task.Wait();
Console.Read();
}
}
public class M
{
public async Task Run()
{
var result1 = await SendSmsMessageAsync("3144586142", "Hello Async World");
Console.WriteLine(result1.IsSuccessful);
Console.WriteLine(result1.Message);
var result2 = M.SendSmsMessage("3144586142", "Hello World");
Console.WriteLine(result2.IsSuccessful);
Console.WriteLine(result1.Message);
}
public static async Task<SmsResponse> SendSmsMessageAsync(string phoneNumber, string message)
{
var twilio = new TwilioRestClient("AC3137d76457814a5eabf7de62f346d39a", "01e8896765b4c7798e0f9d888948a9b2");
var twilioMessage = await twilio.SendSmsMessage("+14046668238", NormalizePhoneNumber(phoneNumber), message);
return new SmsResponse
{
IsSuccessful = twilioMessage.RestException == null,
Message = twilioMessage.RestException != null ? twilioMessage.RestException.Message : "",
ApiTransactionId = twilioMessage.Sid,
ChargeAmount = twilioMessage.Price,
NormalizedPhoneNumber = twilioMessage.To
};
}
public static SmsResponse SendSmsMessage(string phoneNumber, string message)
{
var twilio = new TwilioRestClient("AC3137d76457814a5eabf7de62f346d39a", "01e8896765b4c7798e0f9d888948a9b2");
var twilioMessage = twilio.SendSmsMessage("+14046668238", NormalizePhoneNumber(phoneNumber), message).Result;
return new SmsResponse
{
IsSuccessful = twilioMessage.RestException == null,
Message = twilioMessage.RestException != null ? twilioMessage.RestException.Message : "",
ApiTransactionId = twilioMessage.Sid,
ChargeAmount = twilioMessage.Price,
NormalizedPhoneNumber = twilioMessage.To
};
}
public static string NormalizePhoneNumber(string number)
{
return number;
}
}
public class SmsResponse
{
public bool IsSuccessful { get; set; }
public string Message { get; set; }
public string ApiTransactionId { get; set; }
public decimal ChargeAmount { get; set; }
public string NormalizedPhoneNumber { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment