Overview
Previously, we showed you how to set up a Serverless function that enabled you to receive messages on your Flowroute number and store the SMS details into a DynamoDB table. Today we're going to leverage Python again with the Serverless Framework to post incoming SMS/MMS messages to a Slack channel that you manage. Slack is a popular communications platform that plenty of organizations use today. Slack not only helps with internal communications but also helps teams with coordinating their workflows.
SMS/MMS messaging is widely adopted by companies for customer engagement. We believe the ability to integrate SMS/MMS messages with Slack will be useful, and offer the following practical applications of the SMS/MMS to Slack integration. If you come up with other interesting use cases, please share those with Flowroute by emailing product-ideas@flowroute.com.
Get product / service feedback from your customers
Customer feedback regarding your company’s products and services (via SMS) can be directed to Slack channels for relevant teams to process.Run surveys / polls
Your company can run a survey/poll via SMS, with the customer responses being directed to Slack.Run contests for brand engagement
As a consumer brand, you can keep your followers engaged by having them snap pictures of themselves wearing/using your brand. They can then send the images to you by MMS which will be cross-posted to Slack for your team to determine the snazziest / coolest pic and also share a few laughs!
New to MMS with Flowroute?
We have recently launched v2.1 of our Messaging API that additionally lets you send and receive MMS messages on your Flowroute long code and toll-free numbers. Learn more about MMS on the Messages v2.1 Overview page.
Requirements
- An activated Flowroute account
- A Flowroute phone number (for sending the MMS to)
- Node.js v4 or higher (required for Serverless)
- Serverless Framework v1.x
- An AWS account
Setup and Installation
IAM credentials and the Serverless framework
Please follow steps 1 and 2 of Setup in "Receive and Store SMS using the Serverless Framework".
Incoming webhook for Slack
It is pretty straightforward to integrate incoming webhooks with Slack. To do so:- Visit the Incoming Webhooks page.
- If you're not signed in, you'll be redirected to the login page and asked to enter your workspace_url.
- Once you're logged in, click Add Configuration on the upper left corner of the screen.
- In the "New Configuration" page, choose an existing channel where your Incoming Webhook will post messages to, or create a new channel for testing this app.
- Click Add Incoming Webhooks integration. You will be redirected to the Configuration page of your new service which displays your Integration Settings.
- Copy the value from the "Webhook URL" field.
Serverless Configuration
Create your Serverless template
Once the Serverless framework and its dependencies are installed on your machine, you can create your project directory and add your template:
Create your directory
mkdir mms-to-slack-serverless
Switch to your newly-created directory
cd mms-to-slack-serverless
Create your template
serverless create --template aws-python
In running the create command, you will have generated boilerplate template files - handler.py, serverless.yml, and .gitignore. With the aws-python template above, you can define your AWS Lambda Functions, the events that trigger them and any AWS infrastructure resources they require, all in a file called serverless.yml.
Configure your service
Next, define your service configuration.
serverless.yml
service: mms-to-slack
frameworkVersion: ">=1.0.0 <2.0.0"
provider:
name: aws
runtime: python2.7
stage: dev
region: us-west-2
functions:
MMStoSlack:
handler: handler.mms_to_slack
events:
- http: POST mms_to_slack
Configuration | Description |
---|---|
service | Your Serverless service name; i.e., mms-to-slack. |
frameworkVersion | Your allowed version or range of versions following the Semantic Versioning spec. In this case, we have specified a range above or equal to v1.0.0 and below v2.0.0. This app has been tested using v1.23.0 and v1.14.0. |
provider | Information about your service provider for deployment, AWS, and the runtime you would like to write the service in, Python. | functions | Information about your local function, MMStoSlack, that will be deployed, its handler, and the events that trigger your function, an HTTP POST request. |
Every serverless.yml translates to a single AWS CloudFormation template and a CloudFormation stack is created from that resulting CloudFormation template.
Define your Lambda Function
Now it’s time to write the Lambda function for your service. First, import requests from botocore and the json module.
handler.py
from botocore.vendored import requests
import json
#Paste your webhook URL below
webhook_url = 'https://hooks.slack.com/services/xxxxxxx/xxxxx/xxxxx'
def mms_to_slack(event, context):
parsed = json.loads(event['body'])
response = requests.post(
webhook_url,
json={'text': json.dumps(parsed, indent=4, sort_keys=True,
ensure_ascii=False)}
)
http_reply = {
"statusCode": 200,
"body": response.text
}
return http_reply
In the above function, mms_to_slack, we create a variable called parsed and set its value to the event[body]object that is decoded to a Python object using json.loads(). The event[body] object comes in via an HTTP POST request to your service endpoint. Next, we create a variable, response, that holds your HTTP request with a nicely formatted JSON body and posts it to Slack via your incoming webhook URL. If the post is successful, we send the response to the caller of the Serverless service, Flowroute, which you will configure below.
Deploy your service to AWS
What's next? Let's take advantage of the Serverless Framework's main benefit, the ability to deploy your newly created service without the hassle of server management! To deploy all of your service to your preferred region, run the following in a command shell:
sls deploy --region us-west-2
You can then review your Service Information including the endpoint that you will later need.
Service Information
service: mms-to-slack
stage: dev
region: us-west-2
api keys:
None
endpoints:
POST - https://<service_id>.execute-api.us-west-2.amazonaws.com/dev/mms_to_slack
functions:
MMStoSlack: mms-to-slack-dev-MMStoSlack
For faster deployments, you have the option to only overwrite the zip file of your current function on AWS with the following command:
sls deploy function -f function_name
Use the function name that you have defined in your serverless.yml file as your argument.
Flowroute Configuration
Update your MMS Callback URL
Sign into your Flowroute account. Copy and paste the "endpoints" value under Service Information into the MMS Callback field of the API Control tab. To post your incoming SMS messages to the same Slack channel, you will need to also update the SMS CallBack field.
Text your Flowroute phone number
Next, send a test MMS to any of your MMS-enabled Flowroute number from your mobile phone. To verify that your message has been sent, visit your Slack channel with the incoming webhook integration.
Ta-da! You get all of the important MMS information including cost, body, sending and receiving phone numbers, and the media attachment.
The signed URL that is generated for the MMS media attachment is valid for 15 minutes. If you need to download and save these attachments, please do so within the validity period.Signed URLs for MMS Media
Teardown
Remove your service
Once you're done with your project, you have the option to remove your deployed service without deleting your local files. You can pass the stage or region options or leave it simple like the following:
serverless remove
Cost Considerations
The costs here are based on usage, but are extremely low per transaction, making the service a bargain to run. Often the costs of this are a fraction of running your own callback HTTP server (either in the cloud or in your own datacenter). And with the ability to remove deployed services that you no longer need, you avoid incurring unnecessary costs.