conversation.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from aiohttp import ClientSession
  2. class Conversation:
  3. """
  4. Represents a conversation with specific attributes.
  5. """
  6. def __init__(self, conversationId: str, clientId: str, conversationSignature: str) -> None:
  7. """
  8. Initialize a new conversation instance.
  9. Args:
  10. conversationId (str): Unique identifier for the conversation.
  11. clientId (str): Client identifier.
  12. conversationSignature (str): Signature for the conversation.
  13. """
  14. self.conversationId = conversationId
  15. self.clientId = clientId
  16. self.conversationSignature = conversationSignature
  17. async def create_conversation(session: ClientSession, proxy: str = None) -> Conversation:
  18. """
  19. Create a new conversation asynchronously.
  20. Args:
  21. session (ClientSession): An instance of aiohttp's ClientSession.
  22. proxy (str, optional): Proxy URL. Defaults to None.
  23. Returns:
  24. Conversation: An instance representing the created conversation.
  25. """
  26. url = 'https://www.bing.com/turing/conversation/create?bundleVersion=1.1199.4'
  27. async with session.get(url, proxy=proxy) as response:
  28. try:
  29. data = await response.json()
  30. except:
  31. raise RuntimeError(f"Response: {await response.text()}")
  32. conversationId = data.get('conversationId')
  33. clientId = data.get('clientId')
  34. conversationSignature = response.headers.get('X-Sydney-Encryptedconversationsignature')
  35. if not conversationId or not clientId or not conversationSignature:
  36. raise Exception('Failed to create conversation.')
  37. return Conversation(conversationId, clientId, conversationSignature)
  38. async def list_conversations(session: ClientSession) -> list:
  39. """
  40. List all conversations asynchronously.
  41. Args:
  42. session (ClientSession): An instance of aiohttp's ClientSession.
  43. Returns:
  44. list: A list of conversations.
  45. """
  46. url = "https://www.bing.com/turing/conversation/chats"
  47. async with session.get(url) as response:
  48. response = await response.json()
  49. return response["chats"]
  50. async def delete_conversation(session: ClientSession, conversation: Conversation, proxy: str = None) -> bool:
  51. """
  52. Delete a conversation asynchronously.
  53. Args:
  54. session (ClientSession): An instance of aiohttp's ClientSession.
  55. conversation (Conversation): The conversation to delete.
  56. proxy (str, optional): Proxy URL. Defaults to None.
  57. Returns:
  58. bool: True if deletion was successful, False otherwise.
  59. """
  60. url = "https://sydney.bing.com/sydney/DeleteSingleConversation"
  61. json = {
  62. "conversationId": conversation.conversationId,
  63. "conversationSignature": conversation.conversationSignature,
  64. "participant": {"id": conversation.clientId},
  65. "source": "cib",
  66. "optionsSets": ["autosave"]
  67. }
  68. try:
  69. async with session.post(url, json=json, proxy=proxy) as response:
  70. response = await response.json()
  71. return response["result"]["value"] == "Success"
  72. except:
  73. return False