use teloxide::Bot;
use teloxide::payloads::SendChatActionSetters;
use teloxide::payloads::SendMessageSetters;
use teloxide::payloads::SendPhotoSetters;
use teloxide::prelude::Requester;
use teloxide::requests::JsonRequest;
use teloxide::types::{ChatAction, ChatId, InputFile, MessageId, ThreadId};
pub async fn latest_thread_id_for_chat(chat_id: i64) -> Option<ThreadId> {
let pool = crate::db::global_pool()?;
let repo = crate::db::ChannelMessageRepository::new(pool.clone());
let chat_id_str = chat_id.to_string();
let rows = repo
.recent(Some("telegram"), &chat_id_str, 1, None)
.await
.ok()?;
let row = rows.into_iter().next()?;
let tid_str = row.thread_id?;
tid_str.parse::<i32>().ok().map(|n| ThreadId(MessageId(n)))
}
pub fn message_in_thread<C, T>(
bot: &Bot,
chat_id: C,
thread_id: Option<ThreadId>,
text: T,
) -> JsonRequest<teloxide::payloads::SendMessage>
where
C: Into<ChatId>,
T: Into<String>,
{
let req = bot.send_message(chat_id.into(), text.into());
match thread_id {
Some(t) => req.message_thread_id(t),
None => req,
}
}
pub fn photo_in_thread<C>(
bot: &Bot,
chat_id: C,
thread_id: Option<ThreadId>,
photo: InputFile,
) -> teloxide::requests::MultipartRequest<teloxide::payloads::SendPhoto>
where
C: Into<ChatId>,
{
let req = bot.send_photo(chat_id.into(), photo);
match thread_id {
Some(t) => req.message_thread_id(t),
None => req,
}
}
pub fn chat_action_in_thread<C>(
bot: &Bot,
chat_id: C,
thread_id: Option<ThreadId>,
action: ChatAction,
) -> JsonRequest<teloxide::payloads::SendChatAction>
where
C: Into<ChatId>,
{
let req = bot.send_chat_action(chat_id.into(), action);
match thread_id {
Some(t) => req.message_thread_id(t),
None => req,
}
}