Building a personal assistant SMS bot

I use Siri and my Alexa every day and the microsoft Zo chat bot is always fun to mess with, but there's still so many things I wish I could do with them. Customized commands are seriously lacking in the current day commercial line up. I want to be able to manage servers, my personal internet of things, get alerts when something happens, and be able to mess with people. I want utility beyond asking to set an alarm or pause a song. 

Requirements

Flask>=0.8
twilio>=3.3.6
wikipedia>=1.4.0
pyowm>=2.6.1
wolframalpha>=3.0
git>=3.0


Creating your own bots is easier than ever so let's fill our lust for having things our own way by doing just that!

The best thing about doing this is how many dang options there are. A bot can be written in a number of languages using a ton of different services. You can have it running completely serverless through AWS, or as cost efficient as possible using mostly free services.

First off you'll need a Twilio account. This currently includes a free trial that'll work just find for getting everything working, but Twilio automatically adds a "Sent from your Twilio trial account" message to everything sent from them and there's no way around this that I've aware of besides paying. I'd love to be wrong so if anyone finds one I'd be happy to know! Overall Twilio fees are reasonable. Sending texts should cost you less than a cent each, and even with regular use its unlikely you'll send being doing more than a dozen messages a day. Even 12c a day is less than a dollar a week in costs.

Next for the bot! I personally wanted to run it from a server. I feel there's more control hosting it cause you can make it do literally anything. I used this project called [twilio-sms-bot by phdenzel] as a base. Simple enough. Everything we'll be using is either through Twilio, flask, or written in python. Giving ample opportunity for you to switch things up based on your skill level. Personally I mess things up all the time, especially simple things. So I'm gonna try to keep detail everything out. If you already have a prerequisite installed for example feel free to skill ahead.


For this project I'm just going to use one of my existing debian servers hosted on DigitalOcean and you're welcome to do the same, but as long as you know your way around the terminal on your distro of choise you should be fine. I do recommend linux for this, but I'm sure it can be worked out through windows too.

After SSHing into my server I quickly make sure to update my pakages with 

sudo apt update && upgrade

then make sure python with pip is installed.

sudo apt-get install python-pip python-dev build-essential

sudo -H pip install --upgrade pip

sudo -H pip install --upgrade virtualenv

Now we can clone the github and run the install shell script.

git clone https://github.com/phdenzel/twilio-sms-bot.git
cd twilio-sms-bot
./install.sh
Next up we gotta setup the API keys.We’re basically going to do two things here:

- Tell the bot what credentials it should use (Twilio + anything else you want).
- Make sure we don’t hardcode secrets directly into the code like a goblin.

Go to the Twilio Console and grab:

-Account SID
-Auth Token
-Your Twilio phone number (the one you’re texting)

Now on the server, I like to keep secrets in an env file. Create one:
cd ~/twilio-sms-bot
nano .env
Put this in it (fill in your values):
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_NUMBER=+15551234567
make sure that file isn’t world-readable:
chmod 600 .env

Now… we need the Python app to actually load this .env.
If the repo already loads env vars, cool. If not, we’ll do it the simple way with:
pip install python-dotenv

Then in the file that starts your Flask app (default is run-bot.py), add this near the top:
from dotenv import load_dotenv
load_dotenv()
Now os.environ["TWILIO_ACCOUNT_SID"] etc will work hopefully!!!


Make Twilio able to reach your bot (aka the webhook thing)

Twilio doesn’t “poll” your server. Your server has to be reachable and Twilio will POST inbound messages to it. You’ve got two common ways to do this:

First option: ngrok (fastest for testing)

This is the “I just want it working in 5 minutes” path.

Install ngrok, then run:

ngrok http 5000

You’ll get a forwarding URL like:

https://something-rando.ngrok.io

Keep that running.

Now go to Twilio Console > Phone Numbers > select your number > Messaging.

Set A message comes in to match the previous:

https://something-rando.ngrok.io/sms

(Your route might be /sms, /message, etc depending on the repo. We’ll confirm in a second.)

Save.

Now when you text your Twilio number, Twilio will hit that URL, which tunnels to your local Flask app.

Second Option: real server URL (better for “I want this always on”)

If you have a domain, set up HTTPS (just use Let’s Encrypt), and point Twilio at:

https://yourdomain.com/sms

If you don’t have HTTPS, you can test via HTTP, but Twilio + security + sanity = HTTPS.


Start the bot:

python run-bot.py

You should see Flask boot up.

Now text your Twilio number something simple like:

help

If it replies, congratulations. You’ve built your own little pocket demon! >:D



Pssssst ~ if it doesn’t reply:

  • Check Twilio Console > Messaging Logs (it’ll show webhook errors)

  • Make sure your route matches what Twilio is calling (/sms vs /bot etc)

  • Make sure Flask is listening on the correct port