from flask import Flask, request
import json
import requests

app = Flask(__name__)

# This needs to be filled with the Page Access Token that will be provided
# by the Facebook App that will be created.
PAT = 'EAADhYj0lr14BACDUc9WI2quEuF8cksXZAoOzesZAhes6MgukqS0EQbIlPbehlt69PgZAOuXW5SWQ4C9XFZBJ4aB4MoBBpsBOK5qVRnooHXoqivEoHtd2Sg9OXcGi6DyykIoJF3rNwYotBiMkrA8sBvl9cKB8lCbEl0QHTVZClEQZDZD'

@app.route('/', methods=['GET'])
def handle_verification():
  print "Handling Verification."
  if request.args.get('hub.verify_token', '') == 'baddest_ass_bot_you_know':
    print "Verification successful!"
    return request.args.get('hub.challenge', '')
  else:
    print "Verification failed!"
    return 'Error, wrong validation token'

@app.route('/', methods=['POST'])
def handle_messages():
  print "Handling Messages"
  payload = request.get_data()
  print payload
  for sender, message in messaging_events(payload):
    print "Incoming from %s: %s" % (sender, message)
    send_message(PAT, sender, message)
  return "ok"

def messaging_events(payload):
  """Generate tuples of (sender_id, message_text) from the
  provided payload.
  """
  data = json.loads(payload)
  messaging_events = data["entry"][0]["messaging"]
  for event in messaging_events:
    if "message" in event and "text" in event["message"]:
      yield event["sender"]["id"], event["message"]["text"].encode('unicode_escape')
    else:
      yield event["sender"]["id"], "I can't echo this"


def send_message(token, recipient, text):
  """Send the message text to recipient with id recipient.
  """

  r = requests.post("https://graph.facebook.com/v2.6/me/messages",
    params={"access_token": token},
    data=json.dumps({
      "recipient": {"id": recipient},
      "message": {"text": text.decode('unicode_escape')}
    }),
    headers={'Content-type': 'application/json'})
  if r.status_code != requests.codes.ok:
    print r.text

if __name__ == '__main__':
  app.run()

from flask import Flask, request
import json
import requests

app = Flask(__name__)

# This needs to be filled with the Page Access Token that will be provided
# by the Facebook App that will be created.
PAT = 'EAADhYj0lr14BACDUc9WI2quEuF8cksXZAoOzesZAhes6MgukqS0EQbIlPbehlt69PgZAOuXW5SWQ4C9XFZBJ4aB4MoBBpsBOK5qVRnooHXoqivEoHtd2Sg9OXcGi6DyykIoJF3rNwYotBiMkrA8sBvl9cKB8lCbEl0QHTVZClEQZDZD'

@app.route('/', methods=['GET'])
def handle_verification():
  print "Handling Verification."
  if request.args.get('hub.verify_token', '') == 'baddest_ass_bot_you_know':
    print "Verification successful!"
    return request.args.get('hub.challenge', '')
  else:
    print "Verification failed!"
    return 'Error, wrong validation token'

@app.route('/', methods=['POST'])
def handle_messages():
  print "Handling Messages"
  payload = request.get_data()
  print payload
  for sender, message in messaging_events(payload):
    print "Incoming from %s: %s" % (sender, message)
    send_message(PAT, sender, message)
  return "ok"

def messaging_events(payload):
  """Generate tuples of (sender_id, message_text) from the
  provided payload.
  """
  data = json.loads(payload)
  messaging_events = data["entry"][0]["messaging"]
  for event in messaging_events:
    if "message" in event and "text" in event["message"]:
      yield event["sender"]["id"], event["message"]["text"].encode('unicode_escape')
    else if "attachments" in event["message"]:
      if "location" == event['message']['attachments'][0]["type"]:
        print "Received location"
        coordinates = event['message']['attachments'][0]['payload']['coordinates']
        latitude = coordinates['lat']
        longitude = coordinates['long']
        yield event["sender"]["id"], "I received your location: %s, %s"%(latitude, longitude)
      else if "audio" == event['message']['attachments'][0]["type"]:
        audio_url = event['message']['attachments'][0]['payload']['url']
        yield event["sender"]["id"], "I received your audio: %s".format(audio_url)
      else:
        yield event["sender"]["id"], "I can't echo this"
    else:
      yield event["sender"]["id"], "I can't echo this"


def send_message(token, recipient, text):
  """Send the message text to recipient with id recipient.
  """

  r = requests.post("https://graph.facebook.com/v2.6/me/messages",
    params={"access_token": token},
    data=json.dumps({
      "recipient": {"id": recipient},
      "message": {"text": text.decode('unicode_escape')}
    }),
    headers={'Content-type': 'application/json'})
  if r.status_code != requests.codes.ok:
    print r.text

if __name__ == '__main__':
  app.run()

Add support for location and audio