-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_backup.py
77 lines (62 loc) · 2.78 KB
/
main_backup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from flask import Flask, render_template, request, jsonify, json
import requests
import traceback
import aiml
import os
app = Flask(__name__)
PAGE_ACCESS_TOKEN = 'EAAB159kkGrUBAChAXTIRzCb9G2be4LM1A1e0PWiSwZByUqsRHjc2VyX0jzZBe9yGvmIzjG0KpufOQo8wV5QBVxcP8V0zxojtooHlWL81rM5pZBjZBKANZC8fRuyaz4e6uwxAISFRCFjZAUdHLMZAkymOFORk3Fg6WAeKsvkCw2fmwZDZD'
kernel = aiml.Kernel()
if os.path.isfile("bot_brain.brn"):
kernel.bootstrap(brainFile = "bot_brain.brn")
else:
kernel.bootstrap(learnFiles = "std-startup.xml", commands = "load aiml b")
kernel.saveBrain("bot_brain.brn")
@app.route('/webhook', methods=['GET'])
def handle_verification():
print("Verificando token.")
if request.args.get('hub.verify_token', '') == '24789098':
print("Verificacion correcta.")
return request.args.get('hub.challenge')
else:
print("Verificacion fallida")
return 'Error, validacion de token'
@app.route('/webhook', methods=['POST'])
def handle_messages():
print("Obteniendo Mensajes")
payload = request.get_data()
# print(payload)
for sender, message in messaging_events(payload):
print("Obteniendo de %s: %s" % (sender, message))
send_message(PAGE_ACCESS_TOKEN, sender, message)
return "ok"
def messaging_events(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"], "Solo puedo manejar texto por ahora"
def send_message(token, recipient, text):
if text == 'Solo puedo manejar texto por ahora':
print("Mensaje por defecto")
bot_response = text
else:
req_name = requests.get("https://graph.facebook.com/v2.6/{0}?fields=first_name&access_token={1}".format(recipient,PAGE_ACCESS_TOKEN))
name = json.loads(req_name.text)
kernel.setPredicate("name", name["first_name"], recipient)
print(kernel.getPredicate("name", recipient))
bot_response = kernel.respond(text, recipient)
r = requests.post("https://graph.facebook.com/v2.6/me/messages",
params={"access_token": token},
data=json.dumps({
"recipient": {"id": recipient}, "message": {"text": bot_response}}),
headers={'Content-type': 'application/json'})
if r.status_code != requests.codes.ok:
print(r.text)
def call_geolocalitation_api(postalcode):
r = requests.get('http://api.geonames.org/postalCodeSearchJSON?postalcode={0}&maxRows=1&username=botlibre'.format(postalcode))
json_data = json.loads(r.text)
return json_data["postalCodes"][0]["adminName2"]
if __name__ == "__main__":
app.run(debug=True)