What you'll need
- Join the 46elks Slack workspace or the Slack space of your choice ofc 👍🏼
- A 46elks account.
- Your API credentials.
- A virtual phone number.
- A live server to run Python on.
Send SMS with Python
If you do not have access to a server to run Python on, then you can borrow our server. Send a SMS with your first name to +46766865445 and you'll get a login on SMS:
Log in to the server
- Log in to the server with the credentials that you got in the SMS.
- Swap
username
to your username in the SMS you got from the workshop server. - Use the password you got and you're in.
Send a SMS
- Create your first python file here with
nano send-sms.py
- Copy and paste this in to your new file.
CTRL+X
and thenY
to save your file.- To run your Python script on the server now you run
python send-sms.py
in your Terminal/Command line. - If everything worked you should have gotten a
Response [200]
response.
Set up Slack app and webhook
- Join our 46elks Slack workspace or create your own.
- Create a slack app.
- In your Slack workspace, go to the "Apps" section and create a new Slack app.
- Give your app a name and associate it with the right workspace for example 46elks.
- Add a webhook integration in your app.
- Inside your Slack app settings, navigate to the "Incoming Webhooks" section.
- Activate the Incoming Webhooks feature if it's not already enabled.
- Click on "Add New Webhook to Workspace".
- Choose a channel to post your incoming SMS in.
- Select the channel where you want to send the SMS messages. You can choose an existing channel or create a new one.
- Click "Allow" to authorize your app to post messages to the selected channel.
- Copy your webhook URL and store in a smart place, we are going to use that later.
Set up a Flask app and our Python code
Let's set up Flask to create our virtual environment. Run each command in your command line.
python3 -m venv slack_sms
this creates a isolated Python environment named "slack_sms". Used to manage project specific dependencies.. slack_sms/bin/activate
activates the environment. To stop the environment you run:deactivate
pip install requests
installing the Python library Requests in order to make HTTP requests.pip install Flask
installs Flask, a web framework to create your web application.
Write the Python code
Create a new python file sms-slack.py
and now let's code our SMS integration
- Import Flask.
- At the top of the file,
import requests
- Set up new route. Here we are creating an endpoint at /incoming-sms that listens for HTTP POST requests.
- Handle incoming SMS and save it to variables that we'll use later on.
- Define our Slack URL that we copied and stored previously.
- Create a payload for Slack (what will we forward to Slack).
- Forward the SMS to Slack.
- Handle the response to check success or for troubleshooting.
- Start your Flask app. Replace
5000
with your own port number.
Configure our 46elks number
- Navigate in to 46elks dashboard and click on "Numbers".
- Next to your number you'll click on "Edit".
-
Now we set up our sms_url. It's our webhook, it's the server URL + the endpoint that we set up in our app route. Ex like this
http://64.225.108.133:5000/incoming-sms
. Replace5000
to your port number.
Let's see if it works
Start the Flask app
Now we start our Flask app in the command line and see if it works.
- Run your Flask app. Replace
5000
with your own port number. - Visit your Flask app. Replace
5000
with your own port number.
Now you send a SMS to your virtual number and see if it gets forwarded to your Slack channel - Let's test it and see what happens 🚀
TL;DR version of the guide
You'll need to do the below steps and then copy the entire code is avaible below to be run on your server.
Create a Slack app and webhook
- Go on to 46elks Slack kanal.
- Create a Slack app.
- Add a webhook integration in your app.
- Choose a channel to recive your SMS to.
Send SMS to Slack
- Create a Flask app.
- Create a Python fil, ex
sms-slack.py
Configure your virtual number
- Change the sms_url on your virtual number to the webhook URL + the endpoint in your Flask app.
Send a SMS to your virtual number - Let's see if it works 🚀
If you have any questions, please reach out to our support.