conversation.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from __future__ import annotations
  2. import uuid
  3. from aiohttp import ClientSession
  4. class Conversation:
  5. """
  6. Represents a conversation with specific attributes.
  7. """
  8. def __init__(self, conversationId: str, clientId: str, conversationSignature: str) -> None:
  9. """
  10. Initialize a new conversation instance.
  11. Args:
  12. conversationId (str): Unique identifier for the conversation.
  13. clientId (str): Client identifier.
  14. conversationSignature (str): Signature for the conversation.
  15. """
  16. self.conversationId = conversationId
  17. self.clientId = clientId
  18. self.conversationSignature = conversationSignature
  19. async def create_conversation(session: ClientSession, proxy: str = None) -> Conversation:
  20. """
  21. Create a new conversation asynchronously.
  22. Args:
  23. session (ClientSession): An instance of aiohttp's ClientSession.
  24. proxy (str, optional): Proxy URL. Defaults to None.
  25. Returns:
  26. Conversation: An instance representing the created conversation.
  27. """
  28. url = 'https://www.bing.com/search?toncp=0&FORM=hpcodx&q=Bing+AI&showconv=1&cc=en'
  29. async with session.get(url, proxy=proxy) as response:
  30. response.raise_for_status()
  31. headers = {
  32. "accept": "application/json",
  33. "sec-fetch-dest": "empty",
  34. "sec-fetch-mode": "cors",
  35. "sec-fetch-site": "same-origin",
  36. "x-ms-client-request-id": str(uuid.uuid4()),
  37. "x-ms-useragent": "azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.12.3 OS/Windows",
  38. "referer": url,
  39. "Cookie": "; ".join(f"{c.key}={c.value}" for c in session.cookie_jar)
  40. }
  41. for k, v in headers.items():
  42. session.headers[k] = v
  43. url = 'https://www.bing.com/turing/conversation/create?bundleVersion=1.1579.2'
  44. async with session.get(url, headers=headers, proxy=proxy) as response:
  45. try:
  46. data = await response.json()
  47. except:
  48. raise RuntimeError(f"Response: {await response.text()}")
  49. conversationId = data.get('conversationId')
  50. clientId = data.get('clientId')
  51. conversationSignature = response.headers.get('X-Sydney-Encryptedconversationsignature')
  52. if not conversationId or not clientId or not conversationSignature:
  53. raise Exception('Failed to create conversation.')
  54. return Conversation(conversationId, clientId, conversationSignature)
  55. async def list_conversations(session: ClientSession) -> list:
  56. """
  57. List all conversations asynchronously.
  58. Args:
  59. session (ClientSession): An instance of aiohttp's ClientSession.
  60. Returns:
  61. list: A list of conversations.
  62. """
  63. url = "https://www.bing.com/turing/conversation/chats"
  64. async with session.get(url) as response:
  65. response = await response.json()
  66. return response["chats"]
  67. async def delete_conversation(session: ClientSession, conversation: Conversation, proxy: str = None) -> bool:
  68. """
  69. Delete a conversation asynchronously.
  70. Args:
  71. session (ClientSession): An instance of aiohttp's ClientSession.
  72. conversation (Conversation): The conversation to delete.
  73. proxy (str, optional): Proxy URL. Defaults to None.
  74. Returns:
  75. bool: True if deletion was successful, False otherwise.
  76. """
  77. url = "https://sydney.bing.com/sydney/DeleteSingleConversation"
  78. json = {
  79. "conversationId": conversation.conversationId,
  80. "conversationSignature": conversation.conversationSignature,
  81. "participant": {"id": conversation.clientId},
  82. "source": "cib",
  83. "optionsSets": ["autosave"]
  84. }
  85. try:
  86. async with session.post(url, json=json, proxy=proxy) as response:
  87. response = await response.json()
  88. return response["result"]["value"] == "Success"
  89. except:
  90. return False