conversation.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from aiohttp import ClientSession
  2. class Conversation():
  3. def __init__(self, conversationId: str, clientId: str, conversationSignature: str) -> None:
  4. self.conversationId = conversationId
  5. self.clientId = clientId
  6. self.conversationSignature = conversationSignature
  7. async def create_conversation(session: ClientSession, proxy: str = None) -> Conversation:
  8. url = 'https://www.bing.com/turing/conversation/create?bundleVersion=1.1199.4'
  9. async with session.get(url, proxy=proxy) as response:
  10. data = await response.json()
  11. conversationId = data.get('conversationId')
  12. clientId = data.get('clientId')
  13. conversationSignature = response.headers.get('X-Sydney-Encryptedconversationsignature')
  14. if not conversationId or not clientId or not conversationSignature:
  15. raise Exception('Failed to create conversation.')
  16. return Conversation(conversationId, clientId, conversationSignature)
  17. async def list_conversations(session: ClientSession) -> list:
  18. url = "https://www.bing.com/turing/conversation/chats"
  19. async with session.get(url) as response:
  20. response = await response.json()
  21. return response["chats"]
  22. async def delete_conversation(session: ClientSession, conversation: Conversation, proxy: str = None) -> list:
  23. url = "https://sydney.bing.com/sydney/DeleteSingleConversation"
  24. json = {
  25. "conversationId": conversation.conversationId,
  26. "conversationSignature": conversation.conversationSignature,
  27. "participant": {"id": conversation.clientId},
  28. "source": "cib",
  29. "optionsSets": ["autosave"]
  30. }
  31. try:
  32. async with session.post(url, json=json, proxy=proxy) as response:
  33. response = await response.json()
  34. return response["result"]["value"] == "Success"
  35. except:
  36. return False