Cronhooks Blog

Replace your cron jobs with webhooks

Schedule Whatsapp messages and SMS using Twilio SDK

October 29, 2021

Whatsapp is one of vital parts of communication for this millenia. Well sending messages is pretty simple but it when it comes to scheduling Whatsapp messages then it becomes much more than simple, on top of that if those scheduled messages need to be sent at specific times of different timezones than you start loosing some of your hairs.

So lets see how we can make it simpler and easier using Cronhooks.

1. Getting started

a. A Cronhooks account. You can create a free account here.
b. Twilio Account

2. Implementing the webhook endpoint in your API / web application

We will use aspnet core but this logic is pretty straight forward and can be implmented in any technology.

namespace Controllers
{
    public class WebhooksController : Controller
    {
        public WebhooksController()
        {

        }

        [HttpPost]
        public async Task<IActionResult> SendWhatsappMessage(MessageRequest request)
        {
            try
            {
                // Find your Account SID and Auth Token at twilio.com/console
                // and set the environment variables. See http://twil.io/secure
                string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
                string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");

                TwilioClient.Init(accountSid, authToken);

                var message = MessageResource.Create(
                    body: request.Message,
                    from: new Twilio.Types.PhoneNumber("whatsapp:+14155238886"),
                    to: new Twilio.Types.PhoneNumber($"whatsapp:{request.Phone}")
                );

                return Ok();
            }
            catch(Exception ex)
            {
                return Problem();
            }
        }

        [HttpPost]
        public async Task<IActionResult> SendSMS(MessageRequest request)
        {
            try
            {
                // Find your Account SID and Auth Token at twilio.com/console
                // and set the environment variables. See http://twil.io/secure
                string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
                string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");

                TwilioClient.Init(accountSid, authToken);

                var message = MessageResource.Create(
                    body: request.Message,
                    from: new Twilio.Types.PhoneNumber("+14155238886"),
                    to: new Twilio.Types.PhoneNumber($"{request.Phone}")
                );

                return Ok();
            }
            catch(Exception ex)
            {
                return Problem();
            }
        }
    }

    public class MessageRequest
    {
        public string Phone { get; set;}
        public string Message { get; set;}
    }
}

3. Create a schedule in Cronhooks

Now all setup from your side. Login into Cronhooks app and create new schedule. You can also use Cronhooks API to create schedules but we will use web app for this tutorial.

Provide a title for your understanding, URL of your webhook, timezone, time and payload. Thats all, you are all set. Now you will recieve a message on Whatsapp at given time according to timezone.

schedule-whatsapp-messages-with-twilio

Now you can schedule as much as Whatsapp messages as you want using one webhook.

You can schedule SMS exactly same way just replace your Whatsapp endpoint with sms endpoint like https://yourdomain.com/webhooks/SendSMS.

Thats all for now. There will be more articles for scheduling using different APIs.

Thanks,
Rameez Raja
@mrameez_raja