-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBotio.py
114 lines (95 loc) · 4.23 KB
/
Botio.py
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
106
107
108
109
110
111
112
113
114
import requests
import json
import uuid
class Botio:
def __init__(self, cookie):
self.cookie = cookie
self.organization_uuid = self.get_organization_uuid()
def get_organization_uuid(self):
url = "https://claude.ai/api/organizations"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
'Accept-Language': 'en-US,en;q=0.5',
'Referer': 'https://claude.ai/chats',
'Content-Type': 'application/json',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'Connection': 'keep-alive',
'Cookie': self.cookie
}
response = requests.request("GET", url, headers=headers)
res = json.loads(response.text)
return res[0]['uuid']
def get_chats(self):
url = f"https://claude.ai/api/organizations/{self.organization_uuid}/chat_conversations"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
'Accept-Language': 'en-US,en;q=0.5',
'Referer': 'https://claude.ai/chats',
'Content-Type': 'application/json',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'Connection': 'keep-alive',
'Cookie': self.cookie
}
response = requests.get(url, headers=headers)
return response.json()
def send_message(self, prompt, conversation_id):
url = "https://claude.ai/api/append_message"
payload = json.dumps({
"completion": {
"prompt": prompt,
"timezone": "Asia/Kolkata",
"model": "claude-2"
},
"organization_uuid": self.organization_uuid,
"conversation_uuid": conversation_id,
"text": prompt,
})
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
'Accept': 'text/event-stream, text/event-stream',
'Accept-Language': 'en-US,en;q=0.5',
'Referer': 'https://claude.ai/chats',
'Content-Type': 'application/json',
'Origin': 'https://claude.ai',
'DNT': '1',
'Connection': 'keep-alive',
'Cookie': self.cookie,
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'TE': 'trailers'
}
response = requests.post(url, headers=headers, data=payload, stream=True)
decoded_data = response.content.decode("utf-8")
data = (decoded_data.strip().split('\n')[-3])
answer = {"answer": json.loads(data[6:])['completion']}['answer']
return answer
def create_chats(self):
url = f"https://claude.ai/api/organizations/{self.organization_uuid}/chat_conversations"
uuid1 = self.generate_uuid()
payload = json.dumps({"uuid": uuid1, "name": ""})
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0',
'Accept-Language': 'en-US,en;q=0.5',
'Referer': 'https://claude.ai/chats',
'Content-Type': 'application/json',
'Origin': 'https://claude.ai',
'DNT': '1',
'Connection': 'keep-alive',
'Cookie': self.cookie,
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'TE': 'trailers'
}
response = requests.request("POST", url, headers=headers, data=payload)
return "чат создан" if response.status_code == 201 else response.status_code
def create_uuid(self):
random_uuid = uuid.uuid4()
random_uuid_str = str(random_uuid)
formatted_uuid = f"{random_uuid_str[0:8]}-{random_uuid_str[8:12]}-{random_uuid_str[12:16]}-{random_uuid_str[16:20]}-{random_uuid_str[20:]}"
return formatted_uuid