How to connect Twilio python program to local server?
Question: So I saw this amazing hackathon project, and I would like to test it out.

Here is the link for your reference: <https://github.com/ionambrinoc/oxhack>

I know how to run android apps. Just run it on Android Eclipse, and it works
as I expected. It sends the messages to the twilio backend. I got the project
from github, and I want to try it, but their twilio IDs expired. So I got a
new twilio account, and I tried to replace their ids with mine. And I assumed
that App SIDs are the same as App Secret Keys. (Are they?)

I tried to run my code with `python hello.py` on terminal but I got this HUGE
error.

`TypeError TypeError: expected string or buffer ... The debugger caught an
exception in your WSGI application. You can now look at the traceback which
led to the error... You can execute arbitrary Python code in the stack frames
and there are some extra helpers available for introspection: dump() shows all
variables in the frame dump(obj) dumps all that's known about the object`

I also have some questions about my code. I put the questions in comments in
my python code. Questions are in CAPS

    
    
    from flask import Flask, session, redirect, url_for, escape, request
    from twilio.rest import TwilioRestClient
    import twilio.twiml
    import requests
    import json
    import httplib
    
    account_sid = "SOME_ACCOUNT_SID"
    auth_token  = "SOME_AUTH_TOKEN"
    client = TwilioRestClient(account_sid, auth_token)
    app = Flask(__name__)
    @app.route('/', methods=['GET', 'POST']) #WHAT DOES THIS MEAN?
    def index():
        phonenumber = request.values.get('From', None)
        body = request.values.get('Body', None)
        print(body)
        action = json.loads(body)
        current = action['current']
        desired = action['desired']
        lang = action['lang']
        payload={'current':current,'desired':desired,'lang':lang}
        url='http://school/index.php' #The dev used this link. WHAT DOES IT MEAN?
        r = requests.post(url,data=payload)
        print(payload)
        print(r.text)
        response=r.text
        message = client.messages.create(body=response,
                                to=phonenumber,
                                from_="+1408MYTWILIO")
        return message.sid
    # IS THE APP SID SAME AS APP SECRET KEY?
    app.secret_key = 'MY_APP_SECRET_KEY'
    if __name__ == '__main__':
        app.run(debug=True)
    

If you can answer any of these questions, your help will be greatly
appreciate. As I am still learning, and I really want to see how this
hackathon project works.


Answer: Twilio developer evangelist here.

I can explain some of what is going on there, but probably not all of it I'm
afraid.

Firstly:

    
    
    @app.route('/', methods=['GET', 'POST']) #WHAT DOES THIS MEAN?
    

This is a decorator function in python. In the context of this application it
applies to the method below `def index():`. It means that when the Flask
application receives a `GET` or `POST` request to `'/'` (the root path) it
will execute the `index` method.

    
    
    # IS THE APP SID SAME AS APP SECRET KEY?
    app.secret_key = 'MY_APP_SECRET_KEY'
    

The secret key for the application is not any of the credentials you get from
Twilio. The secret key should be a long random string. It is used with
sessions in Flask and the secret key is used to sign the session
cryptographically. There is more information on this in the [Flask
documentation on
settings](http://flask.pocoo.org/docs/0.10/quickstart/#sessions).

The following lines however:

    
    
    payload={'current':current,'desired':desired,'lang':lang}
    url='http://school/index.php' #The dev used this link. WHAT DOES IT MEAN?
    r = requests.post(url,data=payload)
    

including the odd `http://school/index.php`, I can't really explain. What I do
know is that the hack used the [Esri API](https://developers.arcgis.com/en/)
to find directions between two points. It looks as though this section is
trying to do that. I have no idea what was behind that URL for the team, it
looks as though it may have been some sort of proxy. In order to get this
running again, you may need to look through the [Esri API
documentation](https://developers.arcgis.com/documentation/) and find out how
to get the text description for a route between two places. If you need help
with that, I suggest you give [James
Milner](https://twitter.com/jameslmilner), Esri developer evangelist, a shout.

Hope this helps!

