Skip to main content

pin/
pin.rs

1extern crate futures;
2extern crate telegram_bot_fork;
3extern crate tokio;
4
5use std::env;
6
7use futures::{future::lazy, Stream};
8
9use telegram_bot_fork::*;
10
11fn process(api: Api, message: Message) {
12    if let MessageKind::Text { ref data, .. } = message.kind {
13        match data.as_str() {
14            "/pin" => message
15                .reply_to_message
16                .map(|message| api.spawn(message.pin()))
17                .unwrap_or(()),
18            "/unpin" => api.spawn(message.chat.unpin_message()),
19            _ => (),
20        }
21    }
22}
23
24fn main() {
25    tokio::runtime::current_thread::Runtime::new()
26        .unwrap()
27        .block_on(lazy(|| {
28            let token = env::var("TELEGRAM_BOT_TOKEN").unwrap();
29            let api = Api::new(token).unwrap();
30
31            let stream = api.stream().then(|mb_update| {
32                let res: Result<Result<Update, Error>, ()> = Ok(mb_update);
33                res
34            });
35
36            stream.for_each(move |update| {
37                match update {
38                    Ok(update) => {
39                        if let UpdateKind::Message(message) = update.kind {
40                            process(api.clone(), message)
41                        }
42                    }
43                    Err(_) => {}
44                }
45
46                Ok(())
47            })
48        }))
49        .unwrap();
50}