Sample code for 30+ languages & platforms
C# Requires Chilkat v11.0.0+

Schedule or cancel an SMS with Twilio

See more Twilio Examples

Live messaging operation. Choose one action per run. Scheduling requires an SMS-capable sender in a Twilio Messaging Service. Supply its MG SID, a recipient in E.164 format, and a send time from 15 minutes to 35 days in the future. The example uses UTC one hour from now. Twilio will send the scheduled message unless it is canceled in time.

Save the returned Message SID. To cancel later, select the cancel action and supply that SID under the same account. Check the returned status. If the message has already left the scheduled state, fetch its current status.

Twilio documentation

Chilkat C# Downloads

C#
bool success = false;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Load an API key for the Twilio account from private application configuration.
Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
http.BasicAuth = true;
http.Login = "YOUR_API_KEY_SID";
http.Password = "YOUR_API_KEY_SECRET";

Chilkat.StringBuilder url = new Chilkat.StringBuilder();
url.Append("https://api.twilio.com/2010-04-01/Accounts/");
url.Append("YOUR_ACCOUNT_SID");
url.Append("/Messages");
//  Set this to true on a later run to cancel the saved message.
bool cancelMessage = false;
Chilkat.HttpRequest req = new Chilkat.HttpRequest();
Chilkat.CkDateTime sendTime = new Chilkat.CkDateTime();
req.HttpVerb = "POST";
req.ContentType = "application/x-www-form-urlencoded";
int expectedHttp = 201;
string expectedStatus = "scheduled";
if (cancelMessage != true) {
    bool timeOk = sendTime.SetFromCurrentSystemTime();
    if (timeOk != true) {
        Debug.WriteLine(sendTime.LastErrorText);
        return;
    }

    timeOk = sendTime.AddSeconds(3600);
    if (timeOk != true) {
        Debug.WriteLine(sendTime.LastErrorText);
        return;
    }

    req.AddParam("MessagingServiceSid","YOUR_MESSAGING_SERVICE_SID");
    req.AddParam("To","RECIPIENT_E164_NUMBER");
    req.AddParam("Body","Scheduled message.");
    req.AddParam("ScheduleType","fixed");
    req.AddParam("SendAt",sendTime.GetAsTimestamp(false));
}
else {
    //  Update the individual message. This branch never creates another message.
    url.Append("/");
    url.Append("SAVED_SCHEDULED_MESSAGE_SID");
    req.AddParam("Status","canceled");
    expectedHttp = 200;
    expectedStatus = "canceled";
}

url.Append(".json");
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();
Chilkat.JsonObject reply = new Chilkat.JsonObject();

success = http.HttpReq(url.GetAsString(),req,resp);
if (resp.StatusCode == 0) {
    Debug.WriteLine("No HTTP response from Twilio.");
    return;
}

bool parsed = reply.Load(resp.BodyStr);
if ((success != true) || (resp.StatusCode != expectedHttp)) {
    Debug.WriteLine("Twilio returned HTTP " + Convert.ToString(resp.StatusCode));
    if ((parsed == true) && (reply.JsonTypeOf("code") == 2)) {
        Debug.WriteLine("Twilio error: " + Convert.ToString(reply.IntOf("code")) + "  " + reply.StringOf("message"));
    }

    return;
}

if (parsed != true) {
    Debug.WriteLine("Expected a JSON response from Twilio.");
    return;
}

if ((reply.JsonTypeOf("sid") != 1) || (reply.JsonTypeOf("status") != 1)) {
    Debug.WriteLine("The response is missing the message SID or status.");
    return;
}

Debug.WriteLine("Message SID: " + reply.StringOf("sid"));
Debug.WriteLine("Current status: " + reply.StringOf("status"));
if (reply.StringOfEquals("status",expectedStatus,true) != true) {
    Debug.WriteLine("The requested state was not confirmed. Check this SID before trying again.");
    return;
}

if (cancelMessage != true) {
    Debug.WriteLine("Save the Message SID to check or cancel the scheduled message.");
}