trading_agent.channels.telegram_channel
¶
The Telegram channel: talk to the desks and hear from them on your phone.
It runs two loops beside the desks:
- The message loop asks Telegram for new messages. A chat that is not paired gets a pairing code and nothing else; every such message is logged as a channel event. A paired chat's code-only commands (/status, /positions, /pnl, /pause, /resume, /kill, /desk, /help) are answered at once; anything else is sent to the chat's desk as a user message.
- The event loop listens to the event bus. A desk's answer to a message from Telegram goes back to the chat it came from. The notifications chosen in [telegram] notifications, such as fills and halts, go to every paired chat.
A message sent while its desk is working steers the running turn, as on the web page; the chat is told so, and the running turn's answer comes to it too.
A photo or file from a paired chat is downloaded into the desk's uploads, as an upload from the web page is, and goes to the desk with the caption as the message. Files from chats that are not paired are never downloaded.
Typical usage example:
channel = TelegramChannel(api, pairing, commands, formatter, desks, parts, event_bus, 'main') await channel.run()
TelegramChannel
¶
TelegramChannel(api, pairing, commands, formatter, desks, parts, event_bus, default_desk, attachments=None, requests=None)
The bot, its paired chats and its notifications.
Attributes:
| Name | Type | Description |
|---|---|---|
state |
"connecting", "connected" or "failed". |
|
error |
Why the last attempt failed, or an empty string. |
|
username |
The bot's username once connected. |
Creates the channel without contacting Telegram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api
|
TelegramApi
|
The connection to Telegram. |
required |
pairing
|
PairingStore
|
The paired chats and codes. |
required |
commands
|
TelegramCommands
|
The code-only commands. |
required |
formatter
|
NotificationFormatter
|
Decides what is pushed. |
required |
desks
|
Mapping[str, DeskRuntime]
|
The running desks. |
required |
parts
|
Mapping[str, DeskParts]
|
Each desk's session store, for the daily summaries. |
required |
event_bus
|
EventBus
|
Where the desks' events arrive. |
required |
default_desk
|
str
|
The desk a newly paired chat talks to. |
required |
attachments
|
AttachmentStore | None
|
Where photos and files sent to the bot are kept, or None to refuse them. |
None
|
requests
|
TelegramRequests | None
|
Shows the desks' questions and plans with buttons, or None to leave them to the web page. |
None
|
Source code in src/trading_agent/channels/telegram_channel.py
run
async
¶
Connects, then runs the message loop and the event loop until cancelled.
Source code in src/trading_agent/channels/telegram_channel.py
close
async
¶
approve
async
¶
Pairs the chat that was given a code, and tells the chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
The pairing code. |
required |
Returns:
| Type | Description |
|---|---|
PairedChat
|
The paired chat. |
Raises:
| Type | Description |
|---|---|
PairingError
|
No valid code matches. |