Python version = 3.6.5
Requirements
bandwidth-sdk==3.0.1
certifi==2018.4.16
chardet==3.0.4
Django==2.0.6
idna==2.6
lxml==4.2.1
python-dateutil==2.7.3
pytz==2018.4
requests==2.18.4
six==1.11.0
urllib3==1.22
This guide makes the following assumptions:
- You already have a Bandwidth account (https://dev.bandwidth.com/).
- You already have a Mailgun account (https://www.mailgun.com/).
- You already have a ngrok account (https://ngrok.com/).
- You have python3, pip, ngrok, and virtualenv installed on your system.
- You have purchased a Bandwidth number, and have a domain to use for Mailgun (Mailgun's default sandbox domain will be sufficient for a tutorial).
- You have an email address to use as your destination email.
This guide shows how to set up a bidirectional Email-SMS integration using the Bandwidth messaging API and the Mailgun API hosted locally using ngrok. This app allows an end user to send a text to a Bandwidth number, and receive the contents of that text as an email to an email of your choice. Replying to that email will send a text to the end user with the contents of the reply.
Open up a terminal and execute the following command:
ngrok http 8000
Take note of the forwarding urls. You will need these later in the setup. Leave this terminal running, and continue the rest of this guide on a new terminal.
Open up a new terminal. Create a new Python virtual environment by executing the following command:
virtualenv -p python3 <path_to_virtual_environment>
Activate the virtual environment by executing the following command:
source <path_to_virtual_environment>/bin/activate
Your terminal should show the name of your virtual environment on the prompt.
Run the following commands to install the project requirements via pip:
pip install django
pip install bandwidth-sdk
pip install requests
Execute the following commands:
git clone https://github.com/jmulford-bandwidth/Email-SMS-Integration.git
cd Email-SMS-Integration
- Go to app.bandwidth.com.
- Login to your account.
- Go to the 'Applications' tab.
- Click '+ Create New' button (top left of the page if you already have an app, otherwise in the center of the page).
- Name your application whatever you want (I would suggest something related to email-sms-integration).
- Set the 'Callback request method' to POST.
- Set the 'Application Type' to 'Messaging'.
- In the 'Messaging callback URL' box, type
<your_ngrok_url>/send_mailgun_email/
. - Click the 'Create' button.
- Go to the 'Numbers' tab.
- Assign one of your Bandwidth numbers to your new Bandwith application. This is the number that your end users will text.
- Go to app.mailgun.com.
- Login to your account.
- Go to the 'Routes' tab.
- Click 'Create Route'.
- Under 'Expression Type', select 'Match Recipient'.
- In the 'Recipient' box, type
.*@<your_domain_name>
.- Note: If your domain name will be used for more than just this email-sms integration, you may want a more sophisticated regular expression. If you are using this domain only for the email-sms integration, the catch-all expression will be sufficient.
- Under 'Actions', select 'Forward'.
- In the 'Forward' box, type
<your_ngrok_url>/send_bandwidth_message/
. - Click the 'Create Route' button.
The following environment variables need to be assigned:
- DJANGO_SECRET_KEY
- HOST_URL
- BANDWIDTH_USER_ID
- BANDWIDTH_API_TOKEN
- BANDWIDTH_API_SECRET
- BANDWIDTH_NUMBER
- MAILGUN_API_KEY
- MAILGUN_EMAIL_DOMAIN
- DESTINATION_EMAIL
- DEBUG
A django secret key can be generated by executing the following in a Python shell:
>>>from django.core.management import utils
>>>utils.get_random_secret_key()
<secret key>
>>>
For using ngrok, HOST_URL is found on your terminal running the ngrok session. Do not include the 'http://' or 'http://' part of the url (Note: you do include the 'http://' or 'https://' parts when setting the url in Bandwidth and Mailgun).
Your Bandwidth user id, api token, and api secret values can be found on app.bandwidth.com by clicking the 'Account' tab. BANDWIDTH_NUMBER is the Bandwidth number assigned to your Bandwidth application. This is the number that you want end users to send texts to and receive texts from.
Your Mailgun api key can be found on app.mailgun.com by clicking your account name on the top right corner and going to the security tab. MAILGUN_EMAIL_DOMAIN is the domain linked to your mailgun account. DESTINATION_EMAIL is the email (personal, business, etc.) that you want to receive messages from your end users.
Set DEBUG to False
Execute the following command python manage.py runserver
If you set your environment variables properly, you should see this:
Performing system checks...
System check identified no issues (0 silenced).
June 07, 2018 - 20:25:02
Django version 2.0.6, using settings 'email_sms_integration.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
You can now text your Bandwidth number, and you should receive an email from your Mailgun domain on your destination email. Replying to this email will send a reply text to the initial sender.