3 Commits

Author SHA1 Message Date
Sense T
064565ff00 bugf 2024-02-29 10:47:45 +08:00
Sense T
90fdef4363 version 2024-02-04 13:29:39 +08:00
Sense T
df99275455 group blacklist 2024-01-18 17:19:37 +08:00
5 changed files with 49 additions and 21 deletions

View File

@@ -7,7 +7,8 @@ secretGenerator:
files:
- config/DATABASE_URI
- config/TGBOT_TOKEN
- config/GROUP_BANNED
images:
- name: <Image>
newName: ghcr.io/senseab/hangitbot
newTag: v0.0.3
newTag: v0.1.2

View File

@@ -75,14 +75,21 @@
description = lib.mdDoc "Custom telegram api URI";
};
groupBanned = mkOption {
type = types.listOf types.int;
default = [];
description = lib.mdDoc "GroupID blacklisted";
};
extraOptions = mkOption {
type = types.str;
description = lib.mdDoc "Extra option for bot.";
default = "";
};
};
config = let
args = "${cfg.extraOptions} ${if isString cfg.tgUri then "--api-uri ${escapeShellArg cfg.tgUri}" else ""}";
args = "${cfg.extraOptions} ${if cfg?tgUri then "--api-uri ${escapeShellArg cfg.tgUri}" else ""} ${if cfg?groupBanned then concatStringsSep " " (lists.concatMap (group: ["-b ${group}"]) cfg.groupBanned) else ""}";
in mkIf cfg.enable {
systemd.services.hangitbot = {
wantedBy = ["multi-uesr.target"];

View File

@@ -24,4 +24,8 @@ pub struct Args {
/// Api Server URL
#[clap(long, value_parser, env = "API_URL", default_value=DEFAULT_API_URL)]
pub api_url: String,
/// GroupID blacklisted
#[clap(short = 'b', long, value_parser, env = "GROUP_BANNED", value_delimiter = ',', num_args = 1..)]
pub group_banned: Vec<i64>
}

View File

@@ -7,7 +7,7 @@ use teloxide::{
},
RequestError,
};
use wd_log::{log_error_ln, log_debug_ln};
use wd_log::{log_debug_ln, log_error_ln};
use crate::{
db_controller::Controller,
@@ -23,7 +23,7 @@ pub async fn inline_menu(db: &Controller, bot: &Bot, q: InlineQuery) -> Result<(
.iter()
.map(|n| {
InlineQueryResult::Article(InlineQueryResultArticle::new(
n.name.clone(),
format!("{},{}", n.id, n.name),
format!("{} {}", BOT_TEXT_INLINE_HANG, n.name),
InputMessageContent::Text(InputMessageContentText::new(hangit_text(
n.name.clone(),
@@ -37,15 +37,19 @@ pub async fn inline_menu(db: &Controller, bot: &Bot, q: InlineQuery) -> Result<(
None => vec![],
};
results.push(InlineQueryResult::Article(InlineQueryResultArticle::new(
name.clone(),
format!("{} {}", BOT_TEXT_INLINE_HANG, name.clone()),
InputMessageContent::Text(InputMessageContentText::new(hangit_text(
name.clone(),
!IS_SELF,
!NEED_ESCAPE,
))),
)));
log_debug_ln!("{:?}", results);
if results.is_empty() {
results.push(InlineQueryResult::Article(InlineQueryResultArticle::new(
format!("{},{}", 0, name),
format!("{} {}", BOT_TEXT_INLINE_HANG, name.clone()),
InputMessageContent::Text(InputMessageContentText::new(hangit_text(
name.clone(),
!IS_SELF,
!NEED_ESCAPE,
))),
)));
}
if name.starts_with("@") {
results = vec![]
@@ -58,11 +62,15 @@ pub async fn inline_menu(db: &Controller, bot: &Bot, q: InlineQuery) -> Result<(
pub async fn inline_anwser(db: &Controller, a: ChosenInlineResult) -> Result<(), RequestError> {
log_debug_ln!("{:#?}", a);
if a.result_id == "@" {
let mut c: Vec<&str> = a.result_id.split(",").collect();
c.remove(0);
let result_id = c.concat();
if result_id == "@" {
return Ok(());
}
if let Err(err) = db.hangit(&a.result_id, ChatId(0)).await {
if let Err(err) = db.hangit(&result_id, ChatId(0)).await {
log_error_ln!("{:?}", err);
}

View File

@@ -19,7 +19,7 @@ use teloxide::{
};
use utils::message_handler;
use wd_log::{
log_debug_ln, log_error_ln, log_info_ln, log_panic, set_level, set_prefix, DEBUG, INFO,
log_debug_ln, log_error_ln, log_info_ln, log_panic, set_level, set_prefix, DEBUG, INFO, log_warn_ln,
};
#[tokio::main]
@@ -56,7 +56,11 @@ async fn main() {
.branch(
Update::filter_message()
.branch(dptree::entry().filter_command::<Commands>().endpoint(
|db: Controller, bot: Bot, message: Message, cmd: Commands| async move {
|db: Controller, black_list: Vec<i64>, bot: Bot, message: Message, cmd: Commands| async move {
if black_list.contains(&message.chat.id.0) {
log_warn_ln!("banned group dectected: {:?}", message.chat.id);
return Ok(());
}
let r = match cmd {
Commands::Help => help_handler(&bot, &message).await,
Commands::About => about_handler(&bot, &message).await,
@@ -75,8 +79,12 @@ async fn main() {
))
.branch(
dptree::filter(|msg: Message| msg.chat.is_group() || msg.chat.is_supergroup())
.endpoint(|db: Controller, msg: Message, me: Me| async move {
let r = message_handler(&db, msg, &me).await;
.endpoint(|db: Controller, message: Message, me: Me, black_list: Vec<i64>| async move {
if black_list.contains(&message.chat.id.0) {
log_warn_ln!("banned group dectected: {:?}", message.chat.id);
return Ok(());
}
let r = message_handler(&db, message, &me).await;
match r {
Ok(_) => Ok(()),
Err(err) => {
@@ -99,7 +107,7 @@ async fn main() {
));
Dispatcher::builder(bot, handler)
.dependencies(dptree::deps![db_controller, me])
.dependencies(dptree::deps![db_controller, me, args.group_banned])
.default_handler(|upd| async move { log_debug_ln!("unhandled update: {:?}", upd) })
.enable_ctrlc_handler()
.build()