from flask import Flask, request, jsonify import requests from dotenv import load_dotenv import os load_dotenv() app = Flask(__name__) # Retrieve environment variables VERIFY_TOKEN = os.getenv('VERIFY_TOKEN') PAGE_ACCESS_TOKEN = os.getenv('PAGE_ACCESS_TOKEN') OPENAI_API_KEY = os.getenv('OPENAI_API_KEY') # Debugging: Print the loaded environment variables print(f"VERIFY_TOKEN: {VERIFY_TOKEN}") print(f"PAGE_ACCESS_TOKEN: {PAGE_ACCESS_TOKEN}") print(f"OPENAI_API_KEY: {OPENAI_API_KEY}") @app.route('/', methods=['GET']) def home(): return "Welcome to the chatbot!" @app.route('/facebook', methods=['GET', 'POST']) def webhook(): if request.method == 'GET': # Webhook verification verify_token = request.args.get('hub.verify_token') challenge = request.args.get('hub.challenge') print(f"GET request received: verify_token={VERIFY_TOKEN}, challenge={challenge}") if verify_token == VERIFY_TOKEN: print("Verification token matches. Returning challenge.") return challenge else: print("Error: wrong validation token.") return 'Error, wrong validation token' elif request.method == 'POST': data = request.get_json() print(f"POST request received: {data}") if 'entry' in data and len(data['entry']) > 0 and 'messaging' in data['entry'][0]: messaging_event = data['entry'][0]['messaging'][0] print(f"Messaging event: {messaging_event}") if 'message' in messaging_event: sender_id = messaging_event['sender']['id'] message_text = messaging_event['message'].get('text', '') print(f"Received message from {sender_id}: {message_text}") # Set typing on set_typing_on(sender_id) if message_text.lower() == 'hi': response_text = 'hello' print(f"Sending response to {sender_id}: {response_text}") send_message(sender_id, response_text) else: print(f"No action taken for message: {message_text}") # Set typing off after responding set_typing_off(sender_id) return jsonify({'status': 'ok'}) def send_message(recipient_id, message_text): url = f'https://graph.facebook.com/v12.0/me/messages?access_token={ACCESS_TOKEN}' headers = {'Content-Type': 'application/json'} payload = { 'recipient': {'id': recipient_id}, 'message': {'text': message_text} } print(f"Sending message request: {payload}") response = requests.post(url, headers=headers, json=payload) print(f"Message send response: {response.status_code}, {response.text}") def set_typing_on(recipient_id): url = f'https://graph.facebook.com/v12.0/me/messages?access_token={PAGE_ACCESS_TOKEN}' headers = {'Content-Type': 'application/json'} payload = { 'recipient': {'id': recipient_id}, 'sender_action': 'typing_on' } print(f"Sending typing on request: {payload}") response = requests.post(url, headers=headers, json=payload) print(f"Typing on response: {response.status_code}, {response.text}") def set_typing_off(recipient_id): url = f'https://graph.facebook.com/v12.0/me/messages?access_token={PAGE_ACCESS_TOKEN}' headers = {'Content-Type': 'application/json'} payload = { 'recipient': {'id': recipient_id}, 'sender_action': 'typing_off' } print(f"Sending typing off request: {payload}") response = requests.post(url, headers=headers, json=payload) print(f"Typing off response: {response.status_code}, {response.text}") if __name__ == '__main__': app.run(host="0.0.0.0", port=7860, debug=True)