trading_agent.channels.pairing_store
¶
Which Telegram chats may command the desks, and the pairing codes waiting for the user's approval.
Anyone who finds the bot can message it, so the bot obeys only chats the user has approved. A chat that is not paired gets an eight-character code. The user approves the code in the web page or with trading-agent pairing approve, which proves the chat is theirs: only someone who can see the bot's reply and reach this machine can do both. A code expires after an hour.
The store is one JSON file in workspace/shared, which the desks can read but never write, so no desk can pair a chat by itself. The file is read afresh for every question, so an approval made by the command line reaches the running gateway at once.
Typical usage example:
store = PairingStore(layout.telegram_pairing_file) code = store.request(chat_id, 'Pramod') store.approve(code) store.is_paired(chat_id)
PairingError
¶
Bases: ValueError
A pairing code that does not exist or has expired, or a chat that is not paired.
PendingPairing
dataclass
¶
A chat waiting for the user to approve its code.
Attributes:
| Name | Type | Description |
|---|---|---|
code |
str
|
The eight-character code the bot sent to the chat. |
chat_id |
int
|
Telegram's id for the chat. |
name |
str
|
The sender's name, as Telegram gives it. |
requested_at |
str
|
When the code was made, as an ISO 8601 string in UTC. |
PairedChat
dataclass
¶
A chat the desks obey.
Attributes:
| Name | Type | Description |
|---|---|---|
chat_id |
int
|
Telegram's id for the chat. |
name |
str
|
The sender's name when it was paired. |
paired_at |
str
|
When it was approved, as an ISO 8601 string in UTC. |
desk |
str
|
The desk the chat talks to. |
PairingStore
¶
The paired chats and pending codes, kept in one JSON file.
Creates the store without reading the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
The JSON file. |
required |
clock
|
Callable[[], datetime] | None
|
Returns the current time in UTC; None uses the system clock. |
None
|
Source code in src/trading_agent/channels/pairing_store.py
request
¶
Gives an unpaired chat a pairing code, reusing its code while it is still valid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
The chat. |
required |
name
|
str
|
The sender's name. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The code, or None when too many codes are already waiting, so strangers cannot fill the list. |
Source code in src/trading_agent/channels/pairing_store.py
approve
¶
Pairs the chat that was given a code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
The code, with or without a dash, in any case. |
required |
desk
|
str
|
The desk the chat starts talking to. |
required |
Returns:
| Type | Description |
|---|---|
PairedChat
|
The newly paired chat. |
Raises:
| Type | Description |
|---|---|
PairingError
|
No valid code matches. |
Source code in src/trading_agent/channels/pairing_store.py
revoke
¶
Unpairs a chat, so the desks stop obeying it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
The chat. |
required |
Returns:
| Type | Description |
|---|---|
PairedChat
|
The chat that was removed. |
Raises:
| Type | Description |
|---|---|
PairingError
|
The chat is not paired. |
Source code in src/trading_agent/channels/pairing_store.py
paired_chat
¶
Finds a paired chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
The chat. |
required |
Returns:
| Type | Description |
|---|---|
PairedChat | None
|
The chat, or None when it is not paired. |
Source code in src/trading_agent/channels/pairing_store.py
paired_chats
¶
Every paired chat.
Returns:
| Type | Description |
|---|---|
list[PairedChat]
|
The chats, in the order they were paired. |
Source code in src/trading_agent/channels/pairing_store.py
pending
¶
Every code still waiting for approval.
Returns:
| Type | Description |
|---|---|
list[PendingPairing]
|
The codes, oldest first. |
Source code in src/trading_agent/channels/pairing_store.py
choose_desk
¶
Changes the desk a paired chat talks to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
The chat. |
required |
desk
|
str
|
The desk. |
required |
Raises:
| Type | Description |
|---|---|
PairingError
|
The chat is not paired. |