-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: change teloxide to dev branch
cancel support of webhook
- Loading branch information
itsusinn
committed
Feb 5, 2022
1 parent
5350e29
commit 3962ba2
Showing
13 changed files
with
112 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,56 @@ | ||
use crate::config::CONFIG; | ||
use std::sync::Arc; | ||
use teloxide::dispatching::UpdateWithCx; | ||
use teloxide::prelude::*; | ||
|
||
use teloxide::prelude2::*; | ||
use teloxide::utils::command::BotCommand; | ||
|
||
#[derive(BotCommand)] | ||
#[derive(BotCommand, Clone)] | ||
#[command(rename = "lowercase", description = "信使Bot支持以下命令")] | ||
pub enum Command { | ||
#[command(description = "显示命令帮助")] | ||
Help, | ||
#[command(description = "启用消息转发")] | ||
Enable, | ||
#[command(description = "禁用消息转发")] | ||
Disable, | ||
// #[command(description = "启用消息转发")] | ||
// Enable, | ||
// #[command(description = "禁用消息转发")] | ||
// Disable, | ||
#[command(description = "设置当前Group的转发地址", parse_with = "split")] | ||
SetAddress { address: String }, | ||
} | ||
|
||
pub async fn answer( | ||
cx: Arc<UpdateWithCx<AutoSend<Bot>, Message>>, | ||
command: Command, | ||
) -> anyhow::Result<()> { | ||
match command { | ||
Command::Help => { | ||
cx.answer(Command::descriptions()).await?; | ||
} | ||
Command::Enable => { | ||
cx.answer("Mesagisto信使已启用").await?; | ||
} | ||
Command::Disable => { | ||
cx.answer("Mesagisto信使已禁用").await?; | ||
} | ||
Command::SetAddress { address } => { | ||
let sender_id = cx.update.from().unwrap().id; | ||
let chat_id = cx.chat_id(); | ||
let admins = cx.requester.get_chat_administrators(chat_id).await?; | ||
let mut is_admin = false; | ||
for admin in admins { | ||
if admin.user.id == sender_id { | ||
is_admin = true; | ||
break; | ||
} | ||
impl Command { | ||
pub async fn answer(msg: Message, bot: AutoSend<Bot>, cmd: Command) -> anyhow::Result<()> { | ||
match cmd { | ||
Command::Help => { | ||
let chat_id = msg.chat_id(); | ||
bot.send_message(chat_id, Command::descriptions()).await?; | ||
} | ||
if is_admin { | ||
CONFIG.target_address_mapper.insert(chat_id, address.into()); | ||
cx.answer("成功设置当前Group的信使地址").await?; | ||
} else { | ||
cx.answer("权限不足,拒绝设置信使频道").await?; | ||
// Command::Enable => { | ||
// cx.answer("Mesagisto信使已启用").await?; | ||
// } | ||
// Command::Disable => { | ||
// cx.answer("Mesagisto信使已禁用").await?; | ||
// } | ||
Command::SetAddress { address } => { | ||
let sender_id = msg.from().unwrap().id; | ||
let chat_id = msg.chat_id(); | ||
let admins = bot.get_chat_administrators(chat_id).await?; | ||
let mut is_admin = false; | ||
for admin in admins { | ||
if admin.user.id == sender_id { | ||
is_admin = true; | ||
break; | ||
} | ||
} | ||
if is_admin { | ||
CONFIG.target_address_mapper.insert(chat_id, address.into()); | ||
bot | ||
.send_message(chat_id, "成功设置当前Group的信使地址") | ||
.await?; | ||
} else { | ||
bot | ||
.send_message(chat_id, "权限不足,拒绝设置信使频道") | ||
.await?; | ||
} | ||
} | ||
} | ||
}; | ||
Ok(()) | ||
}; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,5 +78,5 @@ pub struct WebhookConfig { | |
|
||
#[basic_derive] | ||
pub struct FormatConfig { | ||
pub msg: ArcStr | ||
pub msg: ArcStr, | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use teloxide::prelude2::*; | ||
use tracing::info; | ||
|
||
use crate::{command::Command, message::handlers}; | ||
|
||
pub async fn start(bot: &AutoSend<Bot>) { | ||
let handler = Update::filter_message() | ||
.branch( | ||
dptree::entry() | ||
.filter_command::<Command>() | ||
.endpoint(Command::answer), | ||
) | ||
.branch( | ||
dptree::filter(|msg: Message| msg.chat.is_group() || msg.chat.is_supergroup()) | ||
.endpoint(handlers::send::answer_common), | ||
); | ||
info!("Mesagisto-Bot启动成功"); | ||
Dispatcher::builder(bot.clone(), handler) | ||
.default_handler(|upd| async move { | ||
log::warn!("Unhandled update: {:?}", upd); | ||
}) | ||
// If the dispatcher fails for some reason, execute this handler. | ||
.error_handler(LoggingErrorHandler::with_custom_text( | ||
"An error has occurred in the dispatcher", | ||
)) | ||
.build() | ||
.setup_ctrlc_handler() | ||
.dispatch() | ||
.await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.