Skip to content

Instantly share code, notes, and snippets.

@M3nin0
Created January 8, 2018 22:41
Show Gist options
  • Save M3nin0/545dc6219064d92d70ee968c8683e93e to your computer and use it in GitHub Desktop.
Save M3nin0/545dc6219064d92d70ee968c8683e93e to your computer and use it in GitHub Desktop.
import os
import json
from flask import Flask, request
import requests
import traceback
'''
Estrutura do json carregado pelo app
{
"tokens": {
"fb_token": "",
"fb_token_verify": ""
}
}
'''
# Carregando token
with open('config.json') as config:
temp = json.load(config)
token = temp['tokens']['fb_token']
token_verify = temp['tokens']['fb_token_verify']
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def webhook():
if request.method == 'POST':
try:
data = json.loads(request.data.decode())
text = data['entry'][0]['messaging'][0]['message']['text']
sender = data['entry'][0]['messaging'][0]['sender']['id']
if text == 'oi':
resposta = 'Oi! Como vai você ?'
else:
resposta = 'Eu não sei =D'
payload = {'recipient': {'id': sender}, 'message': {'text': resposta}}
r = requests.post('https://graph.facebook.com/v2.6/me/messages/?access_token=' + token, json=payload)
except BaseException as e:
print(e)
elif request.method == 'GET':
if request.args.get('hub.verify_token') == token_verify:
return request.args.get('hub.challenge')
return 'Wrong Verify Token'
return 'Nothing'
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment