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
impl TelegramService
Sourcepub fn new(
workspace_root: PathBuf,
bot_token: Option<String>,
timeout_secs: u64,
loop_id: String,
) -> TelegramResult<Self>
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.
Sourcepub fn workspace_root(&self) -> &PathBuf
pub fn workspace_root(&self) -> &PathBuf
Get a reference to the workspace root.
Sourcepub fn timeout_secs(&self) -> u64
pub fn timeout_secs(&self) -> u64
Get the configured timeout in seconds.
Sourcepub fn bot_token_masked(&self) -> String
pub fn bot_token_masked(&self) -> String
Get a reference to the bot token (masked for logging).
Sourcepub fn state_manager(&self) -> &StateManager
pub fn state_manager(&self) -> &StateManager
Get a reference to the state manager.
Sourcepub fn handler(&mut self) -> &mut MessageHandler
pub fn handler(&mut self) -> &mut MessageHandler
Get a mutable reference to the message handler.
Sourcepub fn shutdown_flag(&self) -> Arc<AtomicBool>
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.
Sourcepub fn start(&self) -> TelegramResult<()>
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.
Sourcepub fn stop(self)
pub fn stop(self)
Stop the Telegram service gracefully.
Signals the background polling task to shut down.
Sourcepub fn send_question(&self, payload: &str) -> TelegramResult<i32>
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).
Sourcepub fn send_checkin(
&self,
iteration: u32,
elapsed: Duration,
context: Option<&CheckinContext>,
) -> TelegramResult<i32>
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.
Sourcepub fn send_document(
&self,
file_path: &Path,
caption: Option<&str>,
) -> TelegramResult<i32>
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.
Sourcepub fn send_photo(
&self,
file_path: &Path,
caption: Option<&str>,
) -> TelegramResult<i32>
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.
Sourcepub fn wait_for_response(
&self,
events_path: &Path,
) -> TelegramResult<Option<String>>
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
impl Debug for TelegramService
Source§impl RobotService for TelegramService
impl RobotService for TelegramService
Source§fn send_question(&self, payload: &str) -> Result<i32>
fn send_question(&self, payload: &str) -> Result<i32>
Source§fn wait_for_response(&self, events_path: &Path) -> Result<Option<String>>
fn wait_for_response(&self, events_path: &Path) -> Result<Option<String>>
human.response event. Read moreSource§fn send_checkin(
&self,
iteration: u32,
elapsed: Duration,
context: Option<&CheckinContext>,
) -> Result<i32>
fn send_checkin( &self, iteration: u32, elapsed: Duration, context: Option<&CheckinContext>, ) -> Result<i32>
Source§fn timeout_secs(&self) -> u64
fn timeout_secs(&self) -> u64
Source§fn shutdown_flag(&self) -> Arc<AtomicBool>
fn shutdown_flag(&self) -> Arc<AtomicBool>
Auto Trait Implementations§
impl Freeze for TelegramService
impl !RefUnwindSafe for TelegramService
impl Send for TelegramService
impl Sync for TelegramService
impl Unpin for TelegramService
impl !UnwindSafe for TelegramService
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Erasable for T
impl<T> Erasable for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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