Spaces:
Sleeping
Sleeping
File size: 3,884 Bytes
67793ba 7364bee e0834c3 fa795f1 b132330 cf9fdca e0834c3 92b3530 e0834c3 7364bee cf9fdca e0834c3 cf9fdca 38b2ac5 67793ba 4bdfabe 38b2ac5 7364bee cf9fdca ab0b4df 7364bee ab0b4df 7364bee ab0b4df 7364bee ab0b4df 67793ba ab0b4df 67793ba 80da867 b132330 80da867 7364bee 67793ba 38b2ac5 cf9fdca 38b2ac5 ab0b4df 92b3530 80da867 e0834c3 80da867 e0834c3 80da867 92b3530 cf9fdca |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
from flask import Flask, request, jsonify
import requests
from dotenv import load_dotenv
import os
from helper.openai_api import chat_completion
# Load environment variables from .env file
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 (remove in production)
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)
result = chat_completion(message_text)
response_text = result.get('response', '')
#response_text = 'hello'
print(f"Sending response to {sender_id}: {response_text}")
send_message(sender_id, response_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={PAGE_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)
|