CORVO-AI commited on
Commit
e1d36ec
·
verified ·
1 Parent(s): dc1b1c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +184 -99
app.py CHANGED
@@ -2,6 +2,7 @@ from flask import Flask, request, jsonify
2
  import requests
3
  import random
4
  import string
 
5
 
6
  app = Flask(__name__)
7
 
@@ -9,7 +10,10 @@ app = Flask(__name__)
9
  # Global variables to hold workspace and bot IDs across requests
10
  # -------------------------------------------------------------------
11
  GLOBAL_WORKSPACE_ID = "wkspace_01JMHB3QHE7GRWYTGAQV7KGWYN"
12
- GLOBAL_BOT_ID = "768ba379-9d54-436a-a438-6eed0bc6a4b0"
 
 
 
13
 
14
 
15
  # -------------------------------------------------------------------
@@ -26,146 +30,225 @@ def create_workspace():
26
  ws_url = "https://api.botpress.cloud/v1/admin/workspaces"
27
  headers = {
28
  "User-Agent": "Mozilla/5.0",
29
- # Replace with your valid cookie below
30
- "cookie": "pscd=try.botpress.com; _gcl_au=1.1.2059955793.1735718381; _hjSessionUser_2931810=eyJpZCI6ImQ2MGMzYjhkLTlkMjQtNTA0OS1hMzlmLWEzNmI0NzA0NzUxNCIsImNyZWF0ZWQiOjE3MzU3MTg0MDcwNTAsImV4aXN0aW5nIjp0cnVlfQ==; hubspotutk=75739411a4d011b2164c4f3d944ecb94; intercom-device-id-bjzkw2xf=afd0a36b-b229-44e3-828e-60483c80c10c; _hjSessionUser_3339867=eyJpZCI6IjU4ODlmMTY4LWRkNGEtNTJhZS1hZTUzLWZlYWQwM2ZmMTVjNyIsImNyZWF0ZWQiOjE3MzU3MTg1ODM4MDgsImV4aXN0aW5nIjp0cnVlfQ==; mp_1195923e954ce61d822842b5832047cd_mixpanel=%7B%22distinct_id%22%3A%20%22anonymous%22%2C%22%24device_id%22%3A%20%22anonymous%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%7D; csrf_token_bd9ac21c34b9f0915e733c3e5305d737d0722c1168be7376b889426b5ec2a298=bB0iEVQewW7/y7AXh62+8RwDCxwDgtPnCBtc4A9z1e0=; ory_kratos_session=MTc0MTI2MzIyM3w1SHF2Y0VvczBhS3hDaWFPVmV2WlFsWWJFYmFrNWdVMGpndGZEcnRtcU5JR2NSejNabGFJWDBhRkN6UWJpT21ILWcydHhwb21LdGhfTVNycFkzblRhZGRuSjU0dGdRNHBXUHlqOFZSaFJEZnoxYklxTjRicTVsSlJBRGQ1Vmc2bk1wNVFNRDRLZVJMM2NLQ2RoZENMZUYxZVlhN3FIWUdUaUR1TzNYekJSTUh3NXdMTWktcjNxVTRQSUljSkRsUVVuRTUxM0wyTzctZGVnZXB4cDdhSUc1aERDQ3VpQUFmT05Wd2pXMkFIeS12Ty1kTF9CRzRncTdIcElhMWZlMkFjbmMzcXpEd1NUeTZvMHZVclZUODBPZz09fA6qpO0Au1OyH-59O1SwM5mq5gyMyx_2XrDmAulQo2W7; ajs_user_id=d403ad7b-ea73-4d29-b977-5fd95afd585c; ajs_anonymous_id=cda6139d-cb82-4906-bfac-adaea115b097; __hstc=59821234.75739411a4d011b2164c4f3d944ecb94.1735718442141.1740216976796.1741285217391.50; _ga_CYSS87Q508=GS1.2.1741285205.50.1.1741285220.0.0.0; _ga_W6YT9YSNLH=GS1.2.1741285204.50.1.1741285220.0.0.0; intercom-session-bjzkw2xf=bUhCbWZKY29aeXdtQjF4MmFSdE9LMjFVTTAzMThQNk5CU2RNWXJmcERPL1ZoMllmOUN0M3FYWHluTDArOU1wRkgrZXpGMXhYdHEwY0RxaVdOdVAxdFhyRnFqQ0ZYSnJPd2FTWkRxMUxqR1k9LS16dGtZdnZCSnlWVzZtQmx0OHV2WFdRPT0=--ddd74fc3758dd5ee94b13b08e2a6b5379357b06f; _ga_HKHSWES9V9=GS1.1.1741285202.64.1.1741285223.39.0.1401653535; _hjSession_2931810=eyJpZCI6IjY4MDdjZjIxLTlmMzYtNGNiNy1iZTNhLTZmNDMyMTc0MzZjNyIsImMiOjE3NDEzNzk0OTk3MjgsInMiOjAsInIiOjAsInNiIjowLCJzciI6MCwic2UiOjAsImZzIjowLCJzcCI6MH0=; _gid=GA1.2.270620776.1741379504; _gat_UA-226900660-1=1; _gat_UA-226900660-2=1; _ga=GA1.1.1726154447.1735718383; _ga_PCC6TBWJY6="
31
  }
32
  payload = {"name": generate_random_name()}
33
- response = requests.post(ws_url, headers=headers, json=payload)
34
 
35
- if response.status_code == 200:
36
- response_json = response.json()
37
- return response_json.get('id')
38
- else:
39
- print(f"Workspace creation failed with: {response.status_code}, {response.text}")
 
 
 
 
 
40
  return None
41
 
42
 
43
  def create_bot(workspace_id):
44
  bot_url = "https://api.botpress.cloud/v1/admin/bots"
45
  headers = {
46
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
47
  "x-workspace-id": workspace_id,
48
- # Replace with your valid cookie below
49
- "cookie": "pscd=try.botpress.com; _gcl_au=1.1.2059955793.1735718381; _hjSessionUser_2931810=eyJpZCI6ImQ2MGMzYjhkLTlkMjQtNTA0OS1hMzlmLWEzNmI0NzA0NzUxNCIsImNyZWF0ZWQiOjE3MzU3MTg0MDcwNTAsImV4aXN0aW5nIjp0cnVlfQ==; hubspotutk=75739411a4d011b2164c4f3d944ecb94; intercom-device-id-bjzkw2xf=afd0a36b-b229-44e3-828e-60483c80c10c; _hjSessionUser_3339867=eyJpZCI6IjU4ODlmMTY4LWRkNGEtNTJhZS1hZTUzLWZlYWQwM2ZmMTVjNyIsImNyZWF0ZWQiOjE3MzU3MTg1ODM4MDgsImV4aXN0aW5nIjp0cnVlfQ==; mp_1195923e954ce61d822842b5832047cd_mixpanel=%7B%22distinct_id%22%3A%20%22anonymous%22%2C%22%24device_id%22%3A%20%22anonymous%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%7D; csrf_token_bd9ac21c34b9f0915e733c3e5305d737d0722c1168be7376b889426b5ec2a298=bB0iEVQewW7/y7AXh62+8RwDCxwDgtPnCBtc4A9z1e0=; ory_kratos_session=MTc0MTI2MzIyM3w1SHF2Y0VvczBhS3hDaWFPVmV2WlFsWWJFYmFrNWdVMGpndGZEcnRtcU5JR2NSejNabGFJWDBhRkN6UWJpT21ILWcydHhwb21LdGhfTVNycFkzblRhZGRuSjU0dGdRNHBXUHlqOFZSaFJEZnoxYklxTjRicTVsSlJBRGQ1Vmc2bk1wNVFNRDRLZVJMM2NLQ2RoZENMZUYxZVlhN3FIWUdUaUR1TzNYekJSTUh3NXdMTWktcjNxVTRQSUljSkRsUVVuRTUxM0wyTzctZGVnZXB4cDdhSUc1aERDQ3VpQUFmT05Wd2pXMkFIeS12Ty1kTF9CRzRncTdIcElhMWZlMkFjbmMzcXpEd1NUeTZvMHZVclZUODBPZz09fA6qpO0Au1OyH-59O1SwM5mq5gyMyx_2XrDmAulQo2W7; ajs_user_id=d403ad7b-ea73-4d29-b977-5fd95afd585c; ajs_anonymous_id=cda6139d-cb82-4906-bfac-adaea115b097; __hstc=59821234.75739411a4d011b2164c4f3d944ecb94.1735718442141.1740216976796.1741285217391.50; _ga_CYSS87Q508=GS1.2.1741285205.50.1.1741285220.0.0.0; _ga_W6YT9YSNLH=GS1.2.1741285204.50.1.1741285220.0.0.0; intercom-session-bjzkw2xf=bUhCbWZKY29aeXdtQjF4MmFSdE9LMjFVTTAzMThQNk5CU2RNWXJmcERPL1ZoMllmOUN0M3FYWHluTDArOU1wRkgrZXpGMXhYdHEwY0RxaVdOdVAxdFhyRnFqQ0ZYSnJPd2FTWkRxMUxqR1k9LS16dGtZdnZCSnlWVzZtQmx0OHV2WFdRPT0=--ddd74fc3758dd5ee94b13b08e2a6b5379357b06f; _ga_HKHSWES9V9=GS1.1.1741285202.64.1.1741285223.39.0.1401653535; _hjSession_2931810=eyJpZCI6IjY4MDdjZjIxLTlmMzYtNGNiNy1iZTNhLTZmNDMyMTc0MzZjNyIsImMiOjE3NDEzNzk0OTk3MjgsInMiOjAsInIiOjAsInNiIjowLCJzciI6MCwic2UiOjAsImZzIjowLCJzcCI6MH0=; _gid=GA1.2.270620776.1741379504; _gat_UA-226900660-1=1; _gat_UA-226900660-2=1; _ga=GA1.1.1726154447.1735718383; _ga_PCC6TBWJY6="
50
  }
51
  payload = {"name": generate_random_name()}
52
- response = requests.post(bot_url, headers=headers, json=payload)
53
-
54
- if response.status_code == 200:
55
- response_json = response.json()
56
- bot_id = response_json.get("bot", {}).get("id")
57
- if not bot_id:
58
- print("Bot ID not found in the response.")
59
- return bot_id
60
- else:
61
- print(f"Bot creation failed with: {response.status_code}, {response.text}")
 
 
 
 
 
 
 
 
 
62
  return None
63
 
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  def delete_bot(bot_id, workspace_id):
66
  url = f"https://api.botpress.cloud/v1/admin/bots/{bot_id}"
67
  headers = {
68
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
69
  "x-workspace-id": workspace_id,
70
- # Replace with your valid cookie below
71
- "cookie": "pscd=try.botpress.com; _gcl_au=1.1.2059955793.1735718381; _hjSessionUser_2931810=eyJpZCI6ImQ2MGMzYjhkLTlkMjQtNTA0OS1hMzlmLWEzNmI0NzA0NzUxNCIsImNyZWF0ZWQiOjE3MzU3MTg0MDcwNTAsImV4aXN0aW5nIjp0cnVlfQ==; hubspotutk=75739411a4d011b2164c4f3d944ecb94; intercom-device-id-bjzkw2xf=afd0a36b-b229-44e3-828e-60483c80c10c; _hjSessionUser_3339867=eyJpZCI6IjU4ODlmMTY4LWRkNGEtNTJhZS1hZTUzLWZlYWQwM2ZmMTVjNyIsImNyZWF0ZWQiOjE3MzU3MTg1ODM4MDgsImV4aXN0aW5nIjp0cnVlfQ==; mp_1195923e954ce61d822842b5832047cd_mixpanel=%7B%22distinct_id%22%3A%20%22anonymous%22%2C%22%24device_id%22%3A%20%22anonymous%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%7D; csrf_token_bd9ac21c34b9f0915e733c3e5305d737d0722c1168be7376b889426b5ec2a298=bB0iEVQewW7/y7AXh62+8RwDCxwDgtPnCBtc4A9z1e0=; ory_kratos_session=MTc0MTI2MzIyM3w1SHF2Y0VvczBhS3hDaWFPVmV2WlFsWWJFYmFrNWdVMGpndGZEcnRtcU5JR2NSejNabGFJWDBhRkN6UWJpT21ILWcydHhwb21LdGhfTVNycFkzblRhZGRuSjU0dGdRNHBXUHlqOFZSaFJEZnoxYklxTjRicTVsSlJBRGQ1Vmc2bk1wNVFNRDRLZVJMM2NLQ2RoZENMZUYxZVlhN3FIWUdUaUR1TzNYekJSTUh3NXdMTWktcjNxVTRQSUljSkRsUVVuRTUxM0wyTzctZGVnZXB4cDdhSUc1aERDQ3VpQUFmT05Wd2pXMkFIeS12Ty1kTF9CRzRncTdIcElhMWZlMkFjbmMzcXpEd1NUeTZvMHZVclZUODBPZz09fA6qpO0Au1OyH-59O1SwM5mq5gyMyx_2XrDmAulQo2W7; ajs_user_id=d403ad7b-ea73-4d29-b977-5fd95afd585c; ajs_anonymous_id=cda6139d-cb82-4906-bfac-adaea115b097; __hstc=59821234.75739411a4d011b2164c4f3d944ecb94.1735718442141.1740216976796.1741285217391.50; _ga_CYSS87Q508=GS1.2.1741285205.50.1.1741285220.0.0.0; _ga_W6YT9YSNLH=GS1.2.1741285204.50.1.1741285220.0.0.0; intercom-session-bjzkw2xf=bUhCbWZKY29aeXdtQjF4MmFSdE9LMjFVTTAzMThQNk5CU2RNWXJmcERPL1ZoMllmOUN0M3FYWHluTDArOU1wRkgrZXpGMXhYdHEwY0RxaVdOdVAxdFhyRnFqQ0ZYSnJPd2FTWkRxMUxqR1k9LS16dGtZdnZCSnlWVzZtQmx0OHV2WFdRPT0=--ddd74fc3758dd5ee94b13b08e2a6b5379357b06f; _ga_HKHSWES9V9=GS1.1.1741285202.64.1.1741285223.39.0.1401653535; _hjSession_2931810=eyJpZCI6IjY4MDdjZjIxLTlmMzYtNGNiNy1iZTNhLTZmNDMyMTc0MzZjNyIsImMiOjE3NDEzNzk0OTk3MjgsInMiOjAsInIiOjAsInNiIjowLCJzciI6MCwic2UiOjAsImZzIjowLCJzcCI6MH0=; _gid=GA1.2.270620776.1741379504; _gat_UA-226900660-1=1; _gat_UA-226900660-2=1; _ga=GA1.1.1726154447.1735718383; _ga_PCC6TBWJY6="
72
  }
73
- return requests.delete(url, headers=headers)
 
 
 
 
 
74
 
75
 
76
  def delete_workspace(workspace_id):
77
  url = f"https://api.botpress.cloud/v1/admin/workspaces/{workspace_id}"
78
  headers = {
79
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
80
- # Replace with your valid cookie below
81
- "cookie": "pscd=try.botpress.com; _gcl_au=1.1.2059955793.1735718381; _hjSessionUser_2931810=eyJpZCI6ImQ2MGMzYjhkLTlkMjQtNTA0OS1hMzlmLWEzNmI0NzA0NzUxNCIsImNyZWF0ZWQiOjE3MzU3MTg0MDcwNTAsImV4aXN0aW5nIjp0cnVlfQ==; hubspotutk=75739411a4d011b2164c4f3d944ecb94; intercom-device-id-bjzkw2xf=afd0a36b-b229-44e3-828e-60483c80c10c; _hjSessionUser_3339867=eyJpZCI6IjU4ODlmMTY4LWRkNGEtNTJhZS1hZTUzLWZlYWQwM2ZmMTVjNyIsImNyZWF0ZWQiOjE3MzU3MTg1ODM4MDgsImV4aXN0aW5nIjp0cnVlfQ==; mp_1195923e954ce61d822842b5832047cd_mixpanel=%7B%22distinct_id%22%3A%20%22anonymous%22%2C%22%24device_id%22%3A%20%22anonymous%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%7D; csrf_token_bd9ac21c34b9f0915e733c3e5305d737d0722c1168be7376b889426b5ec2a298=bB0iEVQewW7/y7AXh62+8RwDCxwDgtPnCBtc4A9z1e0=; ory_kratos_session=MTc0MTI2MzIyM3w1SHF2Y0VvczBhS3hDaWFPVmV2WlFsWWJFYmFrNWdVMGpndGZEcnRtcU5JR2NSejNabGFJWDBhRkN6UWJpT21ILWcydHhwb21LdGhfTVNycFkzblRhZGRuSjU0dGdRNHBXUHlqOFZSaFJEZnoxYklxTjRicTVsSlJBRGQ1Vmc2bk1wNVFNRDRLZVJMM2NLQ2RoZENMZUYxZVlhN3FIWUdUaUR1TzNYekJSTUh3NXdMTWktcjNxVTRQSUljSkRsUVVuRTUxM0wyTzctZGVnZXB4cDdhSUc1aERDQ3VpQUFmT05Wd2pXMkFIeS12Ty1kTF9CRzRncTdIcElhMWZlMkFjbmMzcXpEd1NUeTZvMHZVclZUODBPZz09fA6qpO0Au1OyH-59O1SwM5mq5gyMyx_2XrDmAulQo2W7; ajs_user_id=d403ad7b-ea73-4d29-b977-5fd95afd585c; ajs_anonymous_id=cda6139d-cb82-4906-bfac-adaea115b097; __hstc=59821234.75739411a4d011b2164c4f3d944ecb94.1735718442141.1740216976796.1741285217391.50; _ga_CYSS87Q508=GS1.2.1741285205.50.1.1741285220.0.0.0; _ga_W6YT9YSNLH=GS1.2.1741285204.50.1.1741285220.0.0.0; intercom-session-bjzkw2xf=bUhCbWZKY29aeXdtQjF4MmFSdE9LMjFVTTAzMThQNk5CU2RNWXJmcERPL1ZoMllmOUN0M3FYWHluTDArOU1wRkgrZXpGMXhYdHEwY0RxaVdOdVAxdFhyRnFqQ0ZYSnJPd2FTWkRxMUxqR1k9LS16dGtZdnZCSnlWVzZtQmx0OHV2WFdRPT0=--ddd74fc3758dd5ee94b13b08e2a6b5379357b06f; _ga_HKHSWES9V9=GS1.1.1741285202.64.1.1741285223.39.0.1401653535; _hjSession_2931810=eyJpZCI6IjY4MDdjZjIxLTlmMzYtNGNiNy1iZTNhLTZmNDMyMTc0MzZjNyIsImMiOjE3NDEzNzk0OTk3MjgsInMiOjAsInIiOjAsInNiIjowLCJzciI6MCwic2UiOjAsImZzIjowLCJzcCI6MH0=; _gid=GA1.2.270620776.1741379504; _gat_UA-226900660-1=1; _gat_UA-226900660-2=1; _ga=GA1.1.1726154447.1735718383; _ga_PCC6TBWJY6="
82
  }
83
- return requests.delete(url, headers=headers)
 
 
 
 
 
84
 
85
 
86
  # -------------------------------------------------------------------
87
- # Main function that calls the Botpress GPT-4 endpoint
88
  # -------------------------------------------------------------------
89
  def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
90
  """
91
- Sends the user input and chat history to the Botpress GPT-4 endpoint,
92
  returns the assistant's response and (possibly updated) bot/workspace IDs.
93
  """
94
-
95
  # Prepare the headers
96
  headers = {
97
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
98
- "x-bot-id": bot_id, # existing bot ID (could be None on first try)
99
  "Content-Type": "application/json",
100
- # Replace with your valid cookie below
101
- "Cookie": "pscd=try.botpress.com; _gcl_au=1.1.2059955793.1735718381; _hjSessionUser_2931810=eyJpZCI6ImQ2MGMzYjhkLTlkMjQtNTA0OS1hMzlmLWEzNmI0NzA0NzUxNCIsImNyZWF0ZWQiOjE3MzU3MTg0MDcwNTAsImV4aXN0aW5nIjp0cnVlfQ==; hubspotutk=75739411a4d011b2164c4f3d944ecb94; intercom-device-id-bjzkw2xf=afd0a36b-b229-44e3-828e-60483c80c10c; _hjSessionUser_3339867=eyJpZCI6IjU4ODlmMTY4LWRkNGEtNTJhZS1hZTUzLWZlYWQwM2ZmMTVjNyIsImNyZWF0ZWQiOjE3MzU3MTg1ODM4MDgsImV4aXN0aW5nIjp0cnVlfQ==; mp_1195923e954ce61d822842b5832047cd_mixpanel=%7B%22distinct_id%22%3A%20%22anonymous%22%2C%22%24device_id%22%3A%20%22anonymous%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%7D; csrf_token_bd9ac21c34b9f0915e733c3e5305d737d0722c1168be7376b889426b5ec2a298=bB0iEVQewW7/y7AXh62+8RwDCxwDgtPnCBtc4A9z1e0=; ory_kratos_session=MTc0MTI2MzIyM3w1SHF2Y0VvczBhS3hDaWFPVmV2WlFsWWJFYmFrNWdVMGpndGZEcnRtcU5JR2NSejNabGFJWDBhRkN6UWJpT21ILWcydHhwb21LdGhfTVNycFkzblRhZGRuSjU0dGdRNHBXUHlqOFZSaFJEZnoxYklxTjRicTVsSlJBRGQ1Vmc2bk1wNVFNRDRLZVJMM2NLQ2RoZENMZUYxZVlhN3FIWUdUaUR1TzNYekJSTUh3NXdMTWktcjNxVTRQSUljSkRsUVVuRTUxM0wyTzctZGVnZXB4cDdhSUc1aERDQ3VpQUFmT05Wd2pXMkFIeS12Ty1kTF9CRzRncTdIcElhMWZlMkFjbmMzcXpEd1NUeTZvMHZVclZUODBPZz09fA6qpO0Au1OyH-59O1SwM5mq5gyMyx_2XrDmAulQo2W7; ajs_user_id=d403ad7b-ea73-4d29-b977-5fd95afd585c; ajs_anonymous_id=cda6139d-cb82-4906-bfac-adaea115b097; __hstc=59821234.75739411a4d011b2164c4f3d944ecb94.1735718442141.1740216976796.1741285217391.50; _ga_CYSS87Q508=GS1.2.1741285205.50.1.1741285220.0.0.0; _ga_W6YT9YSNLH=GS1.2.1741285204.50.1.1741285220.0.0.0; intercom-session-bjzkw2xf=bUhCbWZKY29aeXdtQjF4MmFSdE9LMjFVTTAzMThQNk5CU2RNWXJmcERPL1ZoMllmOUN0M3FYWHluTDArOU1wRkgrZXpGMXhYdHEwY0RxaVdOdVAxdFhyRnFqQ0ZYSnJPd2FTWkRxMUxqR1k9LS16dGtZdnZCSnlWVzZtQmx0OHV2WFdRPT0=--ddd74fc3758dd5ee94b13b08e2a6b5379357b06f; _ga_HKHSWES9V9=GS1.1.1741285202.64.1.1741285223.39.0.1401653535; _hjSession_2931810=eyJpZCI6IjY4MDdjZjIxLTlmMzYtNGNiNy1iZTNhLTZmNDMyMTc0MzZjNyIsImMiOjE3NDEzNzk0OTk3MjgsInMiOjAsInIiOjAsInNiIjowLCJzciI6MCwic2UiOjAsImZzIjowLCJzcCI6MH0=; _gid=GA1.2.270620776.1741379504; _gat_UA-226900660-1=1; _gat_UA-226900660-2=1; _ga=GA1.1.1726154447.1735718383; _ga_PCC6TBWJY6="
102
  }
103
 
104
- # Prepare the payload
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  payload = {
106
- "prompt": {
107
- "messages": chat_history, # from user request
108
- "model": "gpt-4o",
 
 
 
 
109
  "temperature": 0.75,
110
- "signatureVersion": "Jan-2024"
111
- },
112
- "variables": {
113
- "TASK_INPUT": ""
114
- },
115
- "options": {
116
- "origin": "cards/ai-task"
117
- },
118
- "origin": "cards/ai-task"
119
  }
120
 
121
- botpress_url = "https://api.botpress.cloud/v1/cognitive/chat-gpt/query"
 
 
122
 
123
  # Attempt to send the request
124
- try:
125
- response = requests.post(botpress_url, json=payload, headers=headers)
 
126
 
127
- # If successful (200)
128
- if response.status_code == 200:
129
- data = response.json()
130
- assistant_content = data.get('choices', [{}])[0].get('message', {}).get('content', '')
131
- return assistant_content, bot_id, workspace_id
132
-
133
- # If we get a 403, it could be because the bot/workspace IDs are invalid/expired
134
- elif response.status_code == 403:
135
- raise Exception("Invalid or expired bot ID.")
136
-
137
- # Other errors
138
- else:
139
- return f"Error {response.status_code}: {response.text}", bot_id, workspace_id
140
 
141
- except Exception as e:
142
- # If invalid/expired bot, create new workspace/bot and retry
143
- if "Invalid or expired bot ID" in str(e):
144
- # Attempt to delete old IDs if they exist
145
- if bot_id and workspace_id:
146
- delete_bot(bot_id, workspace_id)
147
- delete_workspace(workspace_id)
148
-
149
- # Create fresh IDs
150
- new_workspace = create_workspace()
151
- new_bot = create_bot(new_workspace)
152
- if not new_workspace or not new_bot:
153
- return "Failed to regenerate workspace or bot IDs.", None, None
154
-
155
- # Update headers with the new bot ID
156
- headers["x-bot-id"] = new_bot
157
-
158
- # Retry
159
- retry_response = requests.post(botpress_url, json=payload, headers=headers)
160
- if retry_response.status_code == 200:
161
- data = retry_response.json()
162
- assistant_content = data.get('choices', [{}])[0].get('message', {}).get('content', '')
163
- return assistant_content, new_bot, new_workspace
164
  else:
165
- return f"Error {retry_response.status_code}: {retry_response.text}", new_bot, new_workspace
166
- else:
167
- # Other exceptions
168
- return f"Unexpected error: {str(e)}", bot_id, workspace_id
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
 
171
  # -------------------------------------------------------------------
@@ -198,13 +281,14 @@ def chat_endpoint():
198
  # If we don't yet have a workspace or bot, create them
199
  if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
200
  GLOBAL_WORKSPACE_ID = create_workspace()
201
- GLOBAL_BOT_ID = create_bot(GLOBAL_WORKSPACE_ID)
 
202
 
203
  # If creation failed
204
  if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
205
- return jsonify({"assistant_response": "Could not create workspace or bot."}), 500
206
 
207
- # Call our function that interacts with Botpress GPT-4
208
  assistant_response, updated_bot_id, updated_workspace_id = chat_with_assistant(
209
  user_input,
210
  chat_history,
@@ -213,14 +297,15 @@ def chat_endpoint():
213
  )
214
 
215
  # Update global IDs if they changed
216
- GLOBAL_BOT_ID = updated_bot_id
217
- GLOBAL_WORKSPACE_ID = updated_workspace_id
 
218
 
219
  return jsonify({"assistant_response": assistant_response})
220
 
221
 
222
  # -------------------------------------------------------------------
223
- # Run the Flask app (example)
224
  # -------------------------------------------------------------------
225
  if __name__ == "__main__":
226
  app.run(host="0.0.0.0", port=7860, debug=True)
 
2
  import requests
3
  import random
4
  import string
5
+ import time
6
 
7
  app = Flask(__name__)
8
 
 
10
  # Global variables to hold workspace and bot IDs across requests
11
  # -------------------------------------------------------------------
12
  GLOBAL_WORKSPACE_ID = "wkspace_01JMHB3QHE7GRWYTGAQV7KGWYN"
13
+ GLOBAL_BOT_ID = "6d6e5198-a4aa-4eb6-9617-67cdde390664"
14
+
15
+ # Cookie value used in requests (should be updated with a valid cookie)
16
+ AUTH_COOKIE = "pscd=try.botpress.com; _gcl_au=1.1.2059955793.1735718381; _hjSessionUser_2931810=eyJpZCI6ImQ2MGMzYjhkLTlkMjQtNTA0OS1hMzlmLWEzNmI0NzA0NzUxNCIsImNyZWF0ZWQiOjE3MzU3MTg0MDcwNTAsImV4aXN0aW5nIjp0cnVlfQ==; hubspotutk=75739411a4d011b2164c4f3d944ecb94; intercom-device-id-bjzkw2xf=afd0a36b-b229-44e3-828e-60483c80c10c; _hjSessionUser_3339867=eyJpZCI6IjU4ODlmMTY4LWRkNGEtNTJhZS1hZTUzLWZlYWQwM2ZmMTVjNyIsImNyZWF0ZWQiOjE3MzU3MTg1ODM4MDgsImV4aXN0aW5nIjp0cnVlfQ==; mp_1195923e954ce61d822842b5832047cd_mixpanel=%7B%22distinct_id%22%3A%20%22anonymous%22%2C%22%24device_id%22%3A%20%22anonymous%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%7D; csrf_token_bd9ac21c34b9f0915e733c3e5305d737d0722c1168be7376b889426b5ec2a298=bB0iEVQewW7/y7AXh62+8RwDCxwDgtPnCBtc4A9z1e0=; ory_kratos_session=MTc0MTI2MzIyM3w1SHF2Y0VvczBhS3hDaWFPVmV2WlFsWWJFYmFrNWdVMGpndGZEcnRtcU5JR2NSejNabGFJWDBhRkN6UWJpT21ILWcydHhwb21LdGhfTVNycFkzblRhZGRuSjU0dGdRNHBXUHlqOFZSaFJEZnoxYklxTjRicTVsSlJBRGQ1Vmc2bk1wNVFNRDRLZVJMM2NLQ2RoZENMZUYxZVlhN3FIWUdUaUR1TzNYekJSTUh3NXdMTWktcjNxVTRQSUljSkRsUVVuRTUxM0wyTzctZGVnZXB4cDdhSUc1aERDQ3VpQUFmT05Wd2pXMkFIeS12Ty1kTF9CRzRncTdIcElhMWZlMkFjbmMzcXpEd1NUeTZvMHZVclZUODBPZz09fA6qpO0Au1OyH-59O1SwM5mq5gyMyx_2XrDmAulQo2W7; ajs_user_id=d403ad7b-ea73-4d29-b977-5fd95afd585c; ajs_anonymous_id=cda6139d-cb82-4906-bfac-adaea115b097; __hstc=59821234.75739411a4d011b2164c4f3d944ecb94.1735718442141.1740216976796.1741285217391.50; _ga_CYSS87Q508=GS1.2.1741285205.50.1.1741285220.0.0.0; _ga_W6YT9YSNLH=GS1.2.1741285204.50.1.1741285220.0.0.0; intercom-session-bjzkw2xf=bUhCbWZKY29aeXdtQjF4MmFSdE9LMjFVTTAzMThQNk5CU2RNWXJmcERPL1ZoMllmOUN0M3FYWHluTDArOU1wRkgrZXpGMXhYdHEwY0RxaVdOdVAxdFhyRnFqQ0ZYSnJPd2FTWkRxMUxqR1k9LS16dGtZdnZCSnlWVzZtQmx0OHV2WFdRPT0=--ddd74fc3758dd5ee94b13b08e2a6b5379357b06f; _ga_HKHSWES9V9=GS1.1.1741285202.64.1.1741285223.39.0.1401653535; _hjSession_2931810=eyJpZCI6IjY4MDdjZjIxLTlmMzYtNGNiNy1iZTNhLTZmNDMyMTc0MzZjNyIsImMiOjE3NDEzNzk0OTk3MjgsInMiOjAsInIiOjAsInNiIjowLCJzciI6MCwic2UiOjAsImZzIjowLCJzcCI6MH0=; _gid=GA1.2.270620776.1741379504; _gat_UA-226900660-1=1; _gat_UA-226900660-2=1; _ga=GA1.1.1726154447.1735718383; _ga_PCC6TBWJY6="
17
 
18
 
19
  # -------------------------------------------------------------------
 
30
  ws_url = "https://api.botpress.cloud/v1/admin/workspaces"
31
  headers = {
32
  "User-Agent": "Mozilla/5.0",
33
+ "Cookie": AUTH_COOKIE
 
34
  }
35
  payload = {"name": generate_random_name()}
 
36
 
37
+ try:
38
+ response = requests.post(ws_url, headers=headers, json=payload)
39
+ if response.status_code == 200:
40
+ response_json = response.json()
41
+ return response_json.get('id')
42
+ else:
43
+ print(f"Workspace creation failed with: {response.status_code}, {response.text}")
44
+ return None
45
+ except Exception as e:
46
+ print(f"Error creating workspace: {str(e)}")
47
  return None
48
 
49
 
50
  def create_bot(workspace_id):
51
  bot_url = "https://api.botpress.cloud/v1/admin/bots"
52
  headers = {
53
+ "User-Agent": "Mozilla/5.0",
54
  "x-workspace-id": workspace_id,
55
+ "Cookie": AUTH_COOKIE,
56
+ "Content-Type": "application/json"
57
  }
58
  payload = {"name": generate_random_name()}
59
+
60
+ try:
61
+ response = requests.post(bot_url, headers=headers, json=payload)
62
+ if response.status_code == 200:
63
+ response_json = response.json()
64
+ bot_id = response_json.get("bot", {}).get("id")
65
+ if not bot_id:
66
+ print("Bot ID not found in the response.")
67
+
68
+ # Install integration for the new bot
69
+ if bot_id:
70
+ install_bot_integration(bot_id, workspace_id)
71
+
72
+ return bot_id
73
+ else:
74
+ print(f"Bot creation failed with: {response.status_code}, {response.text}")
75
+ return None
76
+ except Exception as e:
77
+ print(f"Error creating bot: {str(e)}")
78
  return None
79
 
80
 
81
+ def install_bot_integration(bot_id, workspace_id):
82
+ """Install required integration for the bot to function properly"""
83
+ url = f"https://api.botpress.cloud/v1/admin/bots/{bot_id}"
84
+
85
+ headers = {
86
+ "User-Agent": "Mozilla/5.0",
87
+ "Cookie": AUTH_COOKIE,
88
+ "Content-Type": "application/json",
89
+ "x-bot-id": bot_id,
90
+ "x-workspace-id": workspace_id
91
+ }
92
+
93
+ # Integration payload
94
+ payload = {
95
+ "integrations": {
96
+ "intver_01JN7C5S1SHM0EC647KWF8AZ0T": {
97
+ "enabled": True
98
+ }
99
+ }
100
+ }
101
+
102
+ try:
103
+ response = requests.put(url, headers=headers, json=payload)
104
+ if response.status_code == 200:
105
+ print(f"Successfully installed integration for bot {bot_id}")
106
+ return True
107
+ else:
108
+ print(f"Failed to install integration: {response.status_code}, {response.text}")
109
+ return False
110
+ except Exception as e:
111
+ print(f"Error installing integration: {str(e)}")
112
+ return False
113
+
114
+
115
  def delete_bot(bot_id, workspace_id):
116
  url = f"https://api.botpress.cloud/v1/admin/bots/{bot_id}"
117
  headers = {
118
+ "User-Agent": "Mozilla/5.0",
119
  "x-workspace-id": workspace_id,
120
+ "Cookie": AUTH_COOKIE
 
121
  }
122
+
123
+ try:
124
+ return requests.delete(url, headers=headers)
125
+ except Exception as e:
126
+ print(f"Error deleting bot: {str(e)}")
127
+ return None
128
 
129
 
130
  def delete_workspace(workspace_id):
131
  url = f"https://api.botpress.cloud/v1/admin/workspaces/{workspace_id}"
132
  headers = {
133
+ "User-Agent": "Mozilla/5.0",
134
+ "Cookie": AUTH_COOKIE
 
135
  }
136
+
137
+ try:
138
+ return requests.delete(url, headers=headers)
139
+ except Exception as e:
140
+ print(f"Error deleting workspace: {str(e)}")
141
+ return None
142
 
143
 
144
  # -------------------------------------------------------------------
145
+ # Main function that calls the Botpress API endpoint
146
  # -------------------------------------------------------------------
147
  def chat_with_assistant(user_input, chat_history, bot_id, workspace_id):
148
  """
149
+ Sends the user input and chat history to the Botpress API endpoint,
150
  returns the assistant's response and (possibly updated) bot/workspace IDs.
151
  """
 
152
  # Prepare the headers
153
  headers = {
154
+ "User-Agent": "Mozilla/5.0",
155
+ "x-bot-id": bot_id,
156
  "Content-Type": "application/json",
157
+ "Cookie": AUTH_COOKIE
 
158
  }
159
 
160
+ # Process chat history into the format expected by the new API
161
+ messages = []
162
+ system_prompt = ""
163
+
164
+ for msg in chat_history:
165
+ if msg["role"] == "system":
166
+ system_prompt = msg["content"]
167
+ else:
168
+ messages.append({
169
+ "role": msg["role"],
170
+ "content": msg["content"],
171
+ "type": "text"
172
+ })
173
+
174
+ # Add the latest user input if not already in chat history
175
+ if user_input and (not messages or messages[-1]["role"] != "user" or messages[-1]["content"] != user_input):
176
+ messages.append({
177
+ "role": "user",
178
+ "content": user_input,
179
+ "type": "text"
180
+ })
181
+
182
+ # Prepare the payload for the new API
183
  payload = {
184
+ "type": "openai:generateContent",
185
+ "input": {
186
+ "model": {
187
+ "id": "o1-2024-12-17"
188
+ },
189
+ "systemPrompt": system_prompt,
190
+ "messages": messages,
191
  "temperature": 0.75,
192
+ "topP": 1,
193
+ "debug": False,
194
+ }
 
 
 
 
 
 
195
  }
196
 
197
+ botpress_url = "https://api.botpress.cloud/v1/chat/actions"
198
+ max_retries = 5
199
+ timeout = 60 # seconds
200
 
201
  # Attempt to send the request
202
+ for attempt in range(max_retries):
203
+ try:
204
+ response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
205
 
206
+ # If successful (200)
207
+ if response.status_code == 200:
208
+ data = response.json()
209
+ assistant_content = data.get('output', {}).get('choices', [{}])[0].get('content', '')
210
+ return assistant_content, bot_id, workspace_id
 
 
 
 
 
 
 
 
211
 
212
+ # Any error status code - treat as invalid bot ID
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
  else:
214
+ raise Exception("Invalid or expired bot ID.")
215
+
216
+ except Exception as e:
217
+ # If invalid/expired bot or any other error, regenerate workspace/bot
218
+ if attempt == max_retries - 1: # Last attempt
219
+ # Attempt to delete old IDs if they exist
220
+ if bot_id and workspace_id:
221
+ delete_bot(bot_id, workspace_id)
222
+ delete_workspace(workspace_id)
223
+
224
+ # Create fresh IDs
225
+ new_workspace = create_workspace()
226
+ if new_workspace:
227
+ new_bot = create_bot(new_workspace)
228
+ if new_bot:
229
+ # Update headers with the new bot ID
230
+ headers["x-bot-id"] = new_bot
231
+
232
+ # Retry with new IDs
233
+ try:
234
+ retry_response = requests.post(botpress_url, json=payload, headers=headers, timeout=timeout)
235
+ if retry_response.status_code == 200:
236
+ data = retry_response.json()
237
+ assistant_content = data.get('output', {}).get('choices', [{}])[0].get('content', '')
238
+ return assistant_content, new_bot, new_workspace
239
+ else:
240
+ return f"Unable to get a response from the assistant.", new_bot, new_workspace
241
+ except Exception as retry_error:
242
+ return f"Unable to get a response from the assistant.", new_bot, new_workspace
243
+
244
+ # If we get here, all regeneration attempts failed
245
+ return "Unable to get a response from the assistant.", None, None
246
+
247
+ # Not the last attempt, wait and retry
248
+ time.sleep(2)
249
+
250
+ # Should not reach here due to the handling in the loop
251
+ return "Unable to get a response from the assistant.", bot_id, workspace_id
252
 
253
 
254
  # -------------------------------------------------------------------
 
281
  # If we don't yet have a workspace or bot, create them
282
  if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
283
  GLOBAL_WORKSPACE_ID = create_workspace()
284
+ if GLOBAL_WORKSPACE_ID:
285
+ GLOBAL_BOT_ID = create_bot(GLOBAL_WORKSPACE_ID)
286
 
287
  # If creation failed
288
  if not GLOBAL_WORKSPACE_ID or not GLOBAL_BOT_ID:
289
+ return jsonify({"assistant_response": "I'm currently unavailable. Please try again later."}), 500
290
 
291
+ # Call our function that interacts with Botpress API
292
  assistant_response, updated_bot_id, updated_workspace_id = chat_with_assistant(
293
  user_input,
294
  chat_history,
 
297
  )
298
 
299
  # Update global IDs if they changed
300
+ if updated_bot_id and updated_workspace_id:
301
+ GLOBAL_BOT_ID = updated_bot_id
302
+ GLOBAL_WORKSPACE_ID = updated_workspace_id
303
 
304
  return jsonify({"assistant_response": assistant_response})
305
 
306
 
307
  # -------------------------------------------------------------------
308
+ # Run the Flask app
309
  # -------------------------------------------------------------------
310
  if __name__ == "__main__":
311
  app.run(host="0.0.0.0", port=7860, debug=True)