Fb-chatbot / app.py
imseldrith's picture
Update app.py
fa48d29 verified
raw
history blame
7.32 kB
from flask import Flask, request, jsonify
import requests
from dotenv import load_dotenv
import os
import re
import json
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)
# Get response with possible image URLs
response_data = chat_completion(message_text, sender_id)
print("ChatBot Response:\n", response_data)
# Convert response_data to dictionary if it's a string
if isinstance(response_data, str):
response_data = json.loads(response_data)
print(type(response_data))
try:
# Ensure response_data is a dictionary
if isinstance(response_data, dict):
response_text = response_data.get('msg', '')
image_urls = response_data.get('image_urls', [])
print(f"ChatBot Response Text: {response_text}")
print(f"ChatBot Response Image URLs: {image_urls}")
# Send text and/or images
if response_text:
send_message(sender_id, response_text)
if image_urls:
for url in image_urls:
send_image(sender_id, url)
elif isinstance(response_data, str):
# Extract text and URLs from the string
response_text, image_urls = extract_text_and_urls(response_data)
print(f"Extracted Text: {response_text}")
print(f"Extracted Image URLs: {image_urls}")
# Send text and/or images
if response_text:
send_message(sender_id, response_text)
if image_urls:
for url in image_urls:
send_image(sender_id, url)
else:
print("Error: ChatBot response is not a dictionary.")
send_message(sender_id, response_data)
except Exception as e:
print(f"Exception occurred: {e}")
# Mark message as seen
mark_seen(sender_id)
# Set typing off after responding
set_typing_off(sender_id)
return jsonify({'status': 'ok'})
def extract_text_and_urls(input_string):
# Regex pattern to find URLs
url_pattern = re.compile(r'https?://\S+|www\.\S+')
# Find all URLs using the pattern
urls = re.findall(url_pattern, input_string)
# Remove URLs from the input string to get the remaining text
text = re.sub(url_pattern, '', input_string).strip()
return text, urls
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 send_image(recipient_id, image_url):
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': {
'attachment': {
'type': 'image',
'payload': {
'url': image_url,
'is_reusable': True
}
}
}
}
print(f"Sending image request: {payload}")
response = requests.post(url, headers=headers, json=payload)
print(f"Image 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}")
def mark_seen(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': 'mark_seen'
}
print(f"Sending mark seen request: {payload}")
response = requests.post(url, headers=headers, json=payload)
print(f"Mark seen response: {response.status_code}, {response.text}")
if __name__ == '__main__':
app.run(host="0.0.0.0", port=7860, debug=True)