imseldrith commited on
Commit
2c9cbe2
·
verified ·
1 Parent(s): 6a35009

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -0
app.py CHANGED
@@ -2,6 +2,7 @@ from flask import Flask, request, jsonify
2
  import requests
3
  from dotenv import load_dotenv
4
  import os
 
5
  from helper.openai_api import chat_completion
6
 
7
  # Load environment variables from .env file
@@ -58,6 +59,9 @@ def webhook():
58
  # Get response with possible image URLs
59
  response_data = chat_completion(message_text, sender_id)
60
  print("ChatBot Response:\n", response_data)
 
 
 
61
  print(type(response_data))
62
  try:
63
 
@@ -75,6 +79,20 @@ def webhook():
75
  if image_urls:
76
  for url in image_urls:
77
  send_image(sender_id, url)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  else:
79
  print("Error: ChatBot response is not a dictionary.")
80
  send_message(sender_id, response_data)
@@ -88,6 +106,15 @@ def webhook():
88
 
89
  return jsonify({'status': 'ok'})
90
 
 
 
 
 
 
 
 
 
 
91
  def send_message(recipient_id, message_text):
92
  url = f'https://graph.facebook.com/v12.0/me/messages?access_token={PAGE_ACCESS_TOKEN}'
93
  headers = {'Content-Type': 'application/json'}
 
2
  import requests
3
  from dotenv import load_dotenv
4
  import os
5
+ import re
6
  from helper.openai_api import chat_completion
7
 
8
  # Load environment variables from .env file
 
59
  # Get response with possible image URLs
60
  response_data = chat_completion(message_text, sender_id)
61
  print("ChatBot Response:\n", response_data)
62
+ # Convert response_data to dictionary if it's a string
63
+ if isinstance(response_data, str):
64
+ response_data = json.loads(response_data)
65
  print(type(response_data))
66
  try:
67
 
 
79
  if image_urls:
80
  for url in image_urls:
81
  send_image(sender_id, url)
82
+ elif isinstance(response_data, str):
83
+ # Extract text and URLs from the string
84
+ response_text, image_urls = extract_text_and_urls(response_data)
85
+
86
+ print(f"Extracted Text: {response_text}")
87
+ print(f"Extracted Image URLs: {image_urls}")
88
+
89
+ # Send text and/or images
90
+ if response_text:
91
+ send_message(sender_id, response_text)
92
+ if image_urls:
93
+ for url in image_urls:
94
+ send_image(sender_id, url)
95
+
96
  else:
97
  print("Error: ChatBot response is not a dictionary.")
98
  send_message(sender_id, response_data)
 
106
 
107
  return jsonify({'status': 'ok'})
108
 
109
+ def extract_text_and_urls(input_string):
110
+ # Regex pattern to find URLs
111
+ url_pattern = re.compile(r'https?://\S+|www\.\S+')
112
+ # Find all URLs using the pattern
113
+ urls = re.findall(url_pattern, input_string)
114
+ # Remove URLs from the input string to get the remaining text
115
+ text = re.sub(url_pattern, '', input_string).strip()
116
+ return text, urls
117
+
118
  def send_message(recipient_id, message_text):
119
  url = f'https://graph.facebook.com/v12.0/me/messages?access_token={PAGE_ACCESS_TOKEN}'
120
  headers = {'Content-Type': 'application/json'}