Idiris Mohamed
Idiris Mohamed

Reputation: 21

Run Twilio bot on AWS EC2 server (Flask)

I'm trying to get a Python Twilio bot to run on a EC2 server I just purchased (Ubuntu 16.04) so that the bot will be able to run 24/7.

I've essentially followed this tutorial (http://amunategui.github.io/idea-to-pitch/) to the bone, but instead of using a plagiarism detector, I put in the some basic Twilio replying code that replies to the message it was sent with Hello, you said [message_body_of_original_text]. It runs fine, but doesn't work when I close my computer.

I'm new to servers, Python, and Flask, so any advice would help greatly.

Basic code of the SMS replying bot:

from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse

app = Flask(__name__)


@app.route("/sms", methods=['GET', 'POST'])
def sms_reply():
    """Respond to incoming calls with a simple text message."""
    # Start our TwiML response
    resp = MessagingResponse()
    message_body = request.form['Body']
    print(message_body)
    # Add a message
    resp.message("Hello, you said {}".format(message_body))

    return str(resp)

if __name__ == "__main__":
    app.run(debug=True)

Upvotes: 0

Views: 373

Answers (2)

miknik
miknik

Reputation: 5941

Are you saying it runs fine on the remote server while your computer is connected to the terminal, but then stops when you log out? If so you are not alone, this was one of the very first python problems I ran into.

You can create a batch/cron job to start the python script automatically, or you can try the running the nohup command from the terminal:

nohup python myScript.py &

The script will now continue to run after you close the shell

Upvotes: 0

Luis Alvarez
Luis Alvarez

Reputation: 684

If you managed to make it run on your machine, you're half way there.

That github tutorial seems good, but extense. Appropriate for a production deployment, but not for a beginner's experiment.

There are a lot of moving pieces in that tutorial: Python code, Apache server, EC2 servers, firewalls, etc. I'd start debugging in incremental steps, without any of that nonsense.

First I'd run the application on the EC2, like I was running it on my machine: python run_my_twiliio_app.py. Your code is using the default values on the call to app.run(), which means your application would be listening on the port 5000 to requests that originate from your local server only. So you'd have to test it from within the server, by doing a local http request to the 5000 port: from within your server run wget localhost:5000/sms

Then I'd move to trying to reach it from outside the EC2. This requires:

  1. Telling the flask app to listen to requests coming from any IP: replace your app.run() with app.run(host='0.0.0.0')
  2. Opening the firewall to incoming requests from any IP through port 5000 (your application's port). In the case of AWS EC2s this involves modifying the rules of your security group via AWS web dashboard.

You should be able to test that this step worked by opening in your browser the.ip.of.ec2:5000/sms (You can get the IP of your EC2 from within AWS dashboard)

Only after I made sure those two steps worked correctly, I'd move to having Apache (or Nginx) serve my web application

Upvotes: 1

Related Questions