Relaying and Replying to Twilio SMS via Slack with Fission.io Functions
Tags: python, serverless, twilio, kubernetes
Published: 2019-02-05 03:42:03

---
title: Relaying and Replying to Twilio SMS via Slack with Fission.io Functions
published: true
tags: python,serverless,twilio,kubernetes
canonical_url: https://medium.com/@jmarhee/relaying-and-replying-to-twilio-sms-via-slack-with-fission-io-functions-fb4a937ce23d
---

Using [a Serverless framework like Fission.io](http://fission.io) is a quick, and portable way to create an application layer between highly-programmable services like Twilio and Slack — common, but not always easily automated and integrated, services for developers to program communication services like SMS, voice, and chat services. Fission addresses this by allowing you to run lightweight programs that can consume and operate on request data, and in our scenario, act as an orchestrator or router for these communication tools.

What we’re going to create is two Fission tasks, small Python (in our case) programs, to receive a number and a message to go out through Slack to Twilio to the intended recipient, and when your Twilio SMS number receives a message, it is delivered to Slack.

**Prerequisites**

You’ll need from Slack an incoming webhook (to post messages to a channel) and a Slash Command (to have Fission trigger your function via POST requests from Slack on this webhook):

[Incoming Webhooks](https://api.slack.com/incoming-webhooks#create_a_webhook)

Make a note of the hooks.slack URL for your Fission task in a moment. Then, create your Slack app for a Slash Command, something like /twilio :

[Slash Commands](https://api.slack.com/slash-commands#creating_commands)

From Twilio, you’ll just need your Account SID, API token, and an SMS-capable Twilio number:

[Twilio | Login](https://www.twilio.com/console)

you will also, once the tasks are deployed (because you’ll need the Fission URL you end up creating), create a Twilio Webhook for any one of your numbers:

[Configuring Phone Numbers to Receive and Respond to SMS and MMS Messages](https://support.twilio.com/hc/en-us/articles/223136047-Configuring-Phone-Numbers-to-Receive-and-Respond-to-SMS-and-MMS-Messages#webhook)

**Creating our Functions**

In send.py we’re going to create the script that runs when your Slash Command is run (a POST request from Slack to Fission):

```
import json

import requests

import urllib

from flask import request, redirect

from twilio.rest import Client

CHANNEL\_NAME="twilio"

SLACK\_TOKEN=""

TWILIO\_SENDER="" #Number with Country code

LIMIT\_TO\_CC="" #i.e. +1
```

In this section, replace SLACK\_TOKEN with your Slack App token, define the CHANNEL you want the response form the Slack API into (ideally the same one where the command will be run), and then optionally add the country code to limit SMS messages to (so you can have users provide a 10-digit phone number), and your Twilio number in TWILIO\_SENDER .

In our main() function (Fission’s default entrypoint for Python tasks), we’ll need to do some setup so it can pick apart the body from Slack when a slash command like /twilio send:8325551900 hello, how are you hits your Fission task:

```
_def_ main():

    params = urllib.parse.parse\_qs(_str_(request.get\_data()))

    incoming\_token = params["b'token"][0]

    channel\_name = params['channel\_name'][0]

    response\_url = params['response\_url'][0]

    message = params['text'][0]
```

There’s a couple of new pieces here, the incoming\_token which we’ll revisit in a moment to ensure it matches the desired token for the Slack app, and response\_url which will give us a URL to POST a response back to with the status of the request.

We’ll, next, add some Help text, and then start slicing up the message:

[jmarhee/fission-twilio-sms-slack](https://github.com/jmarhee/fission-twilio-sms-slack/blob/master/send.py#L34-L57)

so we’re separating out the number from the message text, and then handing it to our twilio\_conn function (which I’ll cover in a second) and send that response back to the Slack response URL so you can see if your message was sent properly or not in slack\_callback :

[jmarhee/fission-twilio-sms-slack](https://github.com/jmarhee/fission-twilio-sms-slack/blob/master/send.py#L13-L32)

At this point, we can create this function:

```
fission function create --name twilio\_send --env python3 --route /twilio\_send --method POST --code send.py
```

and give the URL http://$FISSION\_ROUTER/twilio\_send to your Slack app’s Slash Command to POST to.

Our next step will be to create an endpoint for Twilio to send requests to, so we’ll do similar setup on our recv.py script. For this, you’ll just need your Incoming Webhook URL from Slack:

[jmarhee/fission-twilio-sms-slack](https://github.com/jmarhee/fission-twilio-sms-slack/blob/master/recv.py#L4-L10)

Before we go any further, let’s take a look at the sort of data Twilio sends over this webhook. In our example, we’ll get GET requests from Twilio that look like this:

```
"GET /twil?ToCountry=US&ToState=CO&SmsMessageSid=&NumMedia=0&ToCity=&FromZip=&SmsSid=&FromState=TX&SmsStatus=received&FromCity=&Body=Dhdhdhdhsh&FromCountry=US&To=%&ToZip=&NumSegments=1&MessageSid=&AccountSid=&From=%&ApiVersion=2010-04-01 HTTP/1.1"
```

and we’re mostly concerned with the From , To , and Body fields in the payload from Twilio, so in our recv.py entrypoint, we’ll want to handle these, and pass them to a slack\_callout function like we did in send.py in a much simpler workflow:

[jmarhee/fission-twilio-sms-slack](https://github.com/jmarhee/fission-twilio-sms-slack/blob/master/recv.py#L20-L28)

We’re grabbing these fields from the request.args object containing the above formatted payload, and converting the values to strings, and passing it to a similar Slack callout function that just POSTs directly to the webhook URL to dump the message, and its sender, into the channel.

Once you create your task:

```
fission function create --name twilio\_recv --env python3 --route /twilio\_recv --method GET --code recv.py
```

go back over to the Twilio Console, and in the SMS field for your number, you’ll specify that you’d like a Webhook, and that the data be sent as a GET request to that endpoint, your http://$FISSION\_ROUTER/twilio\_recv URL:

![](https://cdn-images-1.medium.com/max/1024/1*CI8zXkEu7SxUduJcKoV4jg.png)

**Further Reading**

You can take a look at the completed scripts here:

[jmarhee/fission-twilio-sms-slack](https://github.com/jmarhee/fission-twilio-sms-slack)