04. Twilio
This section is pretty simple, we're just going to tell Twilio to forward any incoming SMS to our server on Heroku.
Log in to Twilio and click the "Explore Options" in the left menu
Select
Functions
in the Developer Tools sectionClick the "Create Service" button and name your service whatever you want. Mine will be called
shSMS
.Click the "Add +" button at the top and select "Add Function"
This will create a function named something like "path_1". I renamed mine to
shSMS-forwarding
Delete everything in the window on the right and replace it with this (change the value of
baseURL
andenc
to match your Heroku URL/Secret_Key)
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);
}
};
Click "Save"
Click the "Dependencies" option (located just above the Deploy All button)
In Module, enter
axios
and in Version, enter0.21.4
Click the "Deploy All" button and wait for it to build
Now go back to your Twilio Console and select
Phone Numbers > Manage > Active Numbers
Select a phone number you want to use for shSMS.
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)
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!
Last updated