1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::collections::HashMap;

use futures::stream::Stream;
use telegram_bot::types::{MessageKind, Update, UpdateKind, Message};
use telegram_bot::Api;
use tokio_core::reactor::Core;

pub struct BotWrapper {
    api: Api,
    core: Core,
    commands: HashMap<String, Box<dyn Fn(&Api, &Message) -> ()>>,
}

impl BotWrapper {
    fn handle_update(api: &Api, commands: &HashMap<String, Box<dyn Fn(&Api, &Message) -> ()>>, update: Update) {
        if let UpdateKind::Message(ref msg) = update.kind {
            if let MessageKind::Text { ref data, entities: _ } = msg.kind {
                let data = data.clone().split_off(1);
                let data = data.split_whitespace().next().unwrap_or("");
                commands.get(data).map(|command| command(api, msg));
            }
        }
    }

    pub fn new(token: String) -> BotWrapper {
        let core = Core::new().expect("Failed to execute tokio core");

        let api = Api::configure(token.clone())
            .build(core.handle())
            .expect("Failed to spawn bot threads");
        BotWrapper {
            api,
            core,
            commands: HashMap::new(),
        }
    }

    pub fn run(self) {
        let BotWrapper {
            api,
            mut core,
            commands,
        } = self;
        let update_stream = api
            .stream()
            .for_each(|update| Ok(BotWrapper::handle_update(&api, &commands, update)));

        core.run(update_stream).expect("Failed to run core reactor");
    }

    pub fn command<F>(&mut self, command: String, handle: F)
    where
        F: 'static + Fn(&Api, &Message) -> (),
    {
        self.commands.insert(command, Box::new(handle));
    }
}