Advertisement
Guest User

Untitled

a guest
Jul 13th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.37 KB | None | 0 0
  1. use crate::config::Config as AppConfig;
  2. use crate::services::provider::controller::ProviderController;
  3.  
  4. use carapax::prelude::{
  5.     Api, App, Context, Handler, HandlerFuture, HandlerResult, Message, SendMessage, Method, MessageData,
  6. };
  7. use futures::{
  8.     future::{self, Either},
  9.     Future,
  10. };
  11. use failure::Error;
  12. use std::sync::Arc;
  13.  
  14. #[derive(Clone)]
  15. pub struct ProviderTelegram {
  16.     pub cnfg: Arc<AppConfig>,
  17.     pub provider_cnr: Arc<ProviderController>,
  18. }
  19.  
  20. pub fn init(
  21.     cnfg: &Arc<AppConfig>,
  22.     provider_cnr: &Arc<ProviderController>,
  23.     telegram_app: App,
  24. ) -> App {
  25.     let provider = ProviderTelegram {
  26.         cnfg: cnfg.clone(),
  27.         provider_cnr: provider_cnr.clone(),
  28.     };
  29.  
  30.     telegram_app.add_handler(provider)
  31. }
  32.  
  33. impl Handler for ProviderTelegram {
  34.     type Input = Message;
  35.     type Output = HandlerFuture;
  36.  
  37.     fn handle(&self, ctx: &mut Context, msg: Self::Input) -> Self::Output {
  38.         let api = ctx.get::<Api>().clone();
  39.         let cnt = move |_| HandlerResult::Continue;
  40.         match msg.data {
  41.             MessageData::Text(ref text) => {
  42.                 let command = text.data.clone();
  43.                 let method = self.handle_text_message(&api, msg, command);
  44.                 HandlerFuture::new(method.and_then(move |i| api.execute(i)).map(cnt))
  45.             },
  46.             _ => {
  47.                 let method = self.handle_unsupported_message(msg);
  48.                 HandlerFuture::new(method.and_then(move |i| api.execute(i)).map(cnt))
  49.             }
  50.         }
  51.     }
  52. }
  53.  
  54. impl ProviderTelegram {
  55.     pub fn handle_text_message(&self, api: &Api, msg: Message, command: String) -> impl Future<Item=impl Method<Response=Message>, Error=Error> {
  56.         futures::lazy(move || match command.as_ref() {
  57.             "/hello" => {
  58.                 let chat_id = msg.get_chat_id();
  59.                 Ok(SendMessage::new(chat_id, "Hello there".to_string()))
  60.             }
  61.             _ => {
  62.                 let chat_id = msg.get_chat_id();
  63.                 Ok(SendMessage::new(chat_id, command.clone()))
  64.             }
  65.         })
  66.     }
  67.  
  68.     pub fn handle_unsupported_message(&self, msg: Message) -> impl Future<Item=impl Method<Response=Message>, Error=Error> {
  69.         futures::lazy(move || {
  70.             let chat_id = msg.get_chat_id();
  71.             Ok(SendMessage::new(chat_id, "Unsupported message".to_string()))
  72.         })
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement