avatar
Untitled

clay 123 15th Mar, 2019

PYTHON 1.00 KB
                                           
                         from flask import Flask, request

app = Flask(__name__)
FB_API_URL = 'https://graph.facebook.com/v2.6/me/messages'
VERIFY_TOKEN = 'sercet'
PAGE_ACCESS_TOKEN = ''

def verify_webhook(req):
 if req.args.get('hub.verify_token') == VERIFY_TOKEN:
  return req.args.get('hub.challenge')
 else:
  return 'incorrect'

def get_bot_response(message):
 return 'text ' + str(message)

def respond(sender, message):
 response = get_bot_response(message)
 send_message(sender, response)

def is_user_message(message):
 return (message.get('message') and message['message'].get('text') and not message['message'].get('is_echo')

@app.route('/webhook')
def listen():
 if request.method == 'GET':
  return verify_webhook(request)
 elif request.method == 'POST':
  payload = request.json
  event = payload['entry'][0]['messaging']
  for x in event:
   if is_user_message(x):
    text = x['message']['text']
    sender_id = x['sender']['id']
    respond(sender_id, text)
   return 'ok'
                      
                                       
To share this paste please copy this url and send to your friends
RAW Paste Data
Recent Pastes