Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use crate::config::Config as AppConfig;
- use crate::services::provider::controller::ProviderController;
- use carapax::prelude::{
- Api, App, Context, Handler, HandlerFuture, HandlerResult, Message, SendMessage, Method, MessageData,
- };
- use futures::{
- future::{self, Either},
- Future,
- };
- use failure::Error;
- use std::sync::Arc;
- #[derive(Clone)]
- pub struct ProviderTelegram {
- pub cnfg: Arc<AppConfig>,
- pub provider_cnr: Arc<ProviderController>,
- }
- pub fn init(
- cnfg: &Arc<AppConfig>,
- provider_cnr: &Arc<ProviderController>,
- telegram_app: App,
- ) -> App {
- let provider = ProviderTelegram {
- cnfg: cnfg.clone(),
- provider_cnr: provider_cnr.clone(),
- };
- telegram_app.add_handler(provider)
- }
- impl Handler for ProviderTelegram {
- type Input = Message;
- type Output = HandlerFuture;
- fn handle(&self, ctx: &mut Context, msg: Self::Input) -> Self::Output {
- let api = ctx.get::<Api>().clone();
- let cnt = move |_| HandlerResult::Continue;
- match msg.data {
- MessageData::Text(ref text) => {
- let command = text.data.clone();
- let method = self.handle_text_message(&api, msg, command);
- HandlerFuture::new(method.and_then(move |i| api.execute(i)).map(cnt))
- },
- _ => {
- let method = self.handle_unsupported_message(msg);
- HandlerFuture::new(method.and_then(move |i| api.execute(i)).map(cnt))
- }
- }
- }
- }
- impl ProviderTelegram {
- pub fn handle_text_message(&self, api: &Api, msg: Message, command: String) -> impl Future<Item=impl Method<Response=Message>, Error=Error> {
- futures::lazy(move || match command.as_ref() {
- "/hello" => {
- let chat_id = msg.get_chat_id();
- Ok(SendMessage::new(chat_id, "Hello there".to_string()))
- }
- _ => {
- let chat_id = msg.get_chat_id();
- Ok(SendMessage::new(chat_id, command.clone()))
- }
- })
- }
- pub fn handle_unsupported_message(&self, msg: Message) -> impl Future<Item=impl Method<Response=Message>, Error=Error> {
- futures::lazy(move || {
- let chat_id = msg.get_chat_id();
- Ok(SendMessage::new(chat_id, "Unsupported message".to_string()))
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement