Send & Receive MMS
| Python | PHP | Node | .NET | Ruby |Python
⏱ 30 minutes build timeIntroduction
Telnyx’s messaging API supports both MMS and SMS messsages. Inbound multimedia messaging (MMS) messages include an attachment link in the webhook. The link and corresponding media should be treated as ephemeral and you should save any important media to a media storage (such as AWS S3) of your own.What you can do
At the end of this tutorial you’ll have an application that:- Receives an inbound message (SMS or MMS)
- Iterates over any media attachments and downloads the remote attachment locally
- Uploads the same attachment to AWS S3
- Sends the attachments back to the same phone number that originally sent the message
Pre-reqs & technologies
- Completed or familiar with the Receiving SMS & MMS Quickstart
- A working Messaging Profile with a phone number enabled for SMS & MMS.
- Ability to receive webhooks (with something like ngrok)
- Familiarity with Flask
- Python & PIP installed
- AWS Account setup with proper profiles and groups with IAM for S3. See the Quickstart for more information.
- Previously created S3 bucket with public permissions available.
Setup
Telnyx Portal configuration
Be sure to have a Messaging Profile with a phone number enabled for SMS & MMS and webhook URL pointing to your service (using ngrok or similar)Install packages via PIP
After pasting the above content, Kindly check and remove any new line added
Pipfile file with the packages needed to run the application.
Setting environment variables
The following environmental variables need to be set| Variable | Description |
TELNYX_API_KEY | Your Telnyx API Key |
TELNYX_PUBLIC_KEY | Your Telnyx Public Key |
TELNYX_APP_PORT | Defaults to 8000 The port the app will be served |
AWS_PROFILE | Your AWS profile as set in ~/.aws |
AWS_REGION | The region of your S3 bucket |
TELNYX_MMS_S3_BUCKET | The name of the bucket to upload the media attachments |
.env file
This app uses the excellent python-dotenv package to manage environment variables. Make a copy of the file below, add your credentials, and save as.env in the root directory.
After pasting the above content, Kindly check and remove any new line added
Code-along
We’ll use a singeapp.py file to build the MMS application.
After pasting the above content, Kindly check and remove any new line added
Setup Flask Server
After pasting the above content, Kindly check and remove any new line added
Receiving Webhooks
Now that you have setup your auth token, phone number, and connection, you can begin to use the API Library to send/receive SMS & MMS messages. First, you will need to setup an endpoint to receive webhooks for inbound messages & outbound message Delivery Receipts (DLR).Basic routing & functions
The basic overview of the application is as follows:- Verify webhook & create TelnyxEvent
- Extract information from the webhook
- Iterate over any media and download/re-upload to S3 for each attachment
- Send the message back to the phone number from which it came
- Acknowledge the status update (DLR) of the outbound message
Media download & upload functions
Before diving into the inbound message handler, first we’ll create a few functions to manage our attachments.download_filesaves the content from a URL to diskupload_fileuploads the file passed to AWS S3 (and makes object public)media_downloader_uploadercalls the download function and passes result to upload function
After pasting the above content, Kindly check and remove any new line added
Inbound message handling
Now that we have the functions to manage the media, we can start receiving inbound MMS’s The flow of our function is (at a high level):- Extract relevant information from the webhook
- Build the
webhook_urlto direct the DLR to a new endpoint - Iterate over any attachments/media and call our
media_downloader_uploaderfunction - Send the outbound message back to the original sender with the media attachments
After pasting the above content, Kindly check and remove any new line added
Outbound message handling
As we defined ourwebhook_url path to be /messaging/outbound we’ll need to create a function that accepts a POST request to that path within messaging.js.
After pasting the above content, Kindly check and remove any new line added
Final app.py
All together the app.py should look something like:After pasting the above content, Kindly check and remove any new line added
Usage
Start the serverpython app.py
When you are able to run the server locally, the final step involves making your application accessible from the internet. So far, we’ve set up a local web server. This is typically not accessible from the public internet, making testing inbound requests to web applications difficult.
The best workaround is a tunneling service. They come with client software that runs on your computer and opens an outgoing permanent connection to a publicly available server in a data center. Then, they assign a public URL (typically on a random or custom subdomain) on that server to your account. The public server acts as a proxy that accepts incoming connections to your URL, forwards (tunnels) them through the already established connection and sends them to the local web server as if they originated from the same machine. The most popular tunneling tool is ngrok. Check out the ngrok setup walkthrough to set it up on your computer and start receiving webhooks from inbound messages to your newly created application.
Once you’ve set up ngrok or another tunneling service you can add the public proxy URL to your Inbound Settings in the Mission Control Portal. To do this, click the edit symbol [✎] next to your Messaging Profile. In the “Inbound Settings” > “Webhook URL” field, paste the forwarding address from ngrok into the Webhook URL field. Add messaging/inbound to the end of the URL to direct the request to the webhook endpoint in your server.
For now you’ll leave “Failover URL” blank, but if you’d like to have Telnyx resend the webhook in the case where sending to the Webhook URL fails, you can specify an alternate address in this field.
Callback URLs For Telnyx Applications
| Callback Type | URL |
| Inbound Message Callback | {ngrok-url}/messaging/inbound |
| Outbound Message Status Callback | {ngrok-url}/messaging/outbound |
- Text your phone number and receive a response!
- Send a picture to your phone number and get that same picture right back!
PHP
⏱ 30 minutes build timeIntroduction to MMS
Telnyx’s messaging API supports both MMS and SMS messsages. Inbound multimedia messaging (MMS) messages include an attachment link in the webhook. The link and corresponding media should be treated as ephemeral and you should save any important media to a media storage (such as AWS S3) of your own.What you can do with this Tutorial
At the end of this tutorial you’ll have an application that:- Receives an inbound message (SMS or MMS)
- Iterates over any media attachments and downloads the remote attachment locally
- Uploads the same attachment to AWS S3
- Sends the attachments back to the same phone number that originally sent the message
Pre-reqs & technologies for MMS
- Completed or familiar with the Receiving SMS & MMS Quickstart
- A working Messaging Profile with a phone number enabled for SMS & MMS.
- PHP installed with Composer
- Familiarity with Slim
- Ability to receive webhooks (with something like ngrok)
- AWS Account setup with proper profiles and groups with IAM for S3. See the Quickstart for more information.
- Previously created S3 bucket with public permissions available.
Setup Your MMS Application
Telnyx Portal configuration
Be sure to have a Messaging Profile with a phone number enabled for SMS & MMS and webhook URL pointing to your service (using ngrok or similar)Install packages via composer
After pasting the above content, Kindly check and remove any new line added
composer.json file with the packages needed to run the application.
Setting environment variables
The following environmental variables need to be set| Variable | Description |
TELNYX_API_KEY | Your Telnyx API Key |
TELNYX_PUBLIC_KEY | Your Telnyx Public Key |
TELNYX_APP_PORT | Defaults to 8000 The port the app will be served |
AWS_PROFILE | Your AWS profile as set in ~/.aws |
AWS_REGION | The region of your S3 bucket |
TELNYX_MMS_S3_BUCKET | The name of the bucket to upload the media attachments |
.env file
This app uses the excellent phpenv package to manage environment variables. Make a copy of the file below, add your credentials, and save as.env in the root directory.
After pasting the above content, Kindly check and remove any new line added
Code-along
Now create a folder public and a file in the public folderindex.php, then write the following to setup the telnyx library.After pasting the above content, Kindly check and remove any new line added
Setup Slim Server and instantiate Telnyx
After pasting the above content, Kindly check and remove any new line added
Receiving Webhooks for SMS & MMS
Now that you have setup your auth token, phone number, and connection, you can begin to use the API Library to send/receive SMS & MMS messages. First, you will need to setup an endpoint to receive webhooks for inbound messages & outbound message Delivery Receipts (DLR).Basic routing & functions
The basic overview of the application is as follows:- Verify webhook & create TelnyxEvent
- Extract information from the webhook
- Iterate over any media and download/re-upload to S3 for each attachment
- Send the message back to the phone number from which it came
- Acknowledge the status update (DLR) of the outbound message
Webhook validation middleware
Telnyx signs each webhook that can be validated by checking the signature with your public key. This example adds the verification step as middleware to be included on all Telnyx endpoints.After pasting the above content, Kindly check and remove any new line added
Media Download & Upload Functions
Before diving into the inbound message handler, first we’ll create a few functions to manage our attachments.downloadMediasaves the content from a URL to diskuploadMediauploads the file passed to AWS S3 (and makes object public)downloadUploadaccepts an object and calls both thedownloadMedia&uploadMediareturning the final S3 URL
After pasting the above content, Kindly check and remove any new line added
Inbound message handling
Now that we have the functions to manage the media, we can start receiving inbound MMS’s The flow of our function is (at a high level):- Extract relevant information from the webhook
- Build the
webhook_urlto direct the DLR to a new endpoint - Iterate over any attachments/media and call our
downloadUploadfunction - Send the outbound message back to the original sender with the media attachments
After pasting the above content, Kindly check and remove any new line added
Inbound Message Handling
As we defined ourwebhook_url path to be /messaging/outbound we’ll need to create a function that accepts a POST request to that path.
After pasting the above content, Kindly check and remove any new line added
Final index.php
All together the PHP samples should look something like:After pasting the above content, Kindly check and remove any new line added
Usage
Start the serverphp -S localhost:8000 -t public
When you are able to run the server locally, the final step involves making your application accessible from the internet. So far, we’ve set up a local web server. This is typically not accessible from the public internet, making testing inbound requests to web applications difficult.
The best workaround is a tunneling service. They come with client software that runs on your computer and opens an outgoing permanent connection to a publicly available server in a data center. Then, they assign a public URL (typically on a random or custom subdomain) on that server to your account. The public server acts as a proxy that accepts incoming connections to your URL, forwards (tunnels) them through the already established connection and sends them to the local web server as if they originated from the same machine. The most popular tunneling tool is ngrok. Check out the ngrok setup walkthrough to set it up on your computer and start receiving webhooks from inbound messages to your newly created application.
Once you’ve set up ngrok or another tunneling service you can add the public proxy URL to your Inbound Settings in the Mission Control Portal. To do this, click the edit symbol [✎] next to your Messaging Profile. In the “Inbound Settings” > “Webhook URL” field, paste the forwarding address from ngrok into the Webhook URL field. Add messaging/inbound to the end of the URL to direct the request to the webhook endpoint in your slim-php server.
For now you’ll leave “Failover URL” blank, but if you’d like to have Telnyx resend the webhook in the case where sending to the Webhook URL fails, you can specify an alternate address in this field.
Callback URLs For Telnyx Applications
| Callback Type | URL |
| Inbound Message Callback | {ngrok-url}/messaging/inbound |
| Outbound Message Status Callback | {ngrok-url}/messaging/outbound |
- Text your phone number and receive a response!
- Send a picture to your phone number and get that same picture right back!
Node
⏱ 30 minutes build time || Github RepoIntroduction
Telnyx’s messaging API supports both MMS and SMS messsages. Inbound multimedia messaging (MMS) messages include an attachment link in the webhook. The link and corresponding media should be treated as ephemeral and you should save any important media to a media storage (such as AWS S3) of your own.What you can do
At the end of this tutorial you’ll have an application that:- Receives an inbound message (SMS or MMS)
- Iterates over any media attachments and downloads the remote attachment locally
- Uploads the same attachment to AWS S3
- Sends the attachments back to the same phone number that originally sent the message
Pre-reqs & technologies
- Completed or familiar with the Receiving SMS & MMS Quickstart
- A working Messaging Profile with a phone number enabled for SMS & MMS.
- Node & NPM installed
- Familiarity with Express
- Ability to receive webhooks (with something like ngrok)
- AWS Account setup with proper profiles and groups with IAM for S3. See the Quickstart for more information.
- Previously created S3 bucket with public permissions available.
Setup
Telnyx portal configuration
Be sure to have a Messaging Profile with a phone number enabled for SMS & MMS and webhook URL pointing to your service (using ngrok or similar)Install packages via NPM
After pasting the above content, Kindly check and remove any new line added
package.json file with the packages needed to run the application.
Setting environment variables
The following environmental variables need to be set| Variable | Description |
TELNYX_API_KEY | Your Telnyx API Key |
TELNYX_PUBLIC_KEY | Your Telnyx Public Key |
TELNYX_APP_PORT | Defaults to 8000 The port the app will be served |
AWS_PROFILE | Your AWS profile as set in ~/.aws |
AWS_REGION | The region of your S3 bucket |
TELNYX_MMS_S3_BUCKET | The name of the bucket to upload the media attachments |
.env file
This app uses the excellent dotenv package to manage environment variables. Make a copy of the file below, add your credentials, and save as.env in the root directory.
After pasting the above content, Kindly check and remove any new line added
Code-along
We’ll use a few.js files to build the MMS application. index.js as our entry point and messaging.js to contain our routes and controllers for the app.
After pasting the above content, Kindly check and remove any new line added
Setup Express Server
After pasting the above content, Kindly check and remove any new line added
Receiving Webhooks
Now that you have setup your auth token, phone number, and connection, you can begin to use the API Library to send/receive SMS & MMS messages. First, you will need to setup an endpoint to receive webhooks for inbound messages & outbound message Delivery Receipts (DLR).Basic routing & functions
The basic overview of the application is as follows:- Verify webhook & create TelnyxEvent
- Extract information from the webhook
- Iterate over any media and download/re-upload to S3 for each attachment
- Send the message back to the phone number from which it came
- Acknowledge the status update (DLR) of the outbound message
Webhook validation middleware
Telnyx signs each webhook that can be validated by checking the signature with your public key. This example adds the verification step as middleware to be included on all Telnyx endpoints. After declaring theconst app=express(); and before app.use('/messaging', messaging); add the following code to validate the webhook in indeed from Telnyx.
After pasting the above content, Kindly check and remove any new line added
Media Download & Upload Functions
Before diving into the inbound message handler, first we’ll create a few functions to manage our attachments inside themessaging.js file.
downloadFilesaves the content from a URL to diskuploadFileuploads the file passed to AWS S3 (and makes object public)- Note that this application is demonstrating 2 topics at once, downloading & uploading. It could be improved by piping or streaming the data from Telnyx to S3 instead of saving to disk.
After pasting the above content, Kindly check and remove any new line added
Inbound message handling
Now that we have the functions to manage the media, we can start receiving inbound MMS’s The flow of our function is (at a high level):- Extract relevant information from the webhook
- Build the
webhook_urlto direct the DLR to a new endpoint - Iterate over any attachments/media and call our
download&uploadfunctions - Send the outbound message back to the original sender with the media attachments
After pasting the above content, Kindly check and remove any new line added
Outbound message handling
As we defined ourwebhook_url path to be /messaging/outbound we’ll need to create a function that accepts a POST request to that path within messaging.js.
After pasting the above content, Kindly check and remove any new line added
Decare routes for inbound and outbound messaging
At the bottom ofmessaging.js add the routes and point to the correct controller function
After pasting the above content, Kindly check and remove any new line added
Final index.js
All together the index.js should look something like:After pasting the above content, Kindly check and remove any new line added
Final messaging.js
After pasting the above content, Kindly check and remove any new line added
Usage
Start the servernode index.js
When you are able to run the server locally, the final step involves making your application accessible from the internet. So far, we’ve set up a local web server. This is typically not accessible from the public internet, making testing inbound requests to web applications difficult.
The best workaround is a tunneling service. They come with client software that runs on your computer and opens an outgoing permanent connection to a publicly available server in a data center. Then, they assign a public URL (typically on a random or custom subdomain) on that server to your account. The public server acts as a proxy that accepts incoming connections to your URL, forwards (tunnels) them through the already established connection and sends them to the local web server as if they originated from the same machine. The most popular tunneling tool is ngrok. Check out the ngrok setup walkthrough to set it up on your computer and start receiving webhooks from inbound messages to your newly created application.
Once you’ve set up ngrok or another tunneling service you can add the public proxy URL to your Inbound Settings in the Mission Control Portal. To do this, click the edit symbol [✎] next to your Messaging Profile. In the “Inbound Settings” > “Webhook URL” field, paste the forwarding address from ngrok into the Webhook URL field. Add messaging/inbound to the end of the URL to direct the request to the webhook endpoint in your server.
For now you’ll leave “Failover URL” blank, but if you’d like to have Telnyx resend the webhook in the case where sending to the Webhook URL fails, you can specify an alternate address in this field.
Callback URLs For Telnyx Applications
| Callback Type | URL |
| Inbound Message Callback | {ngrok-url}/messaging/inbound |
| Outbound Message Status Callback | {ngrok-url}/messaging/outbound |
- Text your phone number and receive a response!
- Send a picture to your phone number and get that same picture right back!
.NET
⏱ 30 minutes build timeIntroduction
Telnyx’s messaging API supports both MMS and SMS messsages. Inbound multimedia messaging (MMS) messages include an attachment link in the webhook. The link and corresponding media should be treated as ephemeral and you should save any important media to a media storage (such as AWS S3) of your own.What you can do
At the end of this tutorial you’ll have an application that:- Receives an inbound message (SMS or MMS)
- Iterates over any media attachments and downloads the remote attachment locally
- Uploads the same attachment to AWS S3
- Sends the attachments back to the same phone number that originally sent the message
Pre-reqs & technologies
- Completed or familiar with the Receiving SMS & MMS Quickstart
- A working Messaging Profile with a phone number enabled for SMS & MMS.
- Ability to receive webhooks (with something like ngrok)
- DotNet Core installed
- AWS Account setup with proper profiles and groups with IAM for S3. See the Quickstart for more information.
- Previously created S3 bucket with public permissions available.
Setup
Telnyx Portal configuration
Be sure to have a Messaging Profile with a phone number enabled for SMS & MMS and webhook URL pointing to your service (using ngrok or similar)Create a new dotnet core project
Create a new web project and set the output tomms-demo
After pasting the above content, Kindly check and remove any new line added
Install packages via dotnet CLI
After pasting the above content, Kindly check and remove any new line added
.csproj file with the packages needed to run the application.
Setting environment variables
The following environmental variables need to be set| Variable | Description |
TELNYX_API_KEY | Your Telnyx API Key |
TELNYX_PUBLIC_KEY | Your Telnyx Public Key |
TELNYX_APP_PORT | Defaults to 8000 The port the app will be served |
AWS_PROFILE | Your AWS profile as set in ~/.aws |
AWS_REGION | The region of your S3 bucket |
TELNYX_MMS_S3_BUCKET | The name of the bucket to upload the media attachments |
.env file
This app uses the excellent dotenv.net package to manage environment variables. Make a copy of the file below, add your credentials, and save as.env in the root directory.
After pasting the above content, Kindly check and remove any new line added
Code-along
Thedotnet new web -o mms-demo command scaffolds out a basic ASP.NET Core project structure with a few files and folders. We’re focused mainly on:
- Program.cs
- Startup.cs
After pasting the above content, Kindly check and remove any new line added
Modify Startup.cs to include controllers
Update theConfigureServices function to use the Controllers function and the NewtonsoftJSON library.
After pasting the above content, Kindly check and remove any new line added
Configure function to map the controllers. In the app.UseEndpoints() method add endpoints.MapControllers(); so your Configure function looks like:
After pasting the above content, Kindly check and remove any new line added
Modify Program.cs
Update theMain function to call the DotEnv.Config(); function to load the values in the .env file to Environment variables.
- Be sure to add
using dotenv.net;
After pasting the above content, Kindly check and remove any new line added
CreateHostBuilder function to launch on the port as defined in the .env file:
After pasting the above content, Kindly check and remove any new line added
Receiving Webhooks
Now that the basic app structure is setup, we need to create the Controller to receive webhooks for inbound messages & outbound message Delivery Receipts (DLR).TelnyxMessagingController.cs Scaffold
Build out the controller to look something like the example below. The example includes all the packages the rest of the controller code will leverage.After pasting the above content, Kindly check and remove any new line added
Basic Routing & Functions
The basic overview of the application is as follows:- Receive webhook & de-serialize JSON into a dynamic
- Extract information from the webhook
- Iterate over any media and download/re-upload to S3 for each attachment
- Send the message back to the phone number from which it came
- Acknowledge the status update (DLR) of the outbound message
Webhook Helpers & Media Download/Upload Functions
Before diving into the inbound message handler, first we’ll create a few functions within a new classWebhookHelpers to manage our attachments.
deserializeCallbackToDynamicReads JSON and returns a dynamic object.UploadFileAsyncuploads the file passed to AWS S3 (and makes object public)downloadMediaAsyncdownloads the file to specified directory with the specified fileName
After pasting the above content, Kindly check and remove any new line added
Inbound message handling
Now that we have the functions to manage the media, we can start receiving inbound MMS’s The flow of our function is (at a high level):- Extract relevant information from the webhook
- Build the
webhook_urlto direct the DLR to a new endpoint - Iterate over any attachments/media and call our media management function
- Send the outbound message back to the original sender with the media attachments
After pasting the above content, Kindly check and remove any new line added
Outbound message handling
As we defined ourwebhook_url path to be /messaging/outbound we’ll need to create a function that accepts a POST request to that path within the controller.
After pasting the above content, Kindly check and remove any new line added
Final TelnyxMessagingController.cs
All together the TelnyxMessagingController.cs should look something like:After pasting the above content, Kindly check and remove any new line added
Usage
Start the server:After pasting the above content, Kindly check and remove any new line added
ngrok. Check out the ngrok setup walkthrough to set it up on your computer and start receiving webhooks from inbound messages to your newly created application.
Once you’ve set up ngrok or another tunneling service you can add the public proxy URL to your Inbound Settings in the Mission Control Portal. To do this, click the edit symbol [✎] next to your Messaging Profile. In the “Inbound Settings” > “Webhook URL” field, paste the forwarding address from ngrok into the Webhook URL field. Add messaging/inbound to the end of the URL to direct the request to the webhook endpoint in your server.
For now you’ll leave “Failover URL” blank, but if you’d like to have Telnyx resend the webhook in the case where sending to the Webhook URL fails, you can specify an alternate address in this field.
Callback URLs For Telnyx Applications
| Callback Type | URL |
| Inbound Message Callback | {ngrok-url}/messaging/inbound |
| Outbound Message Status Callback | {ngrok-url}/messaging/outbound |
- Text your phone number and receive a response!
- Send a picture to your phone number and get that same picture right back!
Ruby
⏱ 30 minutes build timeIntroduction
Telnyx’s messaging API supports both MMS and SMS messsages. Inbound multimedia messaging (MMS) messages include an attachment link in the webhook. The link and corresponding media should be treated as ephemeral and you should save any important media to a media storage (such as AWS S3) of your own.What you can do
At the end of this tutorial you’ll have an application that:- Receives an inbound message (SMS or MMS)
- Iterates over any media attachments and downloads the remote attachment locally
- Uploads the same attachment to AWS S3
- Sends the attachments back to the same phone number that originally sent the message
Pre-reqs & technologies
- Completed or familiar with the Receiving SMS & MMS Quickstart
- A working Messaging Profile with a phone number enabled for SMS & MMS.
- Ruby & Gem installed
- Familiarity with Sinatra
- Ability to receive webhooks (with something like ngrok)
- AWS Account setup with proper profiles and groups with IAM for S3. See the Quickstart for more information.
- Previously created S3 bucket with public permissions available.
Setup
Telnyx portal configuration
Be sure to have a Messaging Profile with a phone number enabled for SMS & MMS and webhook URL pointing to your service (using ngrok or similar)Install packages via Gem/bundler
After pasting the above content, Kindly check and remove any new line added
Gemfile file with the packages needed to run the application.
Setting environment variables
The following environmental variables need to be set| Variable | Description |
TELNYX_API_KEY | Your Telnyx API Key |
TELNYX_PUBLIC_KEY | Your Telnyx Public Key |
TELNYX_APP_PORT | Defaults to 8000 The port the app will be served |
AWS_PROFILE | Your AWS profile as set in ~/.aws |
AWS_REGION | The region of your S3 bucket |
TELNYX_MMS_S3_BUCKET | The name of the bucket to upload the media attachments |
.env file
This app uses the excellent dotenv package to manage environment variables. Make a copy of the file below, add your credentials, and save as.env in the root directory.
After pasting the above content, Kindly check and remove any new line added
Code-along
We’ll use a singeapp.rb file to build the MMS application.
After pasting the above content, Kindly check and remove any new line added
Setup Sinatra Server
After pasting the above content, Kindly check and remove any new line added
Receiving Webhooks
Now that you have setup your auth token, phone number, and connection, you can begin to use the API Library to send/receive SMS & MMS messages. First, you will need to setup an endpoint to receive webhooks for inbound messages & outbound message Delivery Receipts (DLR).Basic routing & functions
The basic overview of the application is as follows:- Verify webhook & create TelnyxEvent
- Extract information from the webhook
- Iterate over any media and download/re-upload to S3 for each attachment
- Send the message back to the phone number from which it came
- Acknowledge the status update (DLR) of the outbound message
Media Download & Upload Functions
Before diving into the inbound message handler, first we’ll create a few functions to manage our attachments.download_filesaves the content from a URL to diskupload_fileuploads the file passed to AWS S3 (and makes object public)
After pasting the above content, Kindly check and remove any new line added
Inbound message handling
Now that we have the functions to manage the media, we can start receiving inbound MMS’s The flow of our function is (at a high level):- Extract relevant information from the webhook
- Build the
webhook_urlto direct the DLR to a new endpoint - Iterate over any attachments/media and call our
download&uploadfunctions - Send the outbound message back to the original sender with the media attachments
After pasting the above content, Kindly check and remove any new line added
Outbound Message Handling
As we defined ourwebhook_url path to be /messaging/outbound we’ll need to create a function that accepts a POST request to that path within messaging.js.
After pasting the above content, Kindly check and remove any new line added
Final app.rb
All together the app.rb should look something like:After pasting the above content, Kindly check and remove any new line added
Usage
Start the serverruby app.rb
When you are able to run the server locally, the final step involves making your application accessible from the internet. So far, we’ve set up a local web server. This is typically not accessible from the public internet, making testing inbound requests to web applications difficult.
The best workaround is a tunneling service. They come with client software that runs on your computer and opens an outgoing permanent connection to a publicly available server in a data center. Then, they assign a public URL (typically on a random or custom subdomain) on that server to your account. The public server acts as a proxy that accepts incoming connections to your URL, forwards (tunnels) them through the already established connection and sends them to the local web server as if they originated from the same machine. The most popular tunneling tool is ngrok. Check out the ngrok setup walkthrough to set it up on your computer and start receiving webhooks from inbound messages to your newly created application.
Once you’ve set up ngrok or another tunneling service you can add the public proxy URL to your Inbound Settings in the Mission Control Portal. To do this, click the edit symbol [✎] next to your Messaging Profile. In the “Inbound Settings” > “Webhook URL” field, paste the forwarding address from ngrok into the Webhook URL field. Add messaging/inbound to the end of the URL to direct the request to the webhook endpoint in your server.
For now you’ll leave “Failover URL” blank, but if you’d like to have Telnyx resend the webhook in the case where sending to the Webhook URL fails, you can specify an alternate address in this field.
Callback URLs For Telnyx Applications
| Callback Type | URL |
| Inbound Message Callback | {ngrok-url}/messaging/inbound |
| Outbound Message Status Callback | {ngrok-url}/messaging/outbound |
- Text your phone number and receive a response!
- Send a picture to your phone number and get that same picture right back!