XX-WB / app.py
CORVO-AI's picture
Update app.py
f286579 verified
from flask import Flask, request, jsonify
import requests
import random
import string
import time
app = Flask(__name__)
# Global variables to store workspace and bot IDs
GLOBAL_WORKSPACE_ID = None
GLOBAL_BOT_ID = None
# Cookie value used in requests (should be updated with a valid cookie),
AUTH_COOKIE = "pscd=try.botpress.com; _hjSessionUser_2931810=eyJpZCI6ImQ2MGMzYjhkLTlkMjQtNTA0OS1hMzlmLWEzNmI0NzA0NzUxNCIsImNyZWF0ZWQiOjE3MzU3MTg0MDcwNTAsImV4aXN0aW5nIjp0cnVlfQ==; hubspotutk=75739411a4d011b2164c4f3d944ecb94; intercom-device-id-bjzkw2xf=afd0a36b-b229-44e3-828e-60483c80c10c; _hjSessionUser_3339867=eyJpZCI6IjU4ODlmMTY4LWRkNGEtNTJhZS1hZTUzLWZlYWQwM2ZmMTVjNyIsImNyZWF0ZWQiOjE3MzU3MTg1ODM4MDgsImV4aXN0aW5nIjp0cnVlfQ==; __hstc=59821234.75739411a4d011b2164c4f3d944ecb94.1735718442141.1746194848836.1746538539919.88; mp_1195923e954ce61d822842b5832047cd_mixpanel=%7B%22distinct_id%22%3A%20%22d403ad7b-ea73-4d29-b977-5fd95afd585c%22%2C%22%24device_id%22%3A%20%22d403ad7b-ea73-4d29-b977-5fd95afd585c%22%2C%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fapp.botpress.cloud%2F%22%2C%22%24initial_referring_domain%22%3A%20%22app.botpress.cloud%22%2C%22__mps%22%3A%20%7B%7D%2C%22__mpso%22%3A%20%7B%22%24initial_referrer%22%3A%20%22https%3A%2F%2Fapp.botpress.cloud%2F%22%2C%22%24initial_referring_domain%22%3A%20%22app.botpress.cloud%22%7D%2C%22__mpus%22%3A%20%7B%7D%2C%22__mpa%22%3A%20%7B%7D%2C%22__mpu%22%3A%20%7B%7D%2C%22__mpr%22%3A%20%5B%5D%2C%22__mpap%22%3A%20%5B%5D%2C%22%24user_id%22%3A%20%22d403ad7b-ea73-4d29-b977-5fd95afd585c%22%7D; intercom-session-bjzkw2xf=TTdnZGNWUC9xNXMreE80NXhRZFNZS0pyUEdsbkJRc2JMcXdGZmcveVRPYkxZTmVnVnhqMUhJTWlDcEpVcWljeDZVYVVSblN4YnV5S0xBdWxDd2swQjZiaUZTeWl5M1psRmtoUWJwUU9FSFE9LS1Bay9zNldJTmVhUFdwMFNReFRmcXB3PT0=--68abc5394d7aab99748f3e451637cab5d9152a4c; _ga=GA1.2.1726154447.1735718383; _gid=GA1.2.1619749406.1746821934; _gat_UA-226900660-1=1; _gat_UA-226900660-2=1; _ga_W6YT9YSNLH=GS2.2.s1746821948$o89$g0$t1746821948$j0$l0$h0; _ga_CYSS87Q508=GS2.2.s1746821949$o89$g0$t1746821949$j0$l0$h0; _hjSession_2931810=eyJpZCI6IjA1YTlkMzY3LWNiZWEtNGQ3OC04YzNiLTEzNTFjNjkxYzViZSIsImMiOjE3NDY4MjE5NTU2NjYsInMiOjAsInIiOjAsInNiIjowLCJzciI6MCwic2UiOjAsImZzIjowLCJzcCI6MH0=; _ga_PCC6TBWJY6=GS2.1.s1746821926$o120$g1$t1746821960$j0$l0$h0; _ga_HKHSWES9V9=GS2.1.s1746821929$o120$g1$t1746821960$j29$l0$h193244206; csrf_token_bd9ac21c34b9f0915e733c3e5305d737d0722c1168be7376b889426b5ec2a298=pIDxu4npODUGEpo7JHVQrKZ4GFaa3U+3BpgxaV5hcVw=; ory_kratos_session=MTc0NjgyMjAyOHxfUmdMaVlTQXVfSmxlT1lJSGpyU2FhbjVUTHg0R0ZsQWgtVm00M3pHcXZwVG9yNW1qRDJheUFGaFZvNmFEUVdBOThQR014RjJJbmhUMmhIV1I1ME5UVHZkTDNpMUMtQlRjZ1ZTbE55M19Pb2dHTF9vQlJoSGlBQnRRWUp0M1ZUdnVvcENLeVhOTllWNk1zMk11bFVPOWFrTzJMTTdxMmVteUozVVRDMWE5TVIxbDgzU3dUY2VQaDBRWDN4bDJUVm8yUkZQa19sb09GbzlFZHF2MDFQcVR6bVVWVVpDLXVoQ1lXMEh2LV9Sd2VNZXM1cjM4TGZPVTJqdW5xNTBETTBDYkppU0xNU2xicUk3Z2EyMnFkVmdyQT09fEYiv7pXcfXVnpIFi4JLGgDObQAchyJCoAwGDSkFkoX7; ajs_user_id=d403ad7b-ea73-4d29-b977-5fd95afd585c; ajs_anonymous_id=cda6139d-cb82-4906-bfac-adaea115b097"
# -------------------------------------------------------------------
# Helper functions for random bot/workspace names
# -------------------------------------------------------------------
def generate_random_name(length=5):
"""Generate a random name for workspace or bot"""
return ''.join(random.choices(string.ascii_letters, k=length))
# -------------------------------------------------------------------
# Functions to create/delete workspaces and bots
# -------------------------------------------------------------------
def create_workspace():
"""Create a new workspace and return its ID"""
ws_url = "https://api.botpress.cloud/v1/admin/workspaces"
headers = {
"User-Agent": "Mozilla/5.0",
"Cookie": AUTH_COOKIE
}
payload = {"name": generate_random_name()}
try:
response = requests.post(ws_url, headers=headers, json=payload)
if response.status_code == 200:
response_json = response.json()
workspace_id = response_json.get('id')
print(f"Successfully created workspace: {workspace_id}")
return workspace_id
else:
print(f"Workspace creation failed with: {response.status_code}, {response.text}")
return None
except Exception as e:
print(f"Error creating workspace: {str(e)}")
return None
def create_bot(workspace_id):
"""Create a new bot in the specified workspace and return its ID"""
if not workspace_id:
print("Cannot create bot: No workspace ID provided")
return None
bot_url = "https://api.botpress.cloud/v1/admin/bots"
headers = {
"User-Agent": "Mozilla/5.0",
"x-workspace-id": workspace_id,
"Cookie": AUTH_COOKIE,
"Content-Type": "application/json"
}
payload = {"name": generate_random_name()}
try:
response = requests.post(bot_url, headers=headers, json=payload)
if response.status_code == 200:
response_json = response.json()
bot_id = response_json.get("bot", {}).get("id")
if not bot_id:
print("Bot ID not found in the response.")
return None
print(f"Successfully created bot: {bot_id} in workspace: {workspace_id}")
# Install integration for the new bot
integration_success = install_bot_integration(bot_id, workspace_id)
if integration_success:
print(f"Successfully installed integration for bot {bot_id}")
return bot_id
else:
print(f"Failed to install integration for bot {bot_id}")
return bot_id # Still return the bot ID even if integration fails
else:
print(f"Bot creation failed with: {response.status_code}, {response.text}")
return None
except Exception as e:
print(f"Error creating bot: {str(e)}")
return None
def install_bot_integration(bot_id, workspace_id):
"""Install required integration for the bot to function properly"""
if not bot_id or not workspace_id:
print("Cannot install integration: Missing bot ID or workspace ID")
return False
url = f"https://api.botpress.cloud/v1/admin/bots/{bot_id}"
headers = {
"User-Agent": "Mozilla/5.0",
"Cookie": AUTH_COOKIE,
"Content-Type": "application/json",
"x-bot-id": bot_id,
"x-workspace-id": workspace_id
}
# Integration payload
payload = {
"integrations": {
"intver_01JSCCBZGK79MXK2FSHKHTRKFD": {
"enabled": True
}
}
}
try:
response = requests.put(url, headers=headers, json=payload)
if response.status_code == 200:
print(f"Successfully installed integration for bot {bot_id}")
return True
else:
print(f"Failed to install integration: {response.status_code}, {response.text}")
return False
except Exception as e:
print(f"Error installing integration: {str(e)}")
return False
def delete_bot(bot_id, workspace_id):
"""Delete a bot from the specified workspace"""
if not bot_id or not workspace_id:
print("Cannot delete bot: Missing bot ID or workspace ID")
return False
url = f"https://api.botpress.cloud/v1/admin/bots/{bot_id}"
headers = {
"User-Agent": "Mozilla/5.0",
"x-workspace-id": workspace_id,
"Cookie": AUTH_COOKIE
}
try:
response = requests.delete(url, headers=headers)
if response.status_code in [200, 204]:
print(f"Successfully deleted bot: {bot_id}")
return True
else:
print(f"Failed to delete bot: {response.status_code}, {response.text}")
return False
except Exception as e:
print(f"Error deleting bot: {str(e)}")
return False
def delete_workspace(workspace_id):
"""Delete a workspace"""
if not workspace_id:
print("Cannot delete workspace: No workspace ID provided")
return False
url = f"https://api.botpress.cloud/v1/admin/workspaces/{workspace_id}"
headers = {
"User-Agent": "Mozilla/5.0",
"Cookie": AUTH_COOKIE
}
try:
response = requests.delete(url, headers=headers)
if response.status_code in [200, 204]:
print(f"Successfully deleted workspace: {workspace_id}")
return True
else:
print(f"Failed to delete workspace: {response.status_code}, {response.text}")
return False
except Exception as e:
print(f"Error deleting workspace: {str(e)}")
return False
# -------------------------------------------------------------------
# Web Search Function
# -------------------------------------------------------------------
def web_search(query, count=3, include_sites=None, exclude_sites=None, bot_id=None, workspace_id=None):
"""
Performs a web search using the Botpress API
Args:
query (str): The search query
count (int): Number of search results to return
include_sites (list): List of sites to include in the search
exclude_sites (list): List of sites to exclude from the search
bot_id (str): The bot ID to use for the request
workspace_id (str): The workspace ID to use for the request
Returns:
tuple: (search_results, bot_id, workspace_id)
"""
if not bot_id or not workspace_id:
print("Cannot perform search: Missing bot ID or workspace ID")
return {"error": "Missing bot ID or workspace ID"}, bot_id, workspace_id
# Prepare the headers
headers = {
"User-Agent": "Mozilla/5.0",
"x-bot-id": bot_id,
"x-workspace-id": workspace_id ,
"Content-Type": "application/json",
"Cookie": AUTH_COOKIE
}
# Prepare include/exclude sites
include_sites = include_sites or []
exclude_sites = exclude_sites or []
# Prepare the payload for the API
payload = {
"type": "browser:webSearch",
"input": {
"query": query,
"includeSites": include_sites,
"excludeSites": exclude_sites,
"count": count
}
}
botpress_url = "https://api.botpress.cloud/v1/chat/actions"
max_retries = 3
timeout = 60
# Flag to track if we need to create new IDs due to quota exceeded
quota_exceeded = False
# Attempt to send the request
for attempt in range(max_retries):
try:
print(f"Attempt {attempt+1}: Sending search request to Botpress API with bot_id={bot_id}, workspace_id={workspace_id}")
response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
# If successful (200)
if response.status_code == 200:
data = response.json()
print(f"Successfully received search results from Botpress API")
return data, bot_id, workspace_id
# Check for quota exceeded error specifically
elif response.status_code == 403:
error_data = response.json()
error_message = error_data.get('message', '')
# Check if this is the specific quota exceeded error
if "has reached its usage limit for ai spend" in error_message:
print(f"Quota exceeded error detected: {error_message}")
quota_exceeded = True
break
else:
print(f"Received 403 error but not quota exceeded: {error_message}")
if attempt < max_retries - 1:
time.sleep(2)
continue
else:
return {"error": f"Unable to get search results (Error 403)."}, bot_id, workspace_id
# Handle network errors or timeouts (just retry)
elif response.status_code in [404, 408, 502, 503, 504]:
print(f"Received error {response.status_code}. Retrying...")
time.sleep(3) # Wait before retrying
continue
# Any other error status code
else:
print(f"Received unexpected error: {response.status_code}, {response.text}")
if attempt < max_retries - 1:
time.sleep(2)
continue
else:
return {"error": f"Unable to get search results (Error {response.status_code})."}, bot_id, workspace_id
except requests.exceptions.Timeout:
print(f"Request timed out. Retrying...")
if attempt < max_retries - 1:
time.sleep(2)
continue
else:
return {"error": "The search request timed out. Please try again."}, bot_id, workspace_id
except Exception as e:
print(f"Error during request: {str(e)}")
if attempt < max_retries - 1:
time.sleep(2)
continue
else:
return {"error": f"Unable to get search results: {str(e)}"}, bot_id, workspace_id
# If quota exceeded, we need to create new resources
if quota_exceeded:
print("Quota exceeded. Creating new workspace and bot...")
# First delete the bot, then the workspace (in that order)
if bot_id and workspace_id:
print(f"Deleting bot {bot_id} first...")
delete_success = delete_bot(bot_id, workspace_id)
if delete_success:
print(f"Successfully deleted bot {bot_id}")
else:
print(f"Failed to delete bot {bot_id}")
# Wait 3 seconds before deleting the workspace
print("Waiting 3 seconds before deleting workspace...")
time.sleep(3)
print(f"Now deleting workspace {workspace_id}...")
ws_delete_success = delete_workspace(workspace_id)
if ws_delete_success:
print(f"Successfully deleted workspace {workspace_id}")
else:
print(f"Failed to delete workspace {workspace_id}")
# Create new workspace
new_workspace_id = create_workspace()
if not new_workspace_id:
return {"error": "Failed to create a new workspace after quota exceeded. Please try again later."}, bot_id, workspace_id
# Create new bot in the new workspace
new_bot_id = create_bot(new_workspace_id)
if not new_bot_id:
return {"error": "Failed to create a new bot after quota exceeded. Please try again later."}, new_workspace_id, workspace_id
# Update headers with new bot ID
headers["x-bot-id"] = new_bot_id
# Try one more time with the new IDs
try:
print(f"Retrying with new bot_id={new_bot_id}, workspace_id={new_workspace_id}")
retry_response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
if retry_response.status_code == 200:
data = retry_response.json()
print(f"Successfully received search results with new IDs")
return data, new_bot_id, new_workspace_id
else:
print(f"Failed with new IDs: {retry_response.status_code}, {retry_response.text}")
return {"error": f"Unable to get search results with new credentials."}, new_bot_id, new_workspace_id
except Exception as e:
print(f"Error with new IDs: {str(e)}")
return {"error": f"Unable to get search results with new credentials: {str(e)}"}, new_bot_id, new_workspace_id
# Should not reach here due to the handling in the loop
return {"error": "Unable to get search results."}, bot_id, workspace_id
# -------------------------------------------------------------------
# Flask Endpoints
# -------------------------------------------------------------------
@app.route("/search", methods=["POST"])
def search_endpoint():
"""
Endpoint for web search.
Expects JSON with:
{
"query": "search query",
"count": 3, // Optional, defaults to 3
"include_sites": [], // Optional, defaults to empty list
"exclude_sites": [] // Optional, defaults to empty list
}
Returns the raw search results JSON from the Botpress API
"""
global GLOBAL_WORKSPACE_ID, GLOBAL_BOT_ID
# Parse JSON from request
data = request.get_json(force=True)
query = data.get("query", "")
count = data.get("count", 3)
include_sites = data.get("include_sites", [])
exclude_sites = data.get("exclude_sites", [])
# Validate query
if not query:
return jsonify({"error": "Query parameter is required"}), 400
# Validate count
try:
count = int(count)
if count <= 0:
count = 3
print(f"Invalid count value. Using default: {count}")
except (ValueError, TypeError):
count = 3
print(f"Invalid count format. Using default: {count}")
# If we don't yet have a workspace or bot, create them
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
print("No existing IDs found. Creating new workspace and bot...")
GLOBAL_WORKSPACE_ID = create_workspace()
if GLOBAL_WORKSPACE_ID:
GLOBAL_BOT_ID = create_bot(GLOBAL_WORKSPACE_ID)
# If creation failed
if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
return jsonify({"error": "Failed to initialize search capability. Please try again later."}), 500
# Call our search function
print(f"Sending search request with query='{query}', count={count}")
print(f"Using existing bot_id={GLOBAL_BOT_ID}, workspace_id={GLOBAL_WORKSPACE_ID}")
search_results, updated_bot_id, updated_workspace_id = web_search(
query,
count,
include_sites,
exclude_sites,
GLOBAL_BOT_ID,
GLOBAL_WORKSPACE_ID
)
# Update global IDs if they changed
if updated_bot_id != GLOBAL_BOT_ID or updated_workspace_id != GLOBAL_WORKSPACE_ID:
print(f"Updating global IDs: bot_id={updated_bot_id}, workspace_id={updated_workspace_id}")
GLOBAL_BOT_ID = updated_bot_id
GLOBAL_WORKSPACE_ID = updated_workspace_id
# Return the raw API response
return jsonify(search_results)
# -------------------------------------------------------------------
# Run the Flask app
# -------------------------------------------------------------------
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=True)