Skip to main content

TelegramService

Struct TelegramService 

Source
pub struct TelegramService { /* private fields */ }
Expand description

Coordinates the Telegram bot lifecycle with the Ralph event loop.

Manages startup, shutdown, message sending, and response waiting. Uses the host tokio runtime (from #[tokio::main]) for async operations.

Implementations§

Source§

impl TelegramService

Source

pub fn new( workspace_root: PathBuf, bot_token: Option<String>, timeout_secs: u64, loop_id: String, ) -> TelegramResult<Self>

Create a new TelegramService.

Resolves the bot token from config or RALPH_TELEGRAM_BOT_TOKEN env var.

Source

pub fn workspace_root(&self) -> &PathBuf

Get a reference to the workspace root.

Source

pub fn timeout_secs(&self) -> u64

Get the configured timeout in seconds.

Source

pub fn bot_token_masked(&self) -> String

Get a reference to the bot token (masked for logging).

Source

pub fn state_manager(&self) -> &StateManager

Get a reference to the state manager.

Source

pub fn handler(&mut self) -> &mut MessageHandler

Get a mutable reference to the message handler.

Source

pub fn loop_id(&self) -> &str

Get the loop ID this service is associated with.

Source

pub fn shutdown_flag(&self) -> Arc<AtomicBool>

Returns a clone of the shutdown flag.

Signal handlers can set this flag to interrupt wait_for_response() without waiting for the full timeout.

Source

pub fn start(&self) -> TelegramResult<()>

Start the Telegram service.

Spawns a background polling task on the host tokio runtime to receive incoming messages. Must be called from within a tokio runtime context.

Source

pub fn stop(self)

Stop the Telegram service gracefully.

Signals the background polling task to shut down.

Source

pub fn send_question(&self, payload: &str) -> TelegramResult<i32>

Send a question to the human via Telegram and store it as a pending question.

The question payload is extracted from the human.interact event. A pending question is stored in the state manager so that incoming replies can be routed back to the correct loop.

On send failure, retries up to 3 times with exponential backoff (1s, 2s, 4s). Returns the message ID of the sent Telegram message, or 0 if no chat ID is configured (question is logged but not sent).

Source

pub fn send_checkin( &self, iteration: u32, elapsed: Duration, context: Option<&CheckinContext>, ) -> TelegramResult<i32>

Send a periodic check-in message via Telegram.

Loads the chat ID from state and sends a short status update so the human knows the loop is still running. Skips silently if no chat ID is configured. Returns Ok(0) when skipped, or the message ID on success.

When a CheckinContext is provided, the message includes richer details: current hat, task progress, and cumulative cost.

Source

pub fn send_document( &self, file_path: &Path, caption: Option<&str>, ) -> TelegramResult<i32>

Send a document (file) to the human via Telegram.

Loads the chat ID from state and sends the file at file_path with an optional caption. Returns Ok(0) if no chat ID is configured.

Source

pub fn send_photo( &self, file_path: &Path, caption: Option<&str>, ) -> TelegramResult<i32>

Send a photo to the human via Telegram.

Loads the chat ID from state and sends the image at file_path with an optional caption. Returns Ok(0) if no chat ID is configured.

Source

pub fn wait_for_response( &self, events_path: &Path, ) -> TelegramResult<Option<String>>

Poll the events file for a human.response event, blocking until one arrives or the configured timeout expires.

Polls the given events_path every second for new lines containing "human.response". On response, removes the pending question and returns the response message. On timeout, removes the pending question and returns None.

Trait Implementations§

Source§

impl Debug for TelegramService

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl RobotService for TelegramService

Source§

fn send_question(&self, payload: &str) -> Result<i32>

Send a question to the human and store it as pending. Read more
Source§

fn wait_for_response(&self, events_path: &Path) -> Result<Option<String>>

Poll the events file for a human.response event. Read more
Source§

fn send_checkin( &self, iteration: u32, elapsed: Duration, context: Option<&CheckinContext>, ) -> Result<i32>

Send a periodic check-in message. Read more
Source§

fn timeout_secs(&self) -> u64

Get the configured response timeout in seconds.
Source§

fn shutdown_flag(&self) -> Arc<AtomicBool>

Get a clone of the shutdown flag for cooperative interruption. Read more
Source§

fn stop(self: Box<Self>)

Stop the service gracefully. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Erasable for T

Source§

const ACK_1_1_0: bool = true

Whether this implementor has acknowledged the 1.1.0 update to unerase’s documented implementation requirements. Read more
Source§

unsafe fn unerase(this: NonNull<Erased>) -> NonNull<T>

Unerase this erased pointer. Read more
Source§

fn erase(this: NonNull<Self>) -> NonNull<Erased>

Turn this erasable pointer into an erased pointer. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more