simple/
simple.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 main() {
12    tokio::runtime::current_thread::Runtime::new()
13        .unwrap()
14        .block_on(lazy(|| {
15            let token = env::var("TELEGRAM_BOT_TOKEN").unwrap();
16            let api = Api::new(token).unwrap();
17
18            let stream = api.stream().then(|mb_update| {
19                let res: Result<Result<Update, Error>, ()> = Ok(mb_update);
20                res
21            });
22
23            // Print update or error for each update.
24            stream.for_each(move |update| {
25                match update {
26                    Ok(update) => {
27                        // If the received update contains a new message...
28                        if let UpdateKind::Message(message) = update.kind {
29                            if let MessageKind::Text { ref data, .. } = message.kind {
30                                // Print received text message to stdout.
31                                println!("<{}>: {}", &message.from.first_name, data);
32
33                                // Answer message with "Hi".
34                                api.spawn(message.text_reply(format!(
35                                    "Hi, {}! You just wrote '{}'",
36                                    &message.from.first_name, data
37                                )));
38                            }
39                        }
40                    }
41                    Err(_) => {}
42                }
43
44                Ok(())
45            })
46        }))
47        .unwrap();
48}