Advertisement
ZaMaZaN4iK

Bot

Sep 5th, 2020
2,314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 5.02 KB | None | 0 0
  1. mod detection;
  2. mod commands;
  3.  
  4. use teloxide::{prelude::*, utils::command::BotCommand};
  5.  
  6. use std::collections::HashMap;
  7. use std::sync::{Arc, Mutex};
  8.  
  9. #[tokio::main]
  10. async fn main() {
  11.     run().await;
  12. }
  13.  
  14. async fn run() {
  15.     teloxide::enable_logging!();
  16.     log::info!("Starting CodeDetector bot!");
  17.  
  18.     let bot = Bot::from_env();
  19.  
  20.     let bot_responses_to_messages = Arc::new(Mutex::new(HashMap::<i32, i32>::new()));
  21.     let bot_responses_for_deletion = bot_responses_to_messages.clone();
  22.  
  23.     Dispatcher::new(bot)
  24.         .messages_handler(|rx: DispatcherHandlerRx<Message>| {
  25.             rx.for_each(move |message| {
  26.                 let response_map = bot_responses_to_messages.clone();
  27.  
  28.                 async move {
  29.                     let message_text = match message.update.text() {
  30.                         Some(x) => x,
  31.                         None => return,
  32.                     };
  33.  
  34.                     // Handle commands
  35.                     match commands::Command::parse(message_text, "CodeDetectorBot") {
  36.                         Ok(command) => {
  37.                             commands::command_answer(&message, command).await.log_on_error().await;
  38.                             return;
  39.                         }
  40.                         Err(_) => (),
  41.                     };
  42.  
  43.                     // Handle code formatting
  44.                     if detection::maybe_formatted(message.update.entities()) {
  45.                         return;
  46.                     }
  47.  
  48.                     if detection::is_code_detected(message_text) {
  49.                         static FORMAT_TEXT: &str = "Оберните код в теги: 3 символа ` до и после кода \
  50.                        (в случае одиночной конструкции достаточно 1 ` с обеих сторон). Спасибо!";
  51.  
  52.                         let result = message
  53.                             .reply_to(FORMAT_TEXT)
  54.                             .send()
  55.                             .await;
  56.  
  57.                         match result {
  58.                             Ok(resp) =>
  59.                                 {
  60.                                     println!("Incoming message id: {}", message.update.id);
  61.                                     println!("Bot response id: {}", resp.id);
  62.                                     response_map
  63.                                         .lock()
  64.                                         .unwrap()
  65.                                         .insert(message.update.id, resp.id);
  66.                                 }
  67.                             Err(_) => { result.log_on_error().await; }
  68.                         };
  69.                     }
  70.                 }
  71.             })
  72.         })
  73.         .edited_messages_handler(|rx: DispatcherHandlerRx<Message>| {
  74.             rx.for_each(move |message| {
  75.                 let response_map = bot_responses_for_deletion.clone();
  76.                 async move {
  77.                     let message_text = match message.update.text() {
  78.                         Some(x) => x,
  79.                         None => return,
  80.                     };
  81.  
  82.                     println!("Edit handle triggered!");
  83.                     // Handle code formatting
  84.                     if detection::maybe_formatted(message.update.entities()) {
  85.                         let maybe_bot_answer_id =
  86.                             response_map
  87.                             .lock()
  88.                             .unwrap();
  89.  
  90.                         let maybe_bot_answer_id = maybe_bot_answer_id.get(&message.update.id);
  91.  
  92.                         match maybe_bot_answer_id {
  93.                             Some(value) => {
  94.                                 let dereferenced_val = *value;
  95.                                 println!("Edited message id: {}", message.update.id);
  96.                                 println!("Bot response id: {}", *value);
  97.                                 message.bot
  98.                                     .delete_message(message.chat_id(), dereferenced_val)
  99.                                     .send()
  100.                                     .await
  101.                                     .log_on_error()
  102.                                     .await;
  103.                                 return;
  104.                             }
  105.                             None => { return }
  106.                         }
  107.                     }
  108.  
  109.                     if detection::is_code_detected(message_text) {
  110.                         static FORMAT_TEXT: &str = "Оберните код в теги: 3 символа ` до и после кода \
  111.                        (в случае одиночной конструкции достаточно 1 ` с обеих сторон). Спасибо!";
  112.  
  113.                         message
  114.                             .reply_to(FORMAT_TEXT)
  115.                             .send()
  116.                             .await
  117.                             .log_on_error()
  118.                             .await;
  119.                     }
  120.                 }
  121.             })
  122.         })
  123.         .dispatch()
  124.         .await;
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement