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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! [Telegram bot](https://core.telegram.org/bots/api) service able to receive and send messages.

use crate::consts::USER_AGENT;
use crate::db::Db;
use crate::message::{Message, Reading, Type};
use crate::services::Service;
use crate::value::Value;
use crate::{threading, Result};
use bus::Bus;
use chrono::{DateTime, Utc};
use crossbeam_channel::Sender;
use failure::format_err;
use log::debug;
use reqwest::header::{HeaderMap, HeaderValue};
use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

const TIMEOUT: u32 = 60;

#[derive(Deserialize, Debug, Clone)]
pub struct Settings {
    token: String,
}

/// Telegram bot service.
pub struct Telegram {
    service_id: String,
    client: reqwest::Client,

    /// <https://core.telegram.org/bots/api#making-requests>
    url_prefix: String,
}

/// <https://core.telegram.org/bots/api#getupdates>
#[derive(Serialize)]
struct UpdateParameters {
    offset: Option<i64>,
    limit: Option<u32>,
    timeout: Option<u32>,
    allowed_updates: Option<Vec<String>>,
}

impl Default for UpdateParameters {
    fn default() -> UpdateParameters {
        UpdateParameters {
            offset: None,
            limit: None,
            timeout: Some(TIMEOUT),
            allowed_updates: Some(vec!["message".into()]),
        }
    }
}

/// <https://core.telegram.org/bots/api#making-requests>
#[derive(Deserialize, Debug)]
struct TelegramResponse<T> {
    ok: bool,
    description: Option<String>,
    result: Option<T>,
}

#[derive(Deserialize, Debug)]
struct TelegramUpdate {
    update_id: i64,
    message: Option<TelegramMessage>,
}

/// <https://core.telegram.org/bots/api#message>
#[derive(Deserialize, Debug)]
struct TelegramMessage {
    message_id: i64,

    #[serde(deserialize_with = "chrono::serde::ts_seconds::deserialize")]
    date: DateTime<Utc>,

    chat: TelegramChat,
    text: Option<String>,
}

/// <https://core.telegram.org/bots/api#chat>
#[derive(Deserialize, Debug)]
struct TelegramChat {
    id: i64,
}

impl Service for Telegram {
    fn spawn(self: Box<Self>, _db: Arc<Mutex<Db>>, tx: &Sender<Message>, _rx: &mut Bus<Message>) -> Result<()> {
        let tx = tx.clone();

        threading::spawn(format!("my-iot::telegram:{}", &self.service_id), move || {
            let mut offset: Option<i64> = None;
            loop {
                match self.get_updates(offset) {
                    Ok(ref updates) => {
                        for update in updates.iter() {
                            offset = offset.max(Some(update.update_id + 1));
                            self.send(&update, &tx).unwrap();
                        }
                        debug!("{}: next offset: {:?}", &self.service_id, offset);
                    }
                    Err(error) => {
                        log::error!("Telegram has failed: {}", error);
                        thread::sleep(Duration::from_millis(60000)); // FIXME: something smarter.
                    }
                }
            }
        })?;

        Ok(())
    }
}

impl Telegram {
    pub fn new(service_id: &str, settings: &Settings) -> Result<Telegram> {
        let mut headers = HeaderMap::new();
        headers.insert(reqwest::header::USER_AGENT, HeaderValue::from_static(USER_AGENT));
        Ok(Telegram {
            service_id: service_id.into(),
            url_prefix: format!("https://api.telegram.org/bot{}/", &settings.token),
            client: reqwest::Client::builder()
                .gzip(true)
                .timeout(Duration::from_secs((TIMEOUT + 1).into()))
                .default_headers(headers)
                .build()?,
        })
    }

    /// <https://core.telegram.org/bots/api#getupdates>
    fn get_updates(&self, offset: Option<i64>) -> Result<Vec<TelegramUpdate>> {
        debug!("{}: get updates with offset {:?}", &self.service_id, offset);
        // TODO: make generic `call` method.
        self.client
            .get(&format!("{}{}", self.url_prefix, "getUpdates"))
            .json(&UpdateParameters {
                offset,
                ..Default::default()
            })
            .send()?
            .json::<TelegramResponse<Vec<TelegramUpdate>>>()
            .map_err(Into::into)
            .and_then(|response| {
                if response.ok {
                    Ok(response.result.unwrap())
                } else {
                    Err(format_err!("{}", response.description.unwrap()))
                }
            })
    }

    fn send(&self, update: &TelegramUpdate, tx: &Sender<Message>) -> Result<()> {
        debug!("{}: {:?}", &self.service_id, &update);

        if let Some(ref message) = update.message {
            if let Some(ref text) = message.text {
                tx.send(Message {
                    type_: Type::OneOff,
                    reading: Reading {
                        sensor: format!("{}::{}::message", self.service_id, message.chat.id),
                        value: Value::Text(text.into()),
                        timestamp: message.date.into(),
                    },
                })?;
            }
        }

        Ok(())
    }
}