Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mod detection;
- mod commands;
- use teloxide::{prelude::*, utils::command::BotCommand};
- use std::collections::HashMap;
- use std::sync::{Arc, Mutex};
- #[tokio::main]
- async fn main() {
- run().await;
- }
- async fn run() {
- teloxide::enable_logging!();
- log::info!("Starting CodeDetector bot!");
- let bot = Bot::from_env();
- let bot_responses_to_messages = Arc::new(Mutex::new(HashMap::<i32, i32>::new()));
- let bot_responses_for_deletion = bot_responses_to_messages.clone();
- Dispatcher::new(bot)
- .messages_handler(|rx: DispatcherHandlerRx<Message>| {
- rx.for_each(move |message| {
- let response_map = bot_responses_to_messages.clone();
- async move {
- let message_text = match message.update.text() {
- Some(x) => x,
- None => return,
- };
- // Handle commands
- match commands::Command::parse(message_text, "CodeDetectorBot") {
- Ok(command) => {
- commands::command_answer(&message, command).await.log_on_error().await;
- return;
- }
- Err(_) => (),
- };
- // Handle code formatting
- if detection::maybe_formatted(message.update.entities()) {
- return;
- }
- if detection::is_code_detected(message_text) {
- static FORMAT_TEXT: &str = "Оберните код в теги: 3 символа ` до и после кода \
- (в случае одиночной конструкции достаточно 1 ` с обеих сторон). Спасибо!";
- let result = message
- .reply_to(FORMAT_TEXT)
- .send()
- .await;
- match result {
- Ok(resp) =>
- {
- println!("Incoming message id: {}", message.update.id);
- println!("Bot response id: {}", resp.id);
- response_map
- .lock()
- .unwrap()
- .insert(message.update.id, resp.id);
- }
- Err(_) => { result.log_on_error().await; }
- };
- }
- }
- })
- })
- .edited_messages_handler(|rx: DispatcherHandlerRx<Message>| {
- rx.for_each(move |message| {
- let response_map = bot_responses_for_deletion.clone();
- async move {
- let message_text = match message.update.text() {
- Some(x) => x,
- None => return,
- };
- println!("Edit handle triggered!");
- // Handle code formatting
- if detection::maybe_formatted(message.update.entities()) {
- let maybe_bot_answer_id =
- response_map
- .lock()
- .unwrap();
- let maybe_bot_answer_id = maybe_bot_answer_id.get(&message.update.id);
- match maybe_bot_answer_id {
- Some(value) => {
- let dereferenced_val = *value;
- println!("Edited message id: {}", message.update.id);
- println!("Bot response id: {}", *value);
- message.bot
- .delete_message(message.chat_id(), dereferenced_val)
- .send()
- .await
- .log_on_error()
- .await;
- return;
- }
- None => { return }
- }
- }
- if detection::is_code_detected(message_text) {
- static FORMAT_TEXT: &str = "Оберните код в теги: 3 символа ` до и после кода \
- (в случае одиночной конструкции достаточно 1 ` с обеих сторон). Спасибо!";
- message
- .reply_to(FORMAT_TEXT)
- .send()
- .await
- .log_on_error()
- .await;
- }
- }
- })
- })
- .dispatch()
- .await;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement