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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! This is the actual Bot module. For ergonomic reasons there is a RcBot which uses the real bot
//! as an underlying field. You should always use RcBot. 

use objects;
use error::Error;

use std::str;
use std::io;
use std::time::Duration;
use std::collections::HashMap;
use std::rc::Rc;
use std::cell::{RefCell, Cell};
use std::sync::{Arc, Mutex};

use curl::easy::{Easy, List,Form,InfoType};
use tokio_curl::Session;
use tokio_core::reactor::{Handle, Core, Interval};
use serde_json;
use serde_json::value::Value;
use futures::{Future, IntoFuture, Stream, stream};
use futures::sync::mpsc;
use futures::sync::mpsc::UnboundedSender;

/// A clonable, single threaded bot
///
/// The outer API gets implemented on RcBot
#[derive(Clone)]
pub struct RcBot {
    pub inner: Rc<Bot>
}

impl RcBot {
    pub fn new(handle: Handle, key: &str) -> RcBot {
        RcBot { inner: Rc::new(Bot::new(handle, key)) }
    }
}

/// The main bot structure
pub struct Bot {
    pub key: String,
    pub handle: Handle,
    pub last_id: Cell<u32>,
    pub update_interval: Cell<u64>,
    pub handlers: RefCell<HashMap<String, UnboundedSender<(RcBot, objects::Message)>>>,
    pub session: Session
}

impl Bot {
    pub fn new(handle: Handle, key: &str) -> Bot {
        debug!("Create a new bot with the key {}", key);
        
        Bot { 
            handle: handle.clone(), 
            key: key.into(), 
            last_id: Cell::new(0), 
            update_interval: Cell::new(1000), 
            handlers: RefCell::new(HashMap::new()), 
            session: Session::new(handle.clone()) 
        }
    }

    /// Creates a new request and adds a JSON message to it. The returned Future contains a the
    /// reply as a string.  This method should be used if no file is added because a JSON msg is
    /// always compacter than a formdata one.
    pub fn fetch_json<'a>(&self, func: &str, msg: &str) -> impl Future<Item=String, Error=Error> + 'a{
        debug!("Send JSON: {}", msg);
        
        let mut header = List::new();
        header.append("Content-Type: application/json").unwrap();
        
        let mut a = Easy::new();
        a.http_headers(header).unwrap();
        a.post_fields_copy(msg.as_bytes()).unwrap();
        a.post(true).unwrap();

        self._fetch(func, a)
    }

    /// Creates a new request with some byte content (e.g. a file). The method properties have to be 
    /// in the formdata setup and cannot be sent as JSON.
    pub fn fetch_formdata<'a, T>(&self, func: &str, msg: Value, mut file: T, kind: &str, file_name: &str) -> impl Future<Item=String, Error=Error> + 'a where T: io::Read {
        debug!("Send formdata: {}", msg.to_string());
        let mut content = Vec::new();

        let mut a = Easy::new();
        let mut form = Form::new();
        
        // try to read the byte content to a vector
        let _ = file.read_to_end(&mut content).unwrap();

        // add properties
        for (key, val) in msg.as_object().unwrap().iter() {
            form.part(key).contents(format!("{:?}",val).as_bytes()).add().unwrap();
        }
        
        // add the file
        form.part(kind).buffer(file_name, content).content_type("application/octet-stream").add().unwrap();

        // create a http post request
        a.post(true).unwrap();
        a.httppost(form).unwrap();

        self._fetch(func, a)
    }

    /// calls cURL and parses the result for an error
    pub fn _fetch<'a>(&self, func: &str, mut a: Easy) -> impl Future<Item=String, Error=Error> + 'a {
        let result = Arc::new(Mutex::new(Vec::new()));

        a.url(&format!("https://api.telegram.org/bot{}/{}", self.key, func)).unwrap();
        
        let r2 = result.clone();
        a.write_function(move |data| {
            r2.lock().unwrap().extend_from_slice(data);
            Ok(data.len())
        }).unwrap();

        // print debug information
        a.debug_function(|info, data| {
            match info {
                InfoType::DataOut => {
                    println!("DataOut");
                },
                InfoType::Text => {
                    println!("Text");
                },
                InfoType::HeaderOut => {
                    println!("HeaderOut");
                },
                InfoType::SslDataOut => {
                    println!("SslDataOut");
                }
                _ => println!("something else")
            }

            println!("{:?}", String::from_utf8_lossy(data));
        }).unwrap();
        //a.verbose(true).unwrap();
        //a.show_header(true).unwrap();

        self.session.perform(a)
        .map_err(|x| { Error::TokioCurl(x) })
        .map(move |_| {
            let response = result.lock().unwrap();
            String::from(str::from_utf8(&response).unwrap())
        }).and_then(move |x| {
            debug!("Got a result from telegram: {}", x);
            // try to parse the result as a JSON and find the OK field.
            // If the ok field is true, then the string in "result" will be returned
            if let Ok(req) = serde_json::from_str::<Value>(&x) {
                if let (Some(ok), res) = (req.get("ok").and_then(Value::as_bool), req.get("result")) {
                    if ok {
                        if let Some(result) = res {
                            let answer = serde_json::to_string(result).unwrap();

                            return Ok(answer);
                        }
                    }
                    
                    match req.get("description").and_then(Value::as_str) {
                        Some(err) => Err(Error::Telegram(err.into())),
                        None => Err(Error::Telegram("Unknown".into()))
                    }
                } else {
                    return Err(Error::JSON);
                }
            } else {
                return Err(Error::JSON);
            }
        })
    }
}

impl RcBot {
    /// Sets the update interval to an integer in milliseconds
    pub fn update_interval(self, interval: u64) -> RcBot {
        self.inner.update_interval.set(interval);

        self
    }
   
    /// Creates a new command and returns a stream which will yield a message when the command is send
    pub fn new_cmd(&self, cmd: &str) -> impl Stream<Item=(RcBot,  objects::Message), Error=Error> {
        let (sender, receiver) = mpsc::unbounded();
    
        self.inner.handlers.borrow_mut().insert(cmd.into(), sender);

        receiver.map_err(|_| Error::Unknown)
    }

    /// Register a new commnd
    pub fn register<T>(&self, hnd: T) where T: Stream + 'static {
        self.inner.handle.spawn(hnd.for_each(|_| Ok(())).into_future().map(|_| ()).map_err(|_| ()));
    }
   
    /// The main update loop, the update function is called every update_interval milliseconds
    /// When an update is available the last_id will be updated and the message is filtered
    /// for commands
    /// The message is forwarded to the returned stream if no command was found
    pub fn get_stream<'a>(&'a self) -> impl Stream<Item=(RcBot, objects::Update), Error=Error> + 'a{
        use functions::*;

        Interval::new(Duration::from_millis(self.inner.update_interval.get()), &self.inner.handle).unwrap()
            .map_err(|_| Error::Unknown)
            .and_then(move |_| self.get_updates().offset(self.inner.last_id.get()).send())
            .map(|(_, x)| stream::iter(x.0.into_iter().map(|x| Ok(x)).collect::<Vec<Result<objects::Update, Error>>>()))
            .flatten()
            .and_then(move |x| {
                if self.inner.last_id.get() < x.update_id as u32 +1 {
                    self.inner.last_id.set(x.update_id as u32 +1);
                }
        
                Ok(x) 
            })
        .filter_map(move |mut val| {
            debug!("Got an update from Telegram: {:?}", val);
            let mut forward: Option<String> = None;

            if let Some(ref mut message) = val.message {
                if let Some(text) = message.text.clone() {
                    let mut content = text.split_whitespace();
                    if let Some(cmd) = content.next() {
                        if self.inner.handlers.borrow_mut().contains_key(cmd) {
                            message.text = Some(content.collect::<Vec<&str>>().join(" "));

                            forward = Some(cmd.into());
                        }
                    }
                }
            }
           
            if let Some(cmd) = forward {
                if let Some(sender) = self.inner.handlers.borrow_mut().get_mut(&cmd) {
                    sender.send((self.clone(), val.message.unwrap())).unwrap();
                }
                return None;
            } else {
                return Some((self.clone(), val));
            }
        })
    }
   
    /// helper function to start the event loop
    pub fn run<'a>(&'a self, core: &mut Core) -> Result<(), Error> {
        core.run(self.get_stream().for_each(|_| Ok(())).into_future())      
    }
}