Skip to content

Commit

Permalink
implement report: big brother is watching
Browse files Browse the repository at this point in the history
  • Loading branch information
amiremohamadi committed May 13, 2024
1 parent 3b43d50 commit 5fbc363
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 20 deletions.
1 change: 1 addition & 0 deletions config-example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
admin_users_id = [
7357
]
allowed_chats_id = []
report_chat_id = 7357
mastodon_api_key = ""

Expand Down
38 changes: 28 additions & 10 deletions src/bot.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down Expand Up @@ -64,15 +64,33 @@ impl Bot {
}))
}

pub fn forward(&self, msg: &Message, chat_id: ChatId) -> Result<Response> {
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<Response> {
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()
Expand Down
23 changes: 15 additions & 8 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@ use telegram_types::bot::types::Message;
use worker::*;

pub fn share(bot: &Bot, msg: &Message) -> Result<Response> {
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<Response> {
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)
}
5 changes: 3 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -10,8 +10,9 @@ pub struct Config {

#[derive(Deserialize)]
pub struct BotConfig {
pub admin_users_id: Vec<i64>,
pub admin_users_id: Vec<UserId>,
pub report_chat_id: ChatId,
pub allowed_chats_id: Vec<ChatId>,
}

#[derive(Deserialize)]
Expand Down
5 changes: 5 additions & 0 deletions src/response/report
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
reporter: {:?}

reportee: {:?}

message: {:?}
12 changes: 12 additions & 0 deletions src/telegram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 5fbc363

Please sign in to comment.