Skip to main content

live_location/
live_location.rs

1extern crate futures;
2extern crate telegram_bot_fork;
3extern crate tokio;
4extern crate tokio_timer;
5
6use std::{
7    env,
8    ops::Add,
9    time::{Duration, Instant},
10};
11
12use futures::{future::lazy, Future, Stream};
13
14use tokio_timer::Delay;
15
16use telegram_bot_fork::*;
17
18fn test(api: Api, message: Message) {
19    let timeout = |n| Delay::new(Instant::now().add(Duration::from_secs(n))).from_err();
20    let api_future = || Ok(api.clone());
21
22    let future = api
23        .send(message.location_reply(0.0, 0.0).live_period(60))
24        .join(api_future())
25        .join(timeout(2))
26        .and_then(|((message, api), _)| api.send(message.edit_live_location(10.0, 10.0)))
27        .join(api_future())
28        .join(timeout(4))
29        .and_then(|((message, api), _)| api.send(message.edit_live_location(20.0, 20.0)))
30        .join(api_future())
31        .join(timeout(6))
32        .and_then(|((message, api), _)| api.send(message.edit_live_location(30.0, 30.0)));
33
34    tokio::executor::current_thread::spawn(future.then(|_| Ok(())));
35}
36
37fn main() {
38    tokio::runtime::current_thread::Runtime::new()
39        .unwrap()
40        .block_on(lazy(|| {
41            let token = env::var("TELEGRAM_BOT_TOKEN").unwrap();
42            let api = Api::new(token).unwrap();
43
44            let stream = api.stream().then(|mb_update| {
45                let res: Result<Result<Update, Error>, ()> = Ok(mb_update);
46                res
47            });
48
49            stream.for_each(move |update| {
50                match update {
51                    Ok(update) => {
52                        if let UpdateKind::Message(message) = update.kind {
53                            if let MessageKind::Text { ref data, .. } = message.kind {
54                                match data.as_str() {
55                                    "/livelocation" => test(api.clone(), message.clone()),
56                                    _ => (),
57                                }
58                            }
59                        }
60                    }
61                    Err(_) => {}
62                }
63
64                Ok(())
65            })
66        }))
67        .unwrap();
68}