How to send SMS alert with Twilio?

How to send SMS alert with an external API provider such as Twilio

Q: Is it possible to send SMS using third-party services, such as Twilio?

A: Yes, you can do that with either “Script or Program” or “Python script” monitor, see the instructions below.

1. Prerequisites

The mentioned monitor files require sample script files available from the following archive: send-sms-twilio.zip (1489 bytes).

You can use either trial (i.e., for testing purposes) or paid-for Twilio account. In either case, you should obtain source SMS phone number from Twilio; for more information, please consult with Twilio SMS-related documentation.

In the below sample monitor configuration, it is assumed that

  • you downloaded the above mentioned archive and unpacked its contents into C:\Scripts folder
  • you have registered an account with Twilio and have required data at hand (Twilio API key and SMS source phone number)

If you use another directory name to keep scripts file in, please change the name appropriately in the below instructions.

Important: sending SMS via Twilio may incur significant fees from Twilio service. Please consult their pricing section first, and make sure you only use Twilio-driven SMS alerts when absolutely necessary. Always use testing mode when checking parameters.

2. Use cURL utility to send SMS via Twilio

This method uses curl.exe binary bundled with IPHost; if you plan to use standalone curl.exe executable, edit C:\Scripts\send-sms\curl.cmd file correspondingly.

Start IPHost GUI client, proceed to “Settings > Alerts”, click “New” to create a new alert (in this example, it’s named “Twilio SMS (cURL)”; click “Add > New Simple Action > Execute program”, to open new simple action editor and enter data like below:

Send Twilio SMS via cURL

Detailed fields description:

Mode: Run program
Path: C:\Windows\System32\cmd.exe
Arguments: /c C:\Scripts\send-sms-curl.cmd ACCTID TOKEN FROM TO MESSAGE

where

ACCTID: Twilio account ID, example: AC8d5c4e4650578902345678ee1f397e4
TOKEN: Twilio auth token, example: 59401234567897d20e9b05218adfad
FROM: sending (souce) number in international format, example: +15550000000
TO: recipient number (where SMS should come to), example: +15559991122
MESSAGE: message to send, in quotes, example: “$HostName is now $NewState”

Important: please pay attention to where quotes are used, and where they are not.

Note: do not use the example values above; they won’t work. Use actual data. Note that you can use template variables (like the above: $Hostname and $NewState).

Click “OK” when finished. Now create an alerting rule and specify when to execute the “Twilio SMS (cURL)” alert.

Test the SMS sending by assigning new alerting rule to a monitor, proceeding to “Alerting > Testing” tab and clicking “Test” on corresponding state change.

Note: to run the above test from command line, open elevated cmd.exe (“Run as Administrator”) and type command like

cmd.exe /c C:\Scripts\send-sms-curl.cmd  ACCTID TOKEN FROM TO MESSAGE

Replace the parameters placeholders with their actual values, as shown above.

3. Use Python script to send SMS via Twilio

Start IPHost GUI client, proceed to “Settings > Alerts”, click “New” to create a new alert (in this example, it’s named “Twilio SMS (Python)”; click “Add > New Simple Action > Execute Python script”, to open new simple action editor and enter data like below:

Send Twilio SMS via Python script

Detailed fields description:

Path: C:\Scripts\send-sms-python.py
Arguments: ACCTID TOKEN FROM TO MESSAGE

where

ACCTID: Twilio account ID in quotes, example: “AC8d5c4e4650578902345678ee1f397e4”
TOKEN: Twilio auth token in quotes, example: “59401234567897d20e9b05218adfad”
FROM: sending (souce) number in international format, in quotes, example: “+15550000000”
TO: recipient number (where SMS should come to), in quotes, example: “+15559991122”
MESSAGE: message to send, in quotes, example: “$HostName is now $NewState”

Important: please pay attention that all fields are used with quotes.

Note: do not use the example values above; they won’t work. Use actual data. Note that you can use template variables (like the above: $Hostname and $NewState).

Click “OK” when finished. Now create an alerting rule and specify when to execute the “Twilio SMS (Python)” alert.

Test the SMS sending by assigning new alerting rule to a monitor, proceeding to “Alerting > Testing” tab and clicking “Test” on corresponding state change.

To run the above test from command line, start elevated cmd.exe (“Run as Administrator”) and type command like below

"%ProgramFiles(x86)%\IPHost Network Monitor\Python-3.6.4\python.exe" C:\Scripts\send-sms-python.py ACCTID TOKEN FROM TO MESSAGE

Replace the parameters placeholders with their actual values, as shown above.

4. References

The below documents might be useful when setting up alerts:

Note: please refer all questions on setting up trial Twilio account, gaining source phone number etc to Twilio docs/tech.support.

Related topics