Skip to content

Commit

Permalink
Add get conversations method (#49)
Browse files Browse the repository at this point in the history
- Add new method for fetching all conversations for current client
- Add test for new method
  • Loading branch information
vsakkas authored Aug 28, 2023
1 parent 7b6a581 commit f0e39ce
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Sydney.py

[![Latest Release](https://img.shields.io/github/v/release/vsakkas/sydney.py.svg)](https://github.com/vsakkas/sydney.py/releases/tag/v0.13.0)
[![Latest Release](https://img.shields.io/github/v/release/vsakkas/sydney.py.svg)](https://github.com/vsakkas/sydney.py/releases/tag/v0.14.0)
[![Python](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/)
[![MIT License](https://img.shields.io/badge/license-MIT-blue)](https://github.com/vsakkas/sydney.py/blob/master/LICENSE)

Expand Down Expand Up @@ -223,6 +223,16 @@ async with SydneyClient() as sydney:
print(response)
```

### Conversations

You can also receive all existing conversations that were made with the current client:

```python
async with SydneyClient() as sydney:
response = await sydney.get_conversations()
print(response)
```

*For more detailed documentation and options, please refer to the code docstrings.*

## License
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "sydney-py"
version = "0.13.0"
version = "0.14.0"
description = "Python Client for Bing Chat, also known as Sydney."
authors = ["vsakkas <[email protected]>"]
license = "MIT"
Expand Down
1 change: 1 addition & 0 deletions sydney/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
}

BING_CREATE_CONVERSATION_URL = "https://www.bing.com/turing/conversation/create"
BING_GET_CONVERSATIONS_URL = "https://www.bing.com/turing/conversation/chats"
BING_CHATHUB_URL = "wss://sydney.bing.com/sydney/ChatHub"

DELIMETER = "\x1e" # Record separator character.
37 changes: 37 additions & 0 deletions sydney/sydney.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from sydney.constants import (
BING_CHATHUB_URL,
BING_CREATE_CONVERSATION_URL,
BING_GET_CONVERSATIONS_URL,
DELIMETER,
HEADERS,
)
Expand Down Expand Up @@ -582,3 +583,39 @@ async def close_conversation(self) -> None:
self.invocation_id = None
self.number_of_messages = None
self.max_messages = None

async def get_conversations(self) -> dict:
"""
Get all conversations.
Returns
-------
dict
Dictionary containing `chats`, `result` and `clientId` fields.
The `chats` fields contains the list of conversations and info about
those, `result` contains some metadata about the returned response and
`clientId` is the ID that the current Sydney client is using.
"""
# Use _U cookie to create a conversation.
cookies = {"_U": self.bing_u_cookie}

session = ClientSession(
headers=HEADERS,
cookies=cookies,
trust_env=self.use_proxy, # Use `HTTP_PROXY` and `HTTPS_PROXY` environment variables.
connector=TCPConnector(verify_ssl=False)
if self.use_proxy
else None, # Resolve HTTPS issue when proxy support is enabled.
)

async with session.get(BING_GET_CONVERSATIONS_URL) as response:
if response.status != 200:
raise Exception(
f"Failed to get conversations, received status: {response.status}"
)

response_dict = await response.json()

await session.close()

return response_dict
16 changes: 16 additions & 0 deletions tests/test_conversation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest

from sydney import SydneyClient


@pytest.mark.asyncio
async def test_get_conversations() -> None:
async with SydneyClient() as sydney:
_ = await sydney.ask("Hello, Bing!")

response = await sydney.get_conversations()

assert "chats" in response
assert "result" in response
assert "clientId" in response
assert response["clientId"] == sydney.client_id

0 comments on commit f0e39ce

Please sign in to comment.