# 04. Twilio

This section is pretty simple, we're just going to tell Twilio to forward any incoming SMS to our server on Heroku.

1. Log in to Twilio and click the "Explore Options" in the left menu\\

2. Select `Functions` in the Developer Tools section\\

3. Click the "Create Service" button and name your service whatever you want. Mine will be called `shSMS`.\\

4. Click the "Add +" button at the top and select "Add Function"\\

5. This will create a function named something like "path\_1". I renamed mine to `shSMS-forwarding`\\

6. Delete everything in the window on the right and replace it with this (change the value of `baseURL` and `enc` to match your Heroku URL/Secret\_Key)

```javascript
const axios = require('axios');

exports.handler = async (context, event, callback) => {
    const instance = axios.create({
        baseURL: 'https://shsms-heroku-server-01.herokuapp.com',
        timeout: 4000,
        headers: {
            "X-Custom-Header": "Twilio",
            "enc": "THIS_IS_YOUR_SECRET_KEY_IT_CAN_BE_ANYTHING"
        },
    });

    const mediaArr = [];
    for (const [key, value] of Object.entries(event)) {
        const isMedia = /(MediaUrl)/.test(key)
        if (isMedia) {
            mediaArr.push(value)
        }
    }

    try {
        const res = await instance.post(`/messages`, {
            date: new Date(),
            phoneNumber: event.From,
            toPhoneNumber: event.To,
            message: event.Body,
            event: event,
            mediaNum: event.NumMedia,
            attachedMedia: mediaArr
        })
                
        callback(null, res.success)
    } catch (err) {
        console.error(err);
        callback(err);
    }
};
```

1. Click "Save"\\

2. Click the "Dependencies" option (located just above the Deploy All button)\\

3. In Module, enter `axios` and in Version, enter `0.21.4`\\

4. Click the "Deploy All" button and wait for it to build\\

5. Now go back to your Twilio Console and select `Phone Numbers > Manage > Active Numbers`\\

6. Select a phone number you want to use for shSMS.\\

7. At the bottom in the Messaging section, set the following:\\

   **A message comes in** - function \
   **Service** - shSMS \
   **Environment** - ui \
   **Function path** - shSMS-forwarding (or whatever you named the function we just made)\\

8. Click "Save" at the very bottom

Repeat steps 6-8 to allow multiple numbers to use shSMS. (shSMS version >= 1.1.0)

That's it for the Twilio section! We're in the final sprint!! We're finally going to clone the backend repo!
