Compare commits
No commits in common. "2590055ea195a4aaa54bd8c7dad80e990d73a161" and "25b980a2ffddaacbad6145d275240cc69b3e7d63" have entirely different histories.
2590055ea1
...
25b980a2ff
@ -1,62 +0,0 @@
|
|||||||
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,6 +1,5 @@
|
|||||||
pub mod application;
|
pub mod application;
|
||||||
pub mod callback_info;
|
pub mod callback_info;
|
||||||
pub mod message_forward;
|
|
||||||
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
|||||||
96
src/main.rs
96
src/main.rs
@ -5,10 +5,8 @@ pub mod utils;
|
|||||||
|
|
||||||
use db::application::Application;
|
use db::application::Application;
|
||||||
use db::callback_info::CallbackInfo;
|
use db::callback_info::CallbackInfo;
|
||||||
use db::message_forward::MessageForward;
|
|
||||||
use log::{error, info, warn};
|
use log::{error, info, warn};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use teloxide::sugar::request::RequestReplyExt;
|
|
||||||
use utils::create_callback_button;
|
use utils::create_callback_button;
|
||||||
|
|
||||||
use crate::admin::{admin_command_handler, AdminCommands};
|
use crate::admin::{admin_command_handler, AdminCommands};
|
||||||
@ -113,8 +111,6 @@ pub enum BotError {
|
|||||||
// TODO: not a really good to hardcode types, better to extend it later
|
// TODO: not a really good to hardcode types, better to extend it later
|
||||||
StorageError(#[from] mongodb_storage::MongodbStorageError<<Json as Serializer<State>>::Error>),
|
StorageError(#[from] mongodb_storage::MongodbStorageError<<Json as Serializer<State>>::Error>),
|
||||||
MsgTooOld(String),
|
MsgTooOld(String),
|
||||||
BotLogicError(String),
|
|
||||||
AdminMisconfiguration(String),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type BotResult<T> = Result<T, BotError>;
|
pub type BotResult<T> = Result<T, BotError>;
|
||||||
@ -189,12 +185,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
})
|
})
|
||||||
.endpoint(edit_msg_cmd_handler),
|
.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(
|
.branch(
|
||||||
dptree::case![State::Edit {
|
dptree::case![State::Edit {
|
||||||
literal,
|
literal,
|
||||||
@ -217,55 +207,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
Ok(())
|
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(
|
async fn button_edit_callback(
|
||||||
bot: Bot,
|
bot: Bot,
|
||||||
mut db: DB,
|
mut db: DB,
|
||||||
@ -441,9 +382,8 @@ async fn callback_handler(bot: Bot, mut db: DB, q: CallbackQuery) -> BotResult<(
|
|||||||
}
|
}
|
||||||
Callback::LeaveApplication => {
|
Callback::LeaveApplication => {
|
||||||
let application = Application::new(q.from.clone()).store(&mut db).await?;
|
let application = Application::new(q.from.clone()).store(&mut db).await?;
|
||||||
let msg = send_application_to_chat(&bot, &mut db, &application).await?;
|
send_application_to_chat(&bot, &mut db, &application).await?;
|
||||||
|
answer_message(
|
||||||
let (chat_id, msg_id) = answer_message(
|
|
||||||
&bot,
|
&bot,
|
||||||
q.from.id.0 as i64,
|
q.from.id.0 as i64,
|
||||||
&mut db,
|
&mut db,
|
||||||
@ -451,9 +391,6 @@ async fn callback_handler(bot: Bot, mut db: DB, q: CallbackQuery) -> BotResult<(
|
|||||||
None as Option<InlineKeyboardMarkup>,
|
None as Option<InlineKeyboardMarkup>,
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
MessageForward::new(msg.chat.id.0, msg.id.0, chat_id, msg_id, false)
|
|
||||||
.store(&mut db)
|
|
||||||
.await?;
|
|
||||||
}
|
}
|
||||||
Callback::AskQuestion => {
|
Callback::AskQuestion => {
|
||||||
answer_message(
|
answer_message(
|
||||||
@ -474,7 +411,7 @@ async fn send_application_to_chat(
|
|||||||
bot: &Bot,
|
bot: &Bot,
|
||||||
db: &mut DB,
|
db: &mut DB,
|
||||||
app: &Application<teloxide::types::User>,
|
app: &Application<teloxide::types::User>,
|
||||||
) -> BotResult<Message> {
|
) -> BotResult<()> {
|
||||||
let chat_id: i64 = match db.get_literal_value("support_chat_id").await? {
|
let chat_id: i64 = match db.get_literal_value("support_chat_id").await? {
|
||||||
Some(strcid) => match strcid.parse() {
|
Some(strcid) => match strcid.parse() {
|
||||||
Ok(cid) => cid,
|
Ok(cid) => cid,
|
||||||
@ -485,7 +422,7 @@ async fn send_application_to_chat(
|
|||||||
app.from
|
app.from
|
||||||
))
|
))
|
||||||
.await;
|
.await;
|
||||||
return Err(BotError::BotLogicError(format!("somewhere in bots logic support_chat_id literal not stored as a number, got: {strcid}")));
|
return Ok(());
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
@ -494,9 +431,7 @@ async fn send_application_to_chat(
|
|||||||
app.from
|
app.from
|
||||||
))
|
))
|
||||||
.await;
|
.await;
|
||||||
return Err(BotError::AdminMisconfiguration(format!(
|
return Ok(());
|
||||||
"admin forget to set support_chat_id"
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let msg = match db.get_literal_value("application_format").await? {
|
let msg = match db.get_literal_value("application_format").await? {
|
||||||
@ -512,13 +447,13 @@ async fn send_application_to_chat(
|
|||||||
),
|
),
|
||||||
None => {
|
None => {
|
||||||
notify_admin("format for support_chat_id is not set").await;
|
notify_admin("format for support_chat_id is not set").await;
|
||||||
return Err(BotError::AdminMisconfiguration(format!(
|
return Ok(());
|
||||||
"admin forget to set application_format"
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(bot.send_message(ChatId(chat_id), msg).await?)
|
bot.send_message(ChatId(chat_id), msg).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This is an emergent situation function, so it should not return any Result, but handle Results
|
/// This is an emergent situation function, so it should not return any Result, but handle Results
|
||||||
@ -830,7 +765,7 @@ async fn answer_message<RM: Into<ReplyMarkup>>(
|
|||||||
db: &mut DB,
|
db: &mut DB,
|
||||||
literal: &str,
|
literal: &str,
|
||||||
keyboard: Option<RM>,
|
keyboard: Option<RM>,
|
||||||
) -> BotResult<(i64, i32)> {
|
) -> BotResult<()> {
|
||||||
answer_message_varianted(bot, chat_id, db, literal, None, keyboard).await
|
answer_message_varianted(bot, chat_id, db, literal, None, keyboard).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -841,7 +776,7 @@ async fn answer_message_varianted<RM: Into<ReplyMarkup>>(
|
|||||||
literal: &str,
|
literal: &str,
|
||||||
variant: Option<&str>,
|
variant: Option<&str>,
|
||||||
keyboard: Option<RM>,
|
keyboard: Option<RM>,
|
||||||
) -> BotResult<(i64, i32)> {
|
) -> BotResult<()> {
|
||||||
answer_message_varianted_silence_flag(bot, chat_id, db, literal, variant, false, keyboard).await
|
answer_message_varianted_silence_flag(bot, chat_id, db, literal, variant, false, keyboard).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -853,7 +788,7 @@ async fn answer_message_varianted_silence_flag<RM: Into<ReplyMarkup>>(
|
|||||||
variant: Option<&str>,
|
variant: Option<&str>,
|
||||||
silence_non_variant: bool,
|
silence_non_variant: bool,
|
||||||
keyboard: Option<RM>,
|
keyboard: Option<RM>,
|
||||||
) -> BotResult<(i64, i32)> {
|
) -> BotResult<()> {
|
||||||
let variant_text = match variant {
|
let variant_text = match variant {
|
||||||
Some(variant) => {
|
Some(variant) => {
|
||||||
let value = db.get_literal_alternative_value(literal, variant).await?;
|
let value = db.get_literal_alternative_value(literal, variant).await?;
|
||||||
@ -991,7 +926,7 @@ async fn answer_message_varianted_silence_flag<RM: Into<ReplyMarkup>>(
|
|||||||
}
|
}
|
||||||
None => db.set_message_literal(chat_id, msg_id, literal).await?,
|
None => db.set_message_literal(chat_id, msg_id, literal).await?,
|
||||||
};
|
};
|
||||||
Ok((chat_id, msg_id))
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn replace_message(
|
async fn replace_message(
|
||||||
@ -1036,7 +971,7 @@ async fn replace_message(
|
|||||||
{
|
{
|
||||||
// fallback to sending message
|
// fallback to sending message
|
||||||
warn!("Fallback into sending message instead of editing because it contains media");
|
warn!("Fallback into sending message instead of editing because it contains media");
|
||||||
answer_message_varianted_silence_flag(
|
return answer_message_varianted_silence_flag(
|
||||||
bot,
|
bot,
|
||||||
chat_id,
|
chat_id,
|
||||||
db,
|
db,
|
||||||
@ -1045,8 +980,7 @@ async fn replace_message(
|
|||||||
true,
|
true,
|
||||||
keyboard,
|
keyboard,
|
||||||
)
|
)
|
||||||
.await?;
|
.await;
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
Err(err) => return Err(err.into()),
|
Err(err) => return Err(err.into()),
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user