Building a notification tool using .NET Core, Twilio and WhatsApp messages

Building a notification tool using .NET Core, Twilio and WhatsApp messages

During the past year or so, I’ve built several things. Some are quick hacks, others I’ve spent a considerable amount of time on before writing them up. Most solutions I’ve built and shared, I still use myself. When I built the Raspberry Pi-based temperature monitoring system early this year, I didn’t imagine I’d be staring at the LEGO-housed Raspberry Pi each day for about 8 hours. It sits under my main monitor in my home office. I vowed to myself I’d steal borrow more modern building blocks from my kids and design a more aesthetic solution. Some day.

But then, I noticed that Twilio, the messaging platform, announced beta access to WhatsApp API. Skimming through their marketing material I figured I could use the Twilio API to send notifications to myself on via WhatsApp! This way, I could bypass the LED matrix display and just push a WhatsApp message for myself on events I’d find useful (such as when the temperature rises abnormally).

But why? // insert meme gif (pronounced ‘gif’) of Ryan Reynold’s here, I guess

Everyone I know uses WhatsApp. It’s good enough, a bit like Spotify is for music – so there is little incentive to go out looking for something better. And did I mention everyone uses it already? So you’ll reach people easily when on the go. I’ve shifted plenty of my discussions to the Signal app recently, but trying to convince your parents to switch is akin to learning Swedish in 12 years. Simply impossible.

I rapidly formulated an idea in my mind – I would replace the LED matrix display portion of my Raspberry Pi solution, which shows me the temperature of my hardware cabinet. Instead, I’d still use the Raspberry Pi to collect the temperature data but I’d push that data directly to my phone via WhatsApp. I could even trigger the notification on WhatsApp (to avoid spamming myself) when the temperature rises too high (or drops too low, as I live in Finland it gets cold in the winter).

Getting access to Twilio and the WhatsApp API

I signed up on Twilio (here), and while it was processing my sign-up form I launched Visual Studio 2019. I couldn’t wait to get building my solution! Twilio also asks for your preferences so the dashboard gets personalized for you – I chose C# and WhatsApp, respectively. It’s worth noting that Twilio did not ask for my credit card in any point, which I highly appreciate at this point.

I then spent a few minutes skimming through the API docs. Twilio has amazing docs – sample code is readily available for C#, Python, PHP and even for cURL!

The C# quickstart is clean and simple. First, you’ll need to activate a sandbox for testing the API. You can do that here. You’ll also need to sign up on your mobile device to your sandbox by sending a message to the virtual phone number Twilio is exposing for you.

I was way too excited at this point, as the message I needed to send was join gold-just but I read that as join gold-dust because in my mind that made perfect sense.

The second time I received an error I realized I actually need to check if I did a typo.

Okay, moving on.

Once the sandbox is created, you’ll need to gather your Account SID and Auth Token. They are visible from the Twilio Console.

Armed with these two values, I finally get to do a bit of code!

I created a new .NET Core C# project for a command-line tool, and quickly added the Twilio NuGet package with Install-Package Twilio.

Twilio docs show a skeleton implementation of how to send messages via the WhatsApp provider, so I simply re-used that:

const string accountSid = "AC5ab3c<snip>";
const string authToken = "your_auth_token";

TwilioClient.Init(accountSid, authToken);

var message = MessageResource.Create(
	body: "Hello there!",
	from: new Twilio.Types.PhoneNumber("whatsapp:+14155238886"),
	to: new Twilio.Types.PhoneNumber("whatsapp:+15005550006")
);

Console.WriteLine(message.Sid);

Some examples I saw omitted the whatsapp: prefix from the PhoneNumber type, and that caused all sorts of issues.

I fired off a few test messages, and was delighted to see them ping on my phone and the WhatsApp desktop app:

I verified at this point, that Azure Logic Apps doesn’t yet support the Twilio’s WhatsApp API. So all I had to do now was to wire-up this simple tool to my existing Azure Function that receives temperatures from my Raspberry Pi via Azure IoT Hub.

The problem

Except, it doesn’t work. As I was using the sandbox of Twilio’s WhatsApp API, I started digging around the docs to see how to break away from the constraints of the sandbox. In order to do this, one needs to connect the Twilio number to a WhatsApp Business Profile. Which is currently invite-only. And also requires an active business operating on the Facebook advertising platform, so that WhatsApp can verify the business need. Further guidance can be found here.

I guess my Raspberry Pi-based temperature notifying solution isn’t verifiable. I also left Facebook (but not Messenger or WhatsApp) back in 2019, and I’m hesitant to go back just for this.

Yet, this same hurdle is also what’s protecting us from indefinite WhatsApp spam. I just wish there was a way for me to spam myself outside the Twilio sandbox. It’s both surprising and peculiar that when everything has an API, WhatsApp survives so well (with 1.5 Billion users) without one.

In conclusion

From high energy and excitement to a full stop and a bit of Netflix to close the day. I truly wanted to build this solution, and while it was fun to tinker a bit with the sandbox and the WhatsApp API, I felt so much potential is just beyond the reach of my fingertips.

So, I’ll continue using my previous solution until something better comes along!

Also, next time I’ll read the instructions first.