oc_connect
Loading...
Searching...
No Matches
connector.py
Go to the documentation of this file.
1"""Telegram bundle adapter used by the durable outbox.
2
3Dependencies
4------------
5- oc_connect.connectors.telegram.destinations (DestinationStore - resolves the logical destination to a chat and credential name)
6- oc_connect.connectors.telegram.transport (TelegramTransport - performs the raw Bot API send)
7- oc_connect.credentials (CredentialBroker - retrieves the bot token by name)
8- oc_connect.outbox (OutboxItem - the already-filtered item being delivered)
9"""
10from __future__ import annotations
11
12from collections.abc import Mapping
13from typing import Any
14
15from ...credentials import CredentialBroker
16from ...outbox import OutboxItem
17from .destinations import DestinationStore
18from .transport import TelegramTransport
19
20
22 """Resolve one logical destination and perform one raw Bot API send."""
23
25 self,
26 broker: CredentialBroker,
27 destinations: DestinationStore,
28 ) -> None:
29 self.broker = broker
30 self.destinations = destinations
31
33 self,
34 item: OutboxItem,
35 *,
36 reply_markup: Mapping[str, Any] | None = None,
37 ) -> str | None:
38 """Deliver an already-filtered outbox item, returning its message id.
39
40 The returned ``message_id`` is what an operator reply's ``reply_to``
41 metadata resolves against when the inbound poller correlates a
42 structured reply to the question it answers (ยง4). *reply_markup*
43 (plan 236) is the already-built vocabulary keyboard, or ``None`` for a
44 plain send; the item's content is unchanged either way.
45 """
46 destination = self.destinations.get(item.destination)
47 raw_value = self.broker.retrieve(destination.credential_name)
48 transport = TelegramTransport(raw_value)
49 try:
50 result = transport.send_message(
51 destination.chat_id, item.content, reply_markup=reply_markup
52 )
53 finally:
54 transport.close()
55 message_id = result.get("message_id")
56 return str(message_id) if message_id is not None else None
57
58
59__all__ = ["TelegramConnector"]
None __init__(self, CredentialBroker broker, DestinationStore destinations)
Definition connector.py:28
str|None deliver(self, OutboxItem item, *, Mapping[str, Any]|None reply_markup=None)
Definition connector.py:37