Antonio Cheong
commited on
Commit
·
ef4de38
1
Parent(s):
7c908ce
format
Browse files- .vscode/settings.json +1 -1
- docs.md +1 -1
- main.py +27 -12
- parse_cookies.py +5 -4
.vscode/settings.json
CHANGED
|
@@ -2,4 +2,4 @@
|
|
| 2 |
"cSpell.words": [
|
| 3 |
"websockets"
|
| 4 |
]
|
| 5 |
-
}
|
|
|
|
| 2 |
"cSpell.words": [
|
| 3 |
"websockets"
|
| 4 |
]
|
| 5 |
+
}
|
docs.md
CHANGED
|
@@ -4,4 +4,4 @@
|
|
| 4 |
|
| 5 |
Chat: `wss://sydney.bing.com/sydney/ChatHub`
|
| 6 |
|
| 7 |
-
Conversation creation: `https://www.bing.com/turing/conversation/create`
|
|
|
|
| 4 |
|
| 5 |
Chat: `wss://sydney.bing.com/sydney/ChatHub`
|
| 6 |
|
| 7 |
+
Conversation creation: `https://www.bing.com/turing/conversation/create`
|
main.py
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
"""
|
| 2 |
Main.py
|
| 3 |
"""
|
| 4 |
-
import uuid
|
| 5 |
import json
|
| 6 |
import time
|
|
|
|
|
|
|
|
|
|
| 7 |
import requests
|
| 8 |
from websocket import WebSocket
|
| 9 |
-
from threading import Thread
|
| 10 |
|
| 11 |
|
| 12 |
def append_identifier(msg: dict) -> str:
|
|
@@ -21,6 +22,7 @@ class ChatHubRequest:
|
|
| 21 |
"""
|
| 22 |
Request object for ChatHub
|
| 23 |
"""
|
|
|
|
| 24 |
def __init__(
|
| 25 |
self,
|
| 26 |
conversation_signature: str,
|
|
@@ -36,8 +38,14 @@ class ChatHubRequest:
|
|
| 36 |
self.invocation_id: int = invocation_id
|
| 37 |
self.is_start_of_session: bool = True
|
| 38 |
|
| 39 |
-
self.update(
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def update(
|
| 42 |
self,
|
| 43 |
prompt: str,
|
|
@@ -69,11 +77,12 @@ class ChatHubRequest:
|
|
| 69 |
"text": prompt,
|
| 70 |
"messageType": "Chat",
|
| 71 |
},
|
| 72 |
-
"conversationSignature": conversation_signature
|
|
|
|
| 73 |
"participant": {"id": client_id or self.client_id},
|
| 74 |
"conversationId": conversation_id or self.conversation_id,
|
| 75 |
"previousMessages": [],
|
| 76 |
-
}
|
| 77 |
],
|
| 78 |
"invocationId": str(invocation_id),
|
| 79 |
"target": "chat",
|
|
@@ -81,13 +90,19 @@ class ChatHubRequest:
|
|
| 81 |
}
|
| 82 |
self.is_start_of_session = False
|
| 83 |
|
|
|
|
| 84 |
class Conversation:
|
| 85 |
"""
|
| 86 |
Conversation API
|
| 87 |
"""
|
| 88 |
|
| 89 |
def __init__(self) -> None:
|
| 90 |
-
self.struct: dict = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
self.__create()
|
| 92 |
|
| 93 |
def __create(self):
|
|
@@ -115,7 +130,7 @@ class Conversation:
|
|
| 115 |
}
|
| 116 |
# Create cookies
|
| 117 |
cookies = json.loads(
|
| 118 |
-
open("templates/cookies.json",
|
| 119 |
)
|
| 120 |
# Send GET request
|
| 121 |
response = requests.get(
|
|
@@ -141,16 +156,15 @@ class ChatHub:
|
|
| 141 |
self.thread = Thread(target=self.__ping)
|
| 142 |
self.thread.start()
|
| 143 |
self.stop_thread = False
|
| 144 |
-
|
| 145 |
def ask(self, prompt: str):
|
| 146 |
pass
|
| 147 |
|
| 148 |
-
|
| 149 |
def __initial_handshake(self):
|
| 150 |
self.wss.send(append_identifier({"protocol": "json", "version": 1}))
|
| 151 |
# Receive blank message
|
| 152 |
self.wss.recv()
|
| 153 |
-
|
| 154 |
def __ping(self):
|
| 155 |
timing = 10
|
| 156 |
while True:
|
|
@@ -164,7 +178,7 @@ class ChatHub:
|
|
| 164 |
time.sleep(1)
|
| 165 |
if self.stop_thread:
|
| 166 |
break
|
| 167 |
-
|
| 168 |
def close(self):
|
| 169 |
"""
|
| 170 |
Close all connections
|
|
@@ -173,6 +187,7 @@ class ChatHub:
|
|
| 173 |
self.stop_thread = True
|
| 174 |
self.thread.join()
|
| 175 |
|
|
|
|
| 176 |
async def main():
|
| 177 |
"""
|
| 178 |
Main function
|
|
|
|
| 1 |
"""
|
| 2 |
Main.py
|
| 3 |
"""
|
|
|
|
| 4 |
import json
|
| 5 |
import time
|
| 6 |
+
import uuid
|
| 7 |
+
from threading import Thread
|
| 8 |
+
|
| 9 |
import requests
|
| 10 |
from websocket import WebSocket
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
def append_identifier(msg: dict) -> str:
|
|
|
|
| 22 |
"""
|
| 23 |
Request object for ChatHub
|
| 24 |
"""
|
| 25 |
+
|
| 26 |
def __init__(
|
| 27 |
self,
|
| 28 |
conversation_signature: str,
|
|
|
|
| 38 |
self.invocation_id: int = invocation_id
|
| 39 |
self.is_start_of_session: bool = True
|
| 40 |
|
| 41 |
+
self.update(
|
| 42 |
+
prompt=None,
|
| 43 |
+
conversation_signature=conversation_signature,
|
| 44 |
+
client_id=client_id,
|
| 45 |
+
conversation_id=conversation_id,
|
| 46 |
+
invocation_id=invocation_id,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
def update(
|
| 50 |
self,
|
| 51 |
prompt: str,
|
|
|
|
| 77 |
"text": prompt,
|
| 78 |
"messageType": "Chat",
|
| 79 |
},
|
| 80 |
+
"conversationSignature": conversation_signature
|
| 81 |
+
or self.conversation_signature,
|
| 82 |
"participant": {"id": client_id or self.client_id},
|
| 83 |
"conversationId": conversation_id or self.conversation_id,
|
| 84 |
"previousMessages": [],
|
| 85 |
+
},
|
| 86 |
],
|
| 87 |
"invocationId": str(invocation_id),
|
| 88 |
"target": "chat",
|
|
|
|
| 90 |
}
|
| 91 |
self.is_start_of_session = False
|
| 92 |
|
| 93 |
+
|
| 94 |
class Conversation:
|
| 95 |
"""
|
| 96 |
Conversation API
|
| 97 |
"""
|
| 98 |
|
| 99 |
def __init__(self) -> None:
|
| 100 |
+
self.struct: dict = {
|
| 101 |
+
"conversationId": None,
|
| 102 |
+
"clientId": None,
|
| 103 |
+
"conversationSignature": None,
|
| 104 |
+
"result": {"value": "Success", "message": None},
|
| 105 |
+
}
|
| 106 |
self.__create()
|
| 107 |
|
| 108 |
def __create(self):
|
|
|
|
| 130 |
}
|
| 131 |
# Create cookies
|
| 132 |
cookies = json.loads(
|
| 133 |
+
open("templates/cookies.json", encoding="utf-8").read(),
|
| 134 |
)
|
| 135 |
# Send GET request
|
| 136 |
response = requests.get(
|
|
|
|
| 156 |
self.thread = Thread(target=self.__ping)
|
| 157 |
self.thread.start()
|
| 158 |
self.stop_thread = False
|
| 159 |
+
|
| 160 |
def ask(self, prompt: str):
|
| 161 |
pass
|
| 162 |
|
|
|
|
| 163 |
def __initial_handshake(self):
|
| 164 |
self.wss.send(append_identifier({"protocol": "json", "version": 1}))
|
| 165 |
# Receive blank message
|
| 166 |
self.wss.recv()
|
| 167 |
+
|
| 168 |
def __ping(self):
|
| 169 |
timing = 10
|
| 170 |
while True:
|
|
|
|
| 178 |
time.sleep(1)
|
| 179 |
if self.stop_thread:
|
| 180 |
break
|
| 181 |
+
|
| 182 |
def close(self):
|
| 183 |
"""
|
| 184 |
Close all connections
|
|
|
|
| 187 |
self.stop_thread = True
|
| 188 |
self.thread.join()
|
| 189 |
|
| 190 |
+
|
| 191 |
async def main():
|
| 192 |
"""
|
| 193 |
Main function
|
parse_cookies.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
-
|
| 2 |
cookies = {}
|
| 3 |
-
with open(
|
| 4 |
cookies = f.read().split("; ")
|
| 5 |
# Everything after the first "=" is the value (there may be multiple =)
|
| 6 |
-
cookies = {
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
print(cookies)
|
|
|
|
|
|
|
| 1 |
cookies = {}
|
| 2 |
+
with open("cookies.txt") as f:
|
| 3 |
cookies = f.read().split("; ")
|
| 4 |
# Everything after the first "=" is the value (there may be multiple =)
|
| 5 |
+
cookies = {
|
| 6 |
+
cookie.split("=")[0]: "=".join(cookie.split("=")[1:]) for cookie in cookies
|
| 7 |
+
}
|
| 8 |
|
| 9 |
+
print(cookies)
|