|
|
@ -39,7 +39,6 @@ enum Command {
|
|
|
|
#[command(description = "Show help.")]
|
|
|
|
#[command(description = "Show help.")]
|
|
|
|
Help,
|
|
|
|
Help,
|
|
|
|
// Fallback for an unknown command
|
|
|
|
// Fallback for an unknown command
|
|
|
|
/// Unknown command
|
|
|
|
|
|
|
|
#[command(description = "Unknown command.")]
|
|
|
|
#[command(description = "Unknown command.")]
|
|
|
|
Unknown(String),
|
|
|
|
Unknown(String),
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -69,16 +68,34 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// A: new chat members,
|
|
|
|
// A: new chat members,
|
|
|
|
// B: recognized commands (/start, /help, etc.)
|
|
|
|
// B: recognized commands (/start, /help, etc.)
|
|
|
|
let handler = dptree::entry()
|
|
|
|
let handler = dptree::entry()
|
|
|
|
|
|
|
|
// 1) Branch A: the usual case for the bot being added after the group is created
|
|
|
|
.branch(
|
|
|
|
.branch(
|
|
|
|
Update::filter_message()
|
|
|
|
Update::filter_message()
|
|
|
|
.filter(|msg: Message| msg.new_chat_members().is_some())
|
|
|
|
.filter(|msg: Message| msg.new_chat_members().is_some())
|
|
|
|
.endpoint(handle_new_chat_members),
|
|
|
|
.endpoint(handle_new_chat_members),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
// 2) Branch B: if the group is created with the bot already in it
|
|
|
|
|
|
|
|
.branch(
|
|
|
|
|
|
|
|
Update::filter_message()
|
|
|
|
|
|
|
|
.filter(|msg: Message| {
|
|
|
|
|
|
|
|
// For newly created group
|
|
|
|
|
|
|
|
msg.group_chat_created().is_some()
|
|
|
|
|
|
|
|
// Or newly created supergroup
|
|
|
|
|
|
|
|
|| msg.super_group_chat_created().is_some()
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.endpoint(handle_group_created),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
// 3) Commands etc.:
|
|
|
|
.branch(
|
|
|
|
.branch(
|
|
|
|
Update::filter_message()
|
|
|
|
Update::filter_message()
|
|
|
|
// parse the text into Command
|
|
|
|
|
|
|
|
.filter_command::<Command>()
|
|
|
|
.filter_command::<Command>()
|
|
|
|
.endpoint(handle_commands),
|
|
|
|
.endpoint(handle_commands),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
// 4) User leaves group/chat
|
|
|
|
|
|
|
|
.branch(
|
|
|
|
|
|
|
|
Update::filter_message()
|
|
|
|
|
|
|
|
.filter(|msg: Message| msg.left_chat_member().is_some())
|
|
|
|
|
|
|
|
.endpoint(handle_left_chat_member),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 6) Launch the dispatcher
|
|
|
|
// 6) Launch the dispatcher
|
|
|
@ -121,6 +138,45 @@ async fn handle_new_chat_members(bot: Bot, msg: Message) -> Result<(), teloxide:
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Detect if a new group is created with the bot as a founding member, say the group ID, then leave.
|
|
|
|
|
|
|
|
async fn handle_group_created(bot: Bot, msg: Message) -> Result<(), teloxide::RequestError> {
|
|
|
|
|
|
|
|
// Same logic as if the bot was added:
|
|
|
|
|
|
|
|
let chat_id = msg.chat.id;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Post the group’s chat ID
|
|
|
|
|
|
|
|
bot.send_message(chat_id, format!("Group Chat ID is: {chat_id}")).await?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Optionally log the event
|
|
|
|
|
|
|
|
log::info!(
|
|
|
|
|
|
|
|
"Bot was in a newly created group (chatID: {}). Posting ID and leaving...",
|
|
|
|
|
|
|
|
chat_id
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Leave the group
|
|
|
|
|
|
|
|
bot.leave_chat(chat_id).await?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Handle "left chat member" events so they are not generating WARN statements. These events will happen almost every time the bot is used.
|
|
|
|
|
|
|
|
async fn handle_left_chat_member(
|
|
|
|
|
|
|
|
msg: Message,
|
|
|
|
|
|
|
|
) -> Result<(), teloxide::RequestError> {
|
|
|
|
|
|
|
|
// Telegram sets this field if a user (or the bot) left.
|
|
|
|
|
|
|
|
if let Some(left_user) = msg.left_chat_member() {
|
|
|
|
|
|
|
|
log::info!(
|
|
|
|
|
|
|
|
"User/bot @{} (ID {}) left the chat {}",
|
|
|
|
|
|
|
|
left_user.username.clone().unwrap_or_default(),
|
|
|
|
|
|
|
|
left_user.id,
|
|
|
|
|
|
|
|
msg.chat.id
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Optionally do something else, like sending a "goodbye" message?
|
|
|
|
|
|
|
|
// For now, we'll just swallow this event so it won't be "unhandled".
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Handle commands in private chat
|
|
|
|
/// Handle commands in private chat
|
|
|
|
async fn handle_commands(
|
|
|
|
async fn handle_commands(
|
|
|
|
bot: Bot,
|
|
|
|
bot: Bot,
|
|
|
|