-
I managed to make a welcome message for my bot but I had two problems that I have not been able to solve.
I leave the code below (it's a bit messy but I'll polish it when I can solve the problems): use crate::commands::*;
pub mod database;
pub mod commands;
pub mod utils;
pub mod buttons;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Initialize the logging environment for Teloxide
pretty_env_logger::init();
log::info!("Iniciando Bot...");
// Load environment variables from a .env file
dotenv().ok();
// Connect to the database
//conectar().await.expect("Error al conectar con la Base de Datos");
// Load the init message from a txt file
let init_message = include_str!("init_message.txt");
// Print a message to the console to indicate that the bot has started
println!("{init_message}\n");
let bot = teloxide::Bot::from_env().parse_mode(MarkdownV2);
let handler = dptree::entry().inspect(|u:Update| {
println!("{u:#?}");
})
.branch(Update::filter_message().endpoint(message_handler))
.branch(Update::filter_callback_query().endpoint(callback_handler))
.branch(Update::filter_message().branch(Message::filter_new_chat_members().endpoint(chat_member_handler)))
//.branch(Update::filter_chat_member().endpoint(chat_member_handler))
.branch(Update::filter_inline_query().endpoint(inline_query_handler));
Dispatcher::builder(bot.clone(), handler)
.enable_ctrlc_handler()
.build()
.dispatch()
.await;
Ok(())
}
async fn chat_member_handler(bot:Bot, chat_member:ChatMemberUpdated, test_member: Update) -> Result<(), Box<dyn Error + Send + Sync>> {
let telegram_group_name = chat_member.chat.title().unwrap_or("");
println!("{:#?}", test_member);
let first_name = chat_member.clone().from.first_name;
let username = if let Some(name) = chat_member.from.username {
name
} else if first_name == chat_member.from.first_name {
first_name.clone()
} else {
return Err("Error: Username or first name not found".into());
};
let member = bot.get_chat_member(chat_member.chat.id, chat_member.new_chat_member.user.id).await?;
let member_status = member.status();
if let ChatMemberStatus::Left = member_status {
if username == first_name {
bot.send_message(chat_member.chat.id, format!("Hasta pronto {username}!"))
.parse_mode(ParseMode::Html)
.await?;
} else {
bot.send_message(chat_member.chat.id, format!("Hasta pronto @{username}!"))
.parse_mode(ParseMode::Html)
.await?;
}
} else if username == first_name {
bot.send_message(chat_member.chat.id, format!("Bienvenido a {telegram_group_name} {username}!"))
.parse_mode(ParseMode::Html)
.await?;
} else {
bot.send_message(chat_member.chat.id, format!("Bienvenido a {telegram_group_name} @{username}!"))
.parse_mode(ParseMode::Html)
.await?;
}
Ok(())
} Is there any clue or advice you can give me please. Thank you very much in advance. PD: I will leave the link to my bot repository just in case https://github.com/CrawKatt/TeloxBeta/tree/experimental |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Re 1.: you can use something like Re 2.: You can do something like Hope that helps. |
Beta Was this translation helpful? Give feedback.
I found the solution
regarding the problem with the user mention. I tried with the unwrap_or solution but I had an error about expecting an i64 but I found a UserId, which did not allow me to compile. After searching I ended up finding a solution from StackOverflow where they used a Telegram link and that's when it occurred to me to use the user_id inside a format with the tag.
regarding the welcome message bug, the .filter() method solution worked. The problem I had was that I was applying the method backwards, which caused the method to have no effect. Thank you very much for the advice, it was very useful and I learned a lot of new things.
I leave the code of my solution for thos…