Compare commits
8 Commits
25b980a2ff
...
2590055ea1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2590055ea1 | ||
|
|
aaed6fcdca | ||
|
|
c1c331ab29 | ||
|
|
729f0c3eea | ||
|
|
27a829b784 | ||
|
|
c69b6595fd | ||
|
|
091a42bf31 | ||
|
|
2acda3087b |
62
src/db/message_forward.rs
Normal file
62
src/db/message_forward.rs
Normal file
@ -0,0 +1,62 @@
|
||||
use bson::doc;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::DbResult;
|
||||
use crate::query_call_consume;
|
||||
use crate::CallDB;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct MessageForward {
|
||||
pub _id: bson::oid::ObjectId,
|
||||
pub chat_id: i64,
|
||||
pub message_id: i32,
|
||||
pub source_chat_id: i64,
|
||||
pub source_message_id: i32,
|
||||
pub reply: bool,
|
||||
}
|
||||
|
||||
impl MessageForward {
|
||||
pub fn new(
|
||||
chat_id: i64,
|
||||
message_id: i32,
|
||||
source_chat_id: i64,
|
||||
source_message_id: i32,
|
||||
reply: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
_id: Default::default(),
|
||||
chat_id,
|
||||
message_id,
|
||||
source_chat_id,
|
||||
source_message_id,
|
||||
reply,
|
||||
}
|
||||
}
|
||||
|
||||
query_call_consume!(store, self, db, Self, {
|
||||
let db = db.get_database().await;
|
||||
let ci = db.collection::<Self>("message_forward");
|
||||
|
||||
ci.insert_one(&self).await?;
|
||||
|
||||
Ok(self)
|
||||
});
|
||||
|
||||
pub async fn get<D: CallDB>(
|
||||
db: &mut D,
|
||||
chat_id: i64,
|
||||
message_id: i32,
|
||||
) -> DbResult<Option<Self>> {
|
||||
let db = db.get_database().await;
|
||||
let ci = db.collection::<Self>("message_forward");
|
||||
|
||||
let mf = ci
|
||||
.find_one(doc! {
|
||||
"chat_id": chat_id,
|
||||
"message_id": message_id,
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(mf)
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
pub mod application;
|
||||
pub mod callback_info;
|
||||
pub mod message_forward;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
|
||||
96
src/main.rs
96
src/main.rs
@ -5,8 +5,10 @@ pub mod utils;
|
||||
|
||||
use db::application::Application;
|
||||
use db::callback_info::CallbackInfo;
|
||||
use db::message_forward::MessageForward;
|
||||
use log::{error, info, warn};
|
||||
use std::time::Duration;
|
||||
use teloxide::sugar::request::RequestReplyExt;
|
||||
use utils::create_callback_button;
|
||||
|
||||
use crate::admin::{admin_command_handler, AdminCommands};
|
||||
@ -111,6 +113,8 @@ pub enum BotError {
|
||||
// TODO: not a really good to hardcode types, better to extend it later
|
||||
StorageError(#[from] mongodb_storage::MongodbStorageError<<Json as Serializer<State>>::Error>),
|
||||
MsgTooOld(String),
|
||||
BotLogicError(String),
|
||||
AdminMisconfiguration(String),
|
||||
}
|
||||
|
||||
pub type BotResult<T> = Result<T, BotError>;
|
||||
@ -185,6 +189,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
})
|
||||
.endpoint(edit_msg_cmd_handler),
|
||||
)
|
||||
.branch(
|
||||
Update::filter_message()
|
||||
.filter(|msg: Message| msg.reply_to_message().is_some())
|
||||
.filter(|state: State| matches!(state, State::Start))
|
||||
.endpoint(support_reply_handler),
|
||||
)
|
||||
.branch(
|
||||
dptree::case![State::Edit {
|
||||
literal,
|
||||
@ -207,6 +217,55 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn support_reply_handler(bot: Bot, mut db: DB, msg: Message) -> BotResult<()> {
|
||||
use teloxide::utils::render::Renderer;
|
||||
|
||||
let rm = match msg.reply_to_message() {
|
||||
Some(rm) => rm,
|
||||
None => {
|
||||
return Err(BotError::BotLogicError(
|
||||
"support_reply_handler should not be called when no message is replied".to_string(),
|
||||
));
|
||||
}
|
||||
};
|
||||
let (chat_id, message_id) = (rm.chat.id.0, rm.id.0);
|
||||
let mf = match MessageForward::get(&mut db, chat_id, message_id).await? {
|
||||
Some(mf) => mf,
|
||||
None => {
|
||||
bot.send_message(msg.chat.id, "No forwarded message found for your reply")
|
||||
.await?;
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
let text = match msg.kind {
|
||||
MessageKind::Common(message_common) => match message_common.media_kind {
|
||||
MediaKind::Text(media_text) => {
|
||||
Renderer::new(&media_text.text, &media_text.entities).as_html()
|
||||
}
|
||||
_ => {
|
||||
bot.send_message(msg.chat.id, "Only text messages currently supported!")
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
},
|
||||
// can't hapen because we already have check for reply
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let msg = bot
|
||||
.send_message(ChatId(mf.source_chat_id), text)
|
||||
.parse_mode(ParseMode::Html);
|
||||
let msg = match mf.reply {
|
||||
false => msg,
|
||||
true => msg.reply_to(MessageId(mf.source_message_id)),
|
||||
};
|
||||
msg.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn button_edit_callback(
|
||||
bot: Bot,
|
||||
mut db: DB,
|
||||
@ -382,8 +441,9 @@ async fn callback_handler(bot: Bot, mut db: DB, q: CallbackQuery) -> BotResult<(
|
||||
}
|
||||
Callback::LeaveApplication => {
|
||||
let application = Application::new(q.from.clone()).store(&mut db).await?;
|
||||
send_application_to_chat(&bot, &mut db, &application).await?;
|
||||
answer_message(
|
||||
let msg = send_application_to_chat(&bot, &mut db, &application).await?;
|
||||
|
||||
let (chat_id, msg_id) = answer_message(
|
||||
&bot,
|
||||
q.from.id.0 as i64,
|
||||
&mut db,
|
||||
@ -391,6 +451,9 @@ async fn callback_handler(bot: Bot, mut db: DB, q: CallbackQuery) -> BotResult<(
|
||||
None as Option<InlineKeyboardMarkup>,
|
||||
)
|
||||
.await?;
|
||||
MessageForward::new(msg.chat.id.0, msg.id.0, chat_id, msg_id, false)
|
||||
.store(&mut db)
|
||||
.await?;
|
||||
}
|
||||
Callback::AskQuestion => {
|
||||
answer_message(
|
||||
@ -411,7 +474,7 @@ async fn send_application_to_chat(
|
||||
bot: &Bot,
|
||||
db: &mut DB,
|
||||
app: &Application<teloxide::types::User>,
|
||||
) -> BotResult<()> {
|
||||
) -> BotResult<Message> {
|
||||
let chat_id: i64 = match db.get_literal_value("support_chat_id").await? {
|
||||
Some(strcid) => match strcid.parse() {
|
||||
Ok(cid) => cid,
|
||||
@ -422,7 +485,7 @@ async fn send_application_to_chat(
|
||||
app.from
|
||||
))
|
||||
.await;
|
||||
return Ok(());
|
||||
return Err(BotError::BotLogicError(format!("somewhere in bots logic support_chat_id literal not stored as a number, got: {strcid}")));
|
||||
}
|
||||
},
|
||||
None => {
|
||||
@ -431,7 +494,9 @@ async fn send_application_to_chat(
|
||||
app.from
|
||||
))
|
||||
.await;
|
||||
return Ok(());
|
||||
return Err(BotError::AdminMisconfiguration(format!(
|
||||
"admin forget to set support_chat_id"
|
||||
)));
|
||||
}
|
||||
};
|
||||
let msg = match db.get_literal_value("application_format").await? {
|
||||
@ -447,13 +512,13 @@ async fn send_application_to_chat(
|
||||
),
|
||||
None => {
|
||||
notify_admin("format for support_chat_id is not set").await;
|
||||
return Ok(());
|
||||
return Err(BotError::AdminMisconfiguration(format!(
|
||||
"admin forget to set application_format"
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
bot.send_message(ChatId(chat_id), msg).await?;
|
||||
|
||||
Ok(())
|
||||
Ok(bot.send_message(ChatId(chat_id), msg).await?)
|
||||
}
|
||||
|
||||
/// This is an emergent situation function, so it should not return any Result, but handle Results
|
||||
@ -765,7 +830,7 @@ async fn answer_message<RM: Into<ReplyMarkup>>(
|
||||
db: &mut DB,
|
||||
literal: &str,
|
||||
keyboard: Option<RM>,
|
||||
) -> BotResult<()> {
|
||||
) -> BotResult<(i64, i32)> {
|
||||
answer_message_varianted(bot, chat_id, db, literal, None, keyboard).await
|
||||
}
|
||||
|
||||
@ -776,7 +841,7 @@ async fn answer_message_varianted<RM: Into<ReplyMarkup>>(
|
||||
literal: &str,
|
||||
variant: Option<&str>,
|
||||
keyboard: Option<RM>,
|
||||
) -> BotResult<()> {
|
||||
) -> BotResult<(i64, i32)> {
|
||||
answer_message_varianted_silence_flag(bot, chat_id, db, literal, variant, false, keyboard).await
|
||||
}
|
||||
|
||||
@ -788,7 +853,7 @@ async fn answer_message_varianted_silence_flag<RM: Into<ReplyMarkup>>(
|
||||
variant: Option<&str>,
|
||||
silence_non_variant: bool,
|
||||
keyboard: Option<RM>,
|
||||
) -> BotResult<()> {
|
||||
) -> BotResult<(i64, i32)> {
|
||||
let variant_text = match variant {
|
||||
Some(variant) => {
|
||||
let value = db.get_literal_alternative_value(literal, variant).await?;
|
||||
@ -926,7 +991,7 @@ async fn answer_message_varianted_silence_flag<RM: Into<ReplyMarkup>>(
|
||||
}
|
||||
None => db.set_message_literal(chat_id, msg_id, literal).await?,
|
||||
};
|
||||
Ok(())
|
||||
Ok((chat_id, msg_id))
|
||||
}
|
||||
|
||||
async fn replace_message(
|
||||
@ -971,7 +1036,7 @@ async fn replace_message(
|
||||
{
|
||||
// fallback to sending message
|
||||
warn!("Fallback into sending message instead of editing because it contains media");
|
||||
return answer_message_varianted_silence_flag(
|
||||
answer_message_varianted_silence_flag(
|
||||
bot,
|
||||
chat_id,
|
||||
db,
|
||||
@ -980,7 +1045,8 @@ async fn replace_message(
|
||||
true,
|
||||
keyboard,
|
||||
)
|
||||
.await;
|
||||
.await?;
|
||||
return Ok(());
|
||||
}
|
||||
Err(err) => return Err(err.into()),
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user