# SMS integration with Twilio

## The Workflow

1. Your IoT device sends an outgoing message&#x20;
2. The message is captured by the webhook (defined in Monogoto integration)
3. The webhook is defined in Twilio as a service with a function that triggers SMS send to any device&#x20;

## 1. Use Twilio Functions for the Webhook

Twilio Functions is a serverless environment that allows you to write code to process incoming webhooks without hosting your own server.

### **Steps:**

1. Log in to your Twilio account.
2. Upgrade your account (to send SMS to private numbers - it's mandatory)
3. Navigate to Functions & Assets > Services.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXde0W8zjql_BXRm5Y5aKzC0a6_B3rSj9fKDIqHT7CQFFOyWOU6Rqm-O9hcv_xuoIhkV4H5rrYuF6pgrLaEhJ2MBWHFyccXckOGd2s3MP5TNUxVXrMbAgSBh8Nx0-XQB2ojyoMBqZA?key=Lkh41Y1Exc_xMhEDk43mO9mA" alt=""><figcaption></figcaption></figure>

4. Create a new service and add new Function within it.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXc47IvDD5SfzNAyKmgVYEUf57XtlExx3GoDOn-ADgt7NJWLIO2UbVOAqxSSwQZyf_7pM9yh1dwkC8wgy6mdy6EOVdhS8Nz6fAUwyI3OPIusQfkkhAN2AiaCdw-qz2NnIVMOnwn_?key=Lkh41Y1Exc_xMhEDk43mO9mA" alt=""><figcaption></figcaption></figure>

5\. Add function (press on add and edit the name of the path)

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXeKqpqwQ1volLkNTyEZSQxcD6ERgtSjtU_8sd1Lc_VOgA-8ItQq2vIzbLewXLhdUYhkZpKn4fdjENrmqAZo59V0NkI_3RoNQgH9HRiSFcN2bdLWuzsvmafyxjAMOVrpo_Fy7ylkqA?key=Lkh41Y1Exc_xMhEDk43mO9mA" alt=""><figcaption></figcaption></figure>

6. It will generate a default code. Overwrite it with the following code for the Function:

```
exports.handler = async function (context, event, callback) {
    const client = context.getTwilioClient();


    // Your mobile number and Twilio phone number
    const yourMobileNumber = '+1234567890'; // Replace with your mobile number
    const twilioNumber = '+1987654321';    // Replace with your Twilio number


    try {
        // Log the incoming webhook data
        console.log('Received webhook:', event);


        // Forward the message to your mobile number
        const message = await client.messages.create({
            body: `IoT Alert: ${event.text  || 'No message received'}`,
            from: twilioNumber,
            to: yourMobileNumber,
        });


        console.log(`Message sent: ${message.sid}`);
        callback(null, 'Message forwarded successfully!');
    } catch (error) {
        console.error('Error forwarding message:', error);
        callback(error);
    }
};
```

#### Modify the MSISDNs&#x20;

Destination MSISDN:

```
 const yourMobileNumber = '+972542205056'; // Replace with your mobile number
```

From MSISDN (must be a Twillio number - look in your account for your number) - you can find it in the account dashboard.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXc8Wge7soO7PHUAqslUhCZfb4e_6AfMem1FcEQqAyGte8sEg_rWVXao7tr7jbF6B9cjUjd_LT-C99vjFcdWxvGWV9pzC7PsVZwkhs1BU3SoBVXVJAR2orZbp_b_apIR50DGIBicgQ?key=Lkh41Y1Exc_xMhEDk43mO9mA" alt=""><figcaption></figcaption></figure>

```
    const twilioNumber = '+12405234667';    // Replace with your Twilio number
```

7. Make sure that your function is globally available - tap on the lock icon and modify from protected to global.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcprDSZpD01FO2HuPIvPcmQdzO9xmZjdnm_3XrVBmko87b_eUvtYRNoAvemO5I9Doxeh9gOtE01sp4sK8um0q7086VyWiXRrnnDJ9dyn5HBjZNHq_EhFxUmmmfK0yFYteUPr4jNGg?key=Lkh41Y1Exc_xMhEDk43mO9mA" alt=""><figcaption></figcaption></figure>

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcyN85QkdhuzX1lOOCGsYLmqlwQodbKslyrCZHTDiXDopBppvHHl02TSCzDZUHAMNDEl-k-4GDTgxamV1MxmFkVJzDMwS5lSNelPk9hFmcQBrNgGhlhR1dM3pArsBEZpl6aVwPH?key=Lkh41Y1Exc_xMhEDk43mO9mA" alt=""><figcaption></figcaption></figure>

8. Save & Deploy the Function and note the generated URL.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfJ5LrMGIbq7ex9ekOBkytv72povX2Ye07p12wKOqljeWjJoy9vHwDS31ZfBJajq_HGZWyxPOyOZW5cx8Co57m7nNfvnCB5pluqMAPy-u3Kg8-J86yUVGL9hUJiAYb7UNnqZHfX?key=Lkh41Y1Exc_xMhEDk43mO9mA" alt=""><figcaption></figcaption></figure>

***

## 2. Validate that your destination number is allowed by setting the geolocation permission in your account

<https://console.twilio.com/us1/develop/sms/settings/geo-permissions>

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXf5BNRbVKFkENDrc_s-vhqPzFyL8EuHqSjug0Qld5NaboM7z43jH9e0l_I_qTrfAI0QCX8sQfzL6gNm6hpxYEEemgW7AJCZVNxmT3tqguCQQCH-fZ90g8DTEl8BFCSPxFaSjg_8IQ?key=Lkh41Y1Exc_xMhEDk43mO9mA" alt=""><figcaption></figcaption></figure>

## 3. Point Your Webhook to the Twilio Function

Press on "copy URL" to get your Twilio URL - should be like: `https://sendsms-6806.twil.io/sendSMS`

Use this webhook URL in Monogoto hub webhook integration&#x20;

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXch2Y_1uIKMFoXKxG2Wlm7JIr3ip2SRtxJag2sr0BHDNXjcq1e5xGnaOo8X6rojdLs6EjE9oFJJAhRCLW-smwoMhsKa7YJS4of9qdq5Hl2TKwqJpoZEygWU50_2OFsEYBnptT0j?key=Lkh41Y1Exc_xMhEDk43mO9mA" alt=""><figcaption></figcaption></figure>

View all messages sent in Twilio<br>

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXf1CO7fTKfjUB3MPkLV2el-4zsXcu7xlAs0BbdlVY2ZVAgzXCu66oKgpb4eM_RyGvjxpuzmv6yzkHLWqIAlKucrPxVN8J6LVXSWJ3tNy83KiLwrAqmPqnhfY3gz7HCl-MV1snteng?key=Lkh41Y1Exc_xMhEDk43mO9mA" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.monogoto.io/hub/faq/sms-integration-with-twilio.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
