Skip to content

trading_agent.client.pairing_commands

The trading-agent pairing commands: list, approve and revoke Telegram chats.

They work on the pairing file directly, so they work whether or not the gateway is running, and the running gateway sees the change at the chat's next message.

Typical usage example:

exit_code = PairingCommands(store, 'main').approve('ABCD2345')

PairingCommands

PairingCommands(store, default_desk, output=stdout)

Manages Telegram pairings for the user.

Creates the commands.

Parameters:

Name Type Description Default
store PairingStore

The pairing file.

required
default_desk str

The desk a newly paired chat talks to.

required
output TextIO

Where to print.

stdout
Source code in src/trading_agent/client/pairing_commands.py
def __init__(
    self,
    store: pairing_store_module.PairingStore,
    default_desk: str,
    output: TextIO = sys.stdout,
):
    """Creates the commands.

    Args:
        store (pairing_store_module.PairingStore): The pairing file.
        default_desk (str): The desk a newly paired chat talks to.
        output (TextIO): Where to print.
    """
    self._store = store
    self._default_desk = default_desk
    self._output = output

list

list()

Prints the paired chats and the codes waiting for approval.

Returns:

Type Description
int

Always 0.

Source code in src/trading_agent/client/pairing_commands.py
def list(self) -> int:
    """Prints the paired chats and the codes waiting for approval.

    Returns:
        int: Always 0.
    """
    chats = self._store.paired_chats()
    if not chats:
        self._print('No chat is paired.')
    for chat in chats:
        self._print(
            f'paired: {chat.name or "(no name)"}, chat {chat.chat_id}, desk {chat.desk}, since {chat.paired_at}'
        )
    for pending in self._store.pending():
        self._print(
            f'waiting: code {pending.code} from {pending.name or "(no name)"}, chat {pending.chat_id}, asked at {pending.requested_at}'
        )
    return 0

approve

approve(code)

Pairs the chat that was given a code.

Parameters:

Name Type Description Default
code str

The code the bot sent.

required

Returns:

Type Description
int

0 when paired, 1 when no valid code matches.

Source code in src/trading_agent/client/pairing_commands.py
def approve(self, code: str) -> int:
    """Pairs the chat that was given a code.

    Args:
        code (str): The code the bot sent.

    Returns:
        int: 0 when paired, 1 when no valid code matches.
    """
    try:
        chat = self._store.approve(code, self._default_desk)
    except pairing_store_module.PairingError as error:
        self._print(str(error))
        return 1
    self._print(
        f'Paired {chat.name or "the chat"} (chat {chat.chat_id}); it talks to {chat.desk}. Send the bot any message to start.'
    )
    return 0

revoke

revoke(chat_id)

Unpairs a chat.

Parameters:

Name Type Description Default
chat_id str

The chat's id, as pairing list prints it.

required

Returns:

Type Description
int

0 when removed, 1 when the chat was not paired or the id is not a number.

Source code in src/trading_agent/client/pairing_commands.py
def revoke(self, chat_id: str) -> int:
    """Unpairs a chat.

    Args:
        chat_id (str): The chat's id, as `pairing list` prints it.

    Returns:
        int: 0 when removed, 1 when the chat was not paired or the id is not a number.
    """
    try:
        chat = self._store.revoke(int(chat_id))
    except (ValueError, pairing_store_module.PairingError) as error:
        self._print(str(error))
        return 1
    self._print(
        f'Unpaired {chat.name or "the chat"} (chat {chat.chat_id}). The bot will ignore it.'
    )
    return 0