From 5fbc3635236fbf6444bf51cbb03d67c88f06bfe8 Mon Sep 17 00:00:00 2001 From: Amir Mohammadi Date: Mon, 13 May 2024 21:58:33 +0330 Subject: [PATCH] implement report: big brother is watching --- config-example.toml | 1 + src/bot.rs | 38 ++++++++++++++++++++++++++++---------- src/commands.rs | 23 +++++++++++++++-------- src/config.rs | 5 +++-- src/response/report | 5 +++++ src/telegram.rs | 12 ++++++++++++ 6 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 src/response/report diff --git a/config-example.toml b/config-example.toml index 95f0e3f..0efe772 100644 --- a/config-example.toml +++ b/config-example.toml @@ -3,6 +3,7 @@ admin_users_id = [ 7357 ] +allowed_chats_id = [] report_chat_id = 7357 mastodon_api_key = "" diff --git a/src/bot.rs b/src/bot.rs index fe118b8..267b5f4 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,11 +1,11 @@ use crate::config::Config; -use crate::telegram::{PinChatMessage, WebhookReply}; +use crate::telegram::{ForwardMessage, PinChatMessage, WebhookReply}; use std::collections::HashMap; use telegram_types::bot::{ methods::{ChatTarget, SendMessage}, - types::{ChatId, Message, MessageId, ParseMode}, + types::{ChatId, Message, ParseMode}, }; use worker::*; @@ -64,15 +64,33 @@ impl Bot { })) } + pub fn forward(&self, msg: &Message, chat_id: ChatId) -> Result { + let from_chat_id = msg.chat.id; + let message_id = msg.message_id; + + Response::from_json(&WebhookReply::from(ForwardMessage { + chat_id, + from_chat_id, + message_id, + })) + } + pub async fn process(&self, msg: &Message) -> Result { - if let Some(command) = msg - .text - .as_ref() - .map(|t| t.trim()) - .filter(|t| t.starts_with("!")) - .and_then(|t| self.commands.get(t)) - { - return command(self, msg); + if !self.config.bot.allowed_chats_id.contains(&msg.chat.id) { + // report unallowed chats + return self.forward(msg, self.config.bot.report_chat_id); + } + + if self.config.bot.allowed_chats_id.contains(&msg.chat.id) { + if let Some(command) = msg + .text + .as_ref() + .map(|t| t.trim()) + .filter(|t| t.starts_with("!")) + .and_then(|t| self.commands.get(t)) + { + return command(self, msg); + } } Response::empty() diff --git a/src/commands.rs b/src/commands.rs index 465de87..01ae0f6 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -4,18 +4,25 @@ use telegram_types::bot::types::Message; use worker::*; pub fn share(bot: &Bot, msg: &Message) -> Result { - bot.pin(msg) + if let Some(user) = &msg.from { + if bot.config.bot.admin_users_id.contains(&user.id) { + // TODO: share the message on the linked social media + return bot.pin(msg); + } + } + Response::empty() } pub fn report(bot: &Bot, msg: &Message) -> Result { let id = bot.config.bot.report_chat_id; + let reporter = msg.from.as_ref(); + let reportee = msg.reply_to_message.as_ref().map(|x| x.from.clone()); + let message = msg.reply_to_message.as_ref(); - let reporter = msg.from.as_ref().map(|x| x.id.0).unwrap_or_default(); - let reportee = if let Some(m) = &msg.reply_to_message { - m.from.as_ref().map(|x| x.id.0).unwrap_or_default() - } else { - 0 - }; + let report = format!( + include_str!("./response/report"), + reporter, reportee, message + ); - bot.send(id, &format!("{}", reporter)) + bot.send(id, &report) } diff --git a/src/config.rs b/src/config.rs index e94416e..42497cc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,6 @@ use cidr::IpCidr; use serde::Deserialize; -use telegram_types::bot::types::ChatId; +use telegram_types::bot::types::{ChatId, UserId}; #[derive(Deserialize)] pub struct Config { @@ -10,8 +10,9 @@ pub struct Config { #[derive(Deserialize)] pub struct BotConfig { - pub admin_users_id: Vec, + pub admin_users_id: Vec, pub report_chat_id: ChatId, + pub allowed_chats_id: Vec, } #[derive(Deserialize)] diff --git a/src/response/report b/src/response/report new file mode 100644 index 0000000..5bbbc8d --- /dev/null +++ b/src/response/report @@ -0,0 +1,5 @@ +reporter: {:?} + +reportee: {:?} + +message: {:?} diff --git a/src/telegram.rs b/src/telegram.rs index 8b5614b..5501ff7 100644 --- a/src/telegram.rs +++ b/src/telegram.rs @@ -30,3 +30,15 @@ impl Method for PinChatMessage { const NAME: &'static str = "pinChatMessage"; type Item = Message; } + +#[derive(Clone, Serialize)] +pub struct ForwardMessage { + pub chat_id: ChatId, + pub from_chat_id: ChatId, + pub message_id: MessageId, +} + +impl Method for ForwardMessage { + const NAME: &'static str = "forwardMessage"; + type Item = Message; +}