use std::sync::Arc;
use rustigram_api::BotClient;
use rustigram_types::inline::InlineQuery;
use rustigram_types::message::Message;
use rustigram_types::update::CallbackQuery;
use rustigram_types::update::{Update, UpdateKind};
use rustigram_types::user::ChatId;
#[derive(Clone)]
pub struct Context {
pub update: Arc<Update>,
pub bot: BotClient,
}
impl Context {
#[must_use]
pub fn new(update: Update, bot: BotClient) -> Self {
Self {
update: Arc::new(update),
bot,
}
}
#[must_use]
pub fn update_id(&self) -> i64 {
self.update.update_id
}
#[must_use]
pub fn message(&self) -> Option<&Message> {
match &self.update.kind {
UpdateKind::Message(m)
| UpdateKind::EditedMessage(m)
| UpdateKind::ChannelPost(m)
| UpdateKind::EditedChannelPost(m)
| UpdateKind::BusinessMessage(m)
| UpdateKind::EditedBusinessMessage(m) => Some(m),
UpdateKind::CallbackQuery(q) => q.message.as_ref(),
_ => None,
}
}
#[must_use]
pub fn chat_id(&self) -> Option<ChatId> {
self.update.chat_id().map(ChatId::Id)
}
#[must_use]
pub fn from_id(&self) -> Option<i64> {
self.update.from().map(|u| u.id)
}
#[must_use]
pub fn callback_query(&self) -> Option<&CallbackQuery> {
match &self.update.kind {
UpdateKind::CallbackQuery(q) => Some(q),
_ => None,
}
}
#[must_use]
pub fn inline_query(&self) -> Option<&InlineQuery> {
match &self.update.kind {
UpdateKind::InlineQuery(q) => Some(q),
_ => None,
}
}
#[must_use]
pub fn text(&self) -> Option<&str> {
self.message().and_then(|m| m.effective_text())
}
#[must_use]
pub fn command(&self) -> Option<&str> {
self.message().and_then(|m| m.command())
}
#[must_use]
pub fn is_ephemeral(&self) -> bool {
self.message()
.is_some_and(|m| m.ephemeral_message_id.is_some())
}
#[must_use]
pub fn ephemeral_message_id(&self) -> Option<i64> {
self.message()?.ephemeral_message_id
}
pub fn reply(
&self,
text: impl Into<String>,
) -> Option<rustigram_api::methods::sending::SendMessage> {
let chat_id = self.chat_id()?;
let mut builder = self.bot.send_message(chat_id, text);
if let Some(msg) = self.message() {
builder = builder.reply_to(msg.message_id);
}
Some(builder)
}
#[cfg(feature = "tma")]
#[must_use]
pub fn tma_data(&self) -> Option<&rustigram_types::message::WebAppData> {
self.message()?.web_app_data.as_ref()
}
}