2024-01-26 06:54:13 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-01-10 09:34:56 +00:00
|
|
|
from aiohttp import ClientSession
|
2024-03-12 01:06:06 +00:00
|
|
|
from ...requests import raise_for_status
|
2024-03-19 17:48:32 +00:00
|
|
|
from ...errors import RateLimitError
|
2024-03-26 05:42:47 +00:00
|
|
|
from ...providers.conversation import BaseConversation
|
2024-01-10 09:34:56 +00:00
|
|
|
|
2024-03-26 05:42:47 +00:00
|
|
|
class Conversation(BaseConversation):
|
2024-01-14 06:45:41 +00:00
|
|
|
"""
|
|
|
|
Represents a conversation with specific attributes.
|
|
|
|
"""
|
2024-01-10 09:34:56 +00:00
|
|
|
def __init__(self, conversationId: str, clientId: str, conversationSignature: str) -> None:
|
2024-01-14 06:45:41 +00:00
|
|
|
"""
|
|
|
|
Initialize a new conversation instance.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
conversationId (str): Unique identifier for the conversation.
|
|
|
|
clientId (str): Client identifier.
|
|
|
|
conversationSignature (str): Signature for the conversation.
|
|
|
|
"""
|
2024-01-10 09:34:56 +00:00
|
|
|
self.conversationId = conversationId
|
|
|
|
self.clientId = clientId
|
|
|
|
self.conversationSignature = conversationSignature
|
|
|
|
|
2024-03-16 17:07:53 +00:00
|
|
|
async def create_conversation(session: ClientSession, headers: dict, tone: str) -> Conversation:
|
2024-01-14 06:45:41 +00:00
|
|
|
"""
|
|
|
|
Create a new conversation asynchronously.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
session (ClientSession): An instance of aiohttp's ClientSession.
|
|
|
|
proxy (str, optional): Proxy URL. Defaults to None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Conversation: An instance representing the created conversation.
|
|
|
|
"""
|
2024-03-26 05:42:47 +00:00
|
|
|
if tone == "Copilot":
|
2024-03-16 17:07:53 +00:00
|
|
|
url = "https://copilot.microsoft.com/turing/conversation/create?bundleVersion=1.1634.3-nodesign2"
|
|
|
|
else:
|
|
|
|
url = "https://www.bing.com/turing/conversation/create?bundleVersion=1.1626.1"
|
2024-03-12 01:06:06 +00:00
|
|
|
async with session.get(url, headers=headers) as response:
|
2024-03-19 17:48:32 +00:00
|
|
|
if response.status == 404:
|
|
|
|
raise RateLimitError("Response 404: Do less requests and reuse conversations")
|
2024-03-12 17:45:22 +00:00
|
|
|
await raise_for_status(response, "Failed to create conversation")
|
2024-03-12 01:06:06 +00:00
|
|
|
data = await response.json()
|
|
|
|
conversationId = data.get('conversationId')
|
|
|
|
clientId = data.get('clientId')
|
|
|
|
conversationSignature = response.headers.get('X-Sydney-Encryptedconversationsignature')
|
|
|
|
if not conversationId or not clientId or not conversationSignature:
|
2024-03-12 17:45:22 +00:00
|
|
|
raise RuntimeError('Empty fields: Failed to create conversation')
|
2024-03-12 01:06:06 +00:00
|
|
|
return Conversation(conversationId, clientId, conversationSignature)
|
2024-01-10 09:34:56 +00:00
|
|
|
|
|
|
|
async def list_conversations(session: ClientSession) -> list:
|
2024-01-14 06:45:41 +00:00
|
|
|
"""
|
|
|
|
List all conversations asynchronously.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
session (ClientSession): An instance of aiohttp's ClientSession.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list: A list of conversations.
|
|
|
|
"""
|
2024-01-10 09:34:56 +00:00
|
|
|
url = "https://www.bing.com/turing/conversation/chats"
|
|
|
|
async with session.get(url) as response:
|
|
|
|
response = await response.json()
|
|
|
|
return response["chats"]
|
2024-03-12 17:45:22 +00:00
|
|
|
|
|
|
|
async def delete_conversation(session: ClientSession, conversation: Conversation, headers: dict) -> bool:
|
2024-01-14 06:45:41 +00:00
|
|
|
"""
|
|
|
|
Delete a conversation asynchronously.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
session (ClientSession): An instance of aiohttp's ClientSession.
|
|
|
|
conversation (Conversation): The conversation to delete.
|
|
|
|
proxy (str, optional): Proxy URL. Defaults to None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if deletion was successful, False otherwise.
|
|
|
|
"""
|
2024-01-10 09:34:56 +00:00
|
|
|
url = "https://sydney.bing.com/sydney/DeleteSingleConversation"
|
|
|
|
json = {
|
|
|
|
"conversationId": conversation.conversationId,
|
|
|
|
"conversationSignature": conversation.conversationSignature,
|
|
|
|
"participant": {"id": conversation.clientId},
|
|
|
|
"source": "cib",
|
|
|
|
"optionsSets": ["autosave"]
|
|
|
|
}
|
|
|
|
try:
|
2024-03-12 17:45:22 +00:00
|
|
|
async with session.post(url, json=json, headers=headers) as response:
|
2024-01-10 09:34:56 +00:00
|
|
|
response = await response.json()
|
|
|
|
return response["result"]["value"] == "Success"
|
|
|
|
except:
|
|
|
|
return False
|