Learn how to Receive Inbound Flowroute SMS with Slack

Posted on August 23, 2017

This article describes how to run a simple Python app that can receive inbound SMS messages from your Flowroute phone numbers via the Flowroute API and drop them into the Slack channel of your choice.  This article assumes that you are comfortable with the Linux/Unix command line and have some firewall and computer networking experience.

Before you begin

  • Have a working Linux/Unix-based system that can allow the Python app to be exposed to the public internet.
  • Have Python 2.x or higher with pip package manager installed. Many systems have both installed by default.  If you do not, please install Python and pip first.
  • Have one or more Flowroute DIDs on your Flowroute account. See the Purchase a DID page of your Flowroute Manage account if you need to purchase a DID.
  • Have a free or paid Slack account with API access, or sign up for the Slack Developer Hangout team.

To configure the Python app:

  1. Create or obtain your Slack incoming webhook URL for the Slack channel where you want to post inbound SMS from your Flowroute DIDs.
  2. Click Add Configuration to create the webhook.
  3. In a terminal window, first, install the necessary Python modules if you do not have them already installed. Note that the json module is automatically included in Python versions 2.6 and above.
    pip install flask
    pip install requests
    
  4. Create a new file named receive.py using your preferred text editor, such as Vim or Nano, and paste the following code below into that file.
    NOTE: Replace YOUR_SLACK_WEBHOOK_URL with your Slack incoming webhook URL.
    from flask import Flask, request
    import json
    import requests
    
    app = Flask(__name__)
    
    app.debug = True
    
    @app.route('/',  methods=['POST'])
    def slack():
        json_content = request.json
        json_string = json.dumps(json_content, indent=4)
        parsed_json = json.loads(json_string)
    
        url = 'YOUR_SLACK_WEBHOOK_URL'
        data = {"text": parsed_json['body'],  
                "username": 'From # sent to $'.replace('#', parsed_json['from']).replace('$', parsed_json['to']),
                "icon_emoji": ":calling:"
        }
    
        headers = {'Content-Type': 'application/json',}
    
        r = requests.post(url, data=json.dumps(data), headers=headers)
        return str(r.content)
    
    if __name__ == '__main__':
        app.run(
            host="0.0.0.0",
            port=int("3000"))
    
  5. Run the following command to execute the app:
    python receive.py
    NOTE: You will need to keep the app running continuously in order to receive SMS messages.

    You should see some debugging output as follows, indicating the app webserver is running successfully:

    Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
    Restarting with stat
    Debugger is active!
    Debugger pin code: 144-609-426
  6. Next, expose the app’s listening port 3000 to the public internet. If your system is behind a router/firewall, you’ll need to create a Port Forwarding rule on your firewall to send traffic from your external port to the localhost.
    NOTE: If you want a quick solution or are having firewall trouble, we recommend using ngrok because it makes it incredibly easy to expose port 3000 by creating a tunnel to the localhost.
  7. After downloading ngrok, run it in a new terminal window using the following command:./ngrok http 3000Ngrok will return a subdomain, such as http://555a25e7.ngrok.io/, that forwards requests to your localhost webserver.

    NOTE: If you close ngrok for any reason and reopen it, the ngrok subdomain will change.  When this happens, make sure to update the SMS Callback URL each time, as explained above.

  8. Log on to your Flowroute account.
    • Click the Preferences menu, and then click API Control.The API Control page appears.
    • In the SMS Callback field, enter public_IP:3000 or your ngrok subdomain.For example,1.2.3.4:3000 or http://example.ngrok.io/
    • Click Enable SMS to save.
  9. Finally, send a test SMS message to one or more of your Flowroute phone numbers. If set up correctly, the message should post to your Slack channel.If your test message does not post in Slack, check your receive.py terminal window for some debugging output that will provide details about what caused the failure. For example:
    127.0.0.1 - - [14/Nov/2016 10:22:26] "POST / HTTP/1.1" 500 
    Traceback (most recent call last): 
    ...
    
    NOTE: If you’re using ngrok, check the ngrok terminal window for HTTP error codes that can help determine the cause of the issue as shown below. You can also check the ngrok web interface for additional details at http://127.0.0.1:4040.

    HTTP Requests
    -------------
    POST / 502 Bad Gateway
NOTE: To customize the appearance of the SMS messages in Slack, see Incoming Webhooks at the Slack website.