-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
50 lines (44 loc) · 2.09 KB
/
main.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
from flask import Flask, request, jsonify
from bot import ChatBot
import os
from dotenv import load_dotenv
load_dotenv()
chatbot = ChatBot()
app = Flask(__name__)
#* THIS IS THE WEBHOOK
@app.route('/webhook', methods=['GET', 'POST'])
def webhook():
if request.method == 'GET':
hmode = request.args.get('hub.mode')
htoken = request.args.get('hub.verify_token')
hchallenge = request.args.get('hub.challenge')
if (hmode == 'subscribe') and (htoken == os.getenv('BUSINESS_TOKEN')):
return hchallenge
else:
return 'Error', 400
if request.method == 'POST':
# serialize the request data into a object to access the data easily
request_data = (request.get_json())
changes = request_data['entry'][0]['changes'][0]
if (request_data['object'] == 'whatsapp_business_account') and (changes['field'] == 'messages'):
if not 'statuses' in changes['value']:
print('############ No esta en el campo statuses ############')
print('########### THE CHANGES ############',request.get_json())
sender_phone = changes['value']['contacts'][0]['wa_id']
if changes['value']['messages'][0]['type'] == 'text':
# Get the message
message = changes['value']['messages'][0]['text']['body']
additional_data = changes['value']
# Send the message to the chatbot
chatbot.proccess_message(sender_phone,message,additional_data)
# catch the response from the message of type button
if changes['value']['messages'][0]['type'] == 'interactive':
reply = changes['value']['messages'][0]['interactive']
chatbot.proccess_message_interactive(sender_phone,reply)
# return jsonify(request_data),200
return 'OK', 200
else:
return 'Error', 400
if __name__ == "__main__":
app.run(debug=False, port=os.getenv("PORT", default=5000))
########### THE CHANGES ############