|
1 | 1 | # Forward Message |
2 | 2 |
|
3 | | -This Function in `forward-message.js` returns the TwiML required to forward an incoming SMS message to a single number or each number in a comma-separated list set in the environment variables. |
| 3 | +This application forwards an incoming SMS message to a single number or each number in a comma-separated list set in the environment variables. The forwarded message includes the original sender's number and message body in the format: `From: <number>. Body: <message>`. |
| 4 | + |
| 5 | +## Pre-requisites |
| 6 | + |
| 7 | +- A [Twilio account](https://www.twilio.com/try-twilio) with an active phone number that can receive SMS |
4 | 8 |
|
5 | 9 | ### Environment variables |
6 | 10 |
|
7 | | -This Function expects one environment variable to be set. |
| 11 | +`.env` is included with placeholder values. Update `FORWARDING_NUMBERS` before running. Never commit `.env`. |
| 12 | + |
| 13 | +| Variable | Description | Required | |
| 14 | +| :- | :- | :- | |
| 15 | +| `FORWARDING_NUMBERS` | A list of numbers [in E.164 format](https://support.twilio.com/hc/en-us/articles/223183008-Formatting-International-Phone-Numbers) to forward incoming messages to, separated by commas | Yes | |
| 16 | +| `TWILIO_SMS_WEBHOOK_URL` | The webhook path — pre-set to `/forward-message`, do not change | Yes | |
| 17 | + |
| 18 | +## Running the project |
| 19 | + |
| 20 | +1. Install the [Twilio CLI](https://www.twilio.com/docs/twilio-cli/quickstart#install-twilio-cli) |
| 21 | +1. Install the [serverless toolkit](https://www.twilio.com/docs/labs/serverless-toolkit/getting-started) |
| 22 | + |
| 23 | +```shell |
| 24 | +twilio plugins:install @twilio-labs/plugin-serverless |
| 25 | +``` |
| 26 | + |
| 27 | +1. Initialize the project from the template |
| 28 | + |
| 29 | +```shell |
| 30 | +twilio serverless:init example --template=forward-message --skip-credentials && cd example |
| 31 | +``` |
| 32 | + |
| 33 | +1. Edit `.env` and replace the placeholder `FORWARDING_NUMBERS` with your real E.164 number(s) |
| 34 | + |
| 35 | +1. Start the server |
| 36 | + |
| 37 | +```shell |
| 38 | +twilio serverless:start |
| 39 | +``` |
| 40 | + |
| 41 | +Check the developer console and terminal for any errors, and make sure you've set your environment variables. |
| 42 | + |
| 43 | +### Configuring your Twilio phone number |
| 44 | + |
| 45 | +You can configure the webhook via the Twilio CLI (recommended) or the Console. |
| 46 | + |
| 47 | +**Via CLI:** |
| 48 | + |
| 49 | +Find your phone number SID: |
| 50 | + |
| 51 | +```shell |
| 52 | +twilio api:core:incoming-phone-numbers:list |
| 53 | +``` |
| 54 | + |
| 55 | +Then update the SMS webhook: |
| 56 | + |
| 57 | +```shell |
| 58 | +twilio api:core:incoming-phone-numbers:update \ |
| 59 | + --sid <your-phone-number-sid> \ |
| 60 | + --sms-url https://<your-ngrok-id>.ngrok.app/forward-message \ |
| 61 | + --sms-method POST |
| 62 | +``` |
| 63 | + |
| 64 | +**Via Console:** |
| 65 | + |
| 66 | +1. Go to your [Twilio Console Phone Numbers](https://console.twilio.com/us1/develop/phone-numbers/manage/incoming) |
| 67 | +2. Select the phone number you want to use |
| 68 | +3. Under "Messaging Configuration": |
| 69 | + - Set "A Message Comes In" to **Webhook** |
| 70 | + - Enter your URL: `https://<your-ngrok-id>.ngrok.app/forward-message` (or your deployed URL) |
| 71 | + - Set HTTP method to **POST** |
| 72 | +4. Click **Save** |
| 73 | + |
| 74 | +## Deploying |
8 | 75 |
|
9 | | -| Variable | Meaning | |
10 | | -| :---------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
11 | | -| `FORWARDING_NUMBERS` | A list of numbers [in E.164 format](https://support.twilio.com/hc/en-us/articles/223183008-Formatting-International-Phone-Numbers) you want to forward incoming messages to, separated by commas | |
| 76 | +Deploy your functions and assets with the following command. Note: you must run these commands from inside your project folder. [More information about the serverless toolkit in the docs.](https://www.twilio.com/docs/labs/serverless-toolkit) |
12 | 77 |
|
13 | | -### Parameters |
| 78 | +With the [Twilio CLI](https://www.twilio.com/docs/twilio-cli/quickstart): |
14 | 79 |
|
15 | | -This Function expects the incoming request to be a messaging webhook. The parameters that will be used are `From` and `Body`. |
| 80 | +```shell |
| 81 | +twilio serverless:deploy --service-name forward-message |
| 82 | +``` |
0 commit comments