wagent / app.py
nsquarzoni's picture
Update app.py
6e70e04 verified
# A very simple Flask Hello World app for you to get started with...
from flask import Flask, request, jsonify
import requests
from utilities import calculator, tt, tc, ag
app = Flask(__name__)
WEBHOOK_VERIFY_TOKEN = 'webhk'
GRAPH_API_TOKEN = 'EAANhU2sW6P4BO50pwnKcyUMj8K1ZBE2UDPp5EKzFoG28GQ40v4ZAdAepjVH4tRGd9kaBzp6jDpCpWFeJcTUZCiq2qA6jIl6jvuej7mbWUUgGdc6OUNO0JjrjjNSidcCXYzW0WhwcVxbV2dqxTDsZBHQBflJN8xLFwUrOwFfF3LkfIg03kGBlftjAZCy04j5lXyiRIZBR59OP9Poqgdt9vhRJZBDdQZDZD'
PORT = 7860
@app.route('/webhook', methods=['POST'])
def webhook():
data = request.json
#print("Incoming webhook message:"+ data.get('entry', [{}])[0].get('changes', [{}])[0].get('value', {}).get('messages', [{}])[0].get('text').get('body'))
text = data.get('entry', [{}])[0].get('changes', [{}])[0].get('value', {}).get('messages', [{}])[0].get('text').get('body')
#text = data.get('choices')[0].get('message').get('content')
message = data.get('entry', [{}])[0].get('changes', [{}])[0].get('value', {}).get('messages', [{}])[0]
if message.get('type') == 'text':
business_phone_number_id = data.get('entry', [{}])[0].get('changes', [{}])[0].get('value', {}).get('metadata', {}).get('phone_number_id')
# Send reply message
reply_url = f"https://graph.facebook.com/v18.0/{business_phone_number_id}/messages"
headers = {'Authorization': f'Bearer {GRAPH_API_TOKEN}'}
'''reply_data = {
'messaging_product': 'whatsapp',
'to': message.get('from'),
'text': {'body': f"Eccchho: {calculator(message.get('text', {}).get('body'))}"},
'context': {'message_id': message.get('id')}
}
requests.post(reply_url, headers=headers, json=reply_data)'''
'''reply_data = {
'messaging_product': 'whatsapp',
'to': message.get('from'),
'text': {'body': f"Eccchho2: {tc(message.get('text', {}).get('body'))}"},
'context': {'message_id': message.get('id')}
}
requests.post(reply_url, headers=headers, json=reply_data)'''
rep= ag(text)
reply_data = {
'messaging_product': 'whatsapp',
'to': message.get('from'),
'text': {'body': f"Eccchho3: {rep}"},
'context': {'message_id': message.get('id')}
}
requests.post(reply_url, headers=headers, json=reply_data)
# Mark message as read
read_data = {
'messaging_product': 'whatsapp',
'status': 'read',
'message_id': message.get('id')
}
requests.post(reply_url, headers=headers, json=read_data)
print('new message')
return 200
@app.route('/webhook', methods=['GET'])
def verify_webhook():
mode = request.args.get('hub.mode')
token = request.args.get('hub.verify_token')
challenge = request.args.get('hub.challenge')
if mode == 'subscribe' and token == WEBHOOK_VERIFY_TOKEN:
print("Webhook verified successfully!")
return challenge, 200
else:
return '', 403
@app.route('/')
def home():
return '<pre>Nothing to see here.\nCheckout README.md to start.</pre>'
if __name__ == '__main__':
app.run(port=PORT)