Skip to content

Commit

Permalink
feat: 处理用户对另一个用户的模拟指令行为
Browse files Browse the repository at this point in the history
  • Loading branch information
fython committed Feb 27, 2024
1 parent 17b96f5 commit c8a06fa
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
mod commands;
mod configs;
mod msgfmt;
mod rpt;
mod userinteract;

use teloxide::Bot;
use teloxide::prelude::*;
use teloxide::types::{MediaKind, MediaText, MessageKind};
use crate::commands::*;
use crate::configs::BotConfigs;
use crate::rpt::{REPEATER_STATES, RepeaterNextAction};
use crate::rpt::{RepeaterNextAction, REPEATER_STATES};
use teloxide::prelude::*;
use teloxide::types::{MediaKind, MediaText, MessageKind};
use teloxide::Bot;

#[tokio::main]
async fn main() {
Expand Down Expand Up @@ -48,14 +50,21 @@ async fn handle_cmd(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()>
async fn handle_messages_in_any_groups(bot: Bot, msg: Message) -> ResponseResult<()> {
log::debug!("receive group msg: text={:?}", msg.text());

if let MessageKind::Common(common) = msg.kind {
if let MediaKind::Text(MediaText { text, entities: _ }) = common.media_kind {
match REPEATER_STATES.get_next_action(msg.chat.id, text.clone()) {
RepeaterNextAction::Repeat => {
log::info!("{} needs repeat", text.clone());
bot.send_message(msg.chat.id, text).await?;
},
_ => (),
if let MessageKind::Common(common) = &msg.kind {
if let MediaKind::Text(MediaText { text, entities: _ }) = &common.media_kind {
if userinteract::handle_user_do_sth_to_another(&bot, &msg)
.await
.is_some()
{
return Ok(());
} else {
match REPEATER_STATES.get_next_action(msg.chat.id, text.clone()) {
RepeaterNextAction::Repeat => {
log::info!("{} needs repeat", text.clone());
bot.send_message(msg.chat.id, text).await?;
}
_ => (),
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/msgfmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use teloxide::types::User;
use teloxide::utils::markdown;

pub(crate) fn markup_username_with_link(user: &User) -> String {
markdown::link(
&format!("tg://user?id={}", user.id),
&markdown::escape(&user.full_name()),
)
}
42 changes: 42 additions & 0 deletions src/userinteract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::msgfmt::markup_username_with_link;
use teloxide::prelude::*;
use teloxide::types::ParseMode;

/// 处理用户对另一个用户的模拟指令行为
///
/// 当用户发送类似 `/do_sth` 的消息时,机器人会回复类似 `@user1 do_sth 了 @user2` 的消息
/// 若消息不符合条件时,此函数会返回 None。此外,发送失败时不会传递错误到上游
pub(crate) async fn handle_user_do_sth_to_another(bot: &Bot, msg: &Message) -> Option<()> {
let from_user = msg.from();
let reply_user = msg.reply_to_message().and_then(|reply| reply.from());
let text = msg.text();
if text.is_none() || from_user.is_none() || reply_user.is_none() {
return None;
}
let text = text.unwrap();
if text.starts_with("/") && text.find("@").is_none() {
let act = text.strip_prefix("/");
if act.is_none() {
return None;
}
let act = act.unwrap();
let res = bot
.send_message(
msg.chat.id,
format!(
"{} {}了 {}",
markup_username_with_link(from_user.unwrap()),
act,
markup_username_with_link(reply_user.unwrap())
),
)
.parse_mode(ParseMode::MarkdownV2)
.await;
if res.is_err() {
log::error!("failed to send message: {:?}", res.err());
}
// TODO 统计行为次数
return Some(());
}
return None;
}

0 comments on commit c8a06fa

Please sign in to comment.