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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Available telegram functions, copied from https://core.telegram.org/bots/api#available-methods
//!
//! telebot-derive implements setter, setter and send methods to each struct

use bot::{Bot, RcBot};
use serde_json;
use objects;
use objects::{Integer, NotImplemented};
use error::Error;
use file;
use futures::Future;
use std::rc::Rc;
use futures::Poll;
use erased_serde::Serialize;

pub trait TelegramSendable {
    type Item;

    fn send(self) -> Box<Future<Item=Self::Item, Error=Error>>;
}

/// The strongly typed version of the parse_mode field which indicates the type of text
pub enum ParseMode {
    Markdown,
    HTML,
    Text
}

impl Into<String> for ParseMode {
    fn into(self) -> String {
        let tmp = match self {
            ParseMode::Markdown => "Markdown",
            ParseMode::HTML => "HTML",
            ParseMode::Text => "Text"
        };

        tmp.into()
    }
}

/// The strongly typed version of the action field which indicates the type of action
pub enum Action {
    Typing,
    UploadPhoto,
    RecordVideo,
    UploadVideo,
    RecordAudio,
    UploadAudio,
    UploadDocument,
    FindLocation
}

impl Into<String> for Action {
    fn into(self) -> String {
        let tmp = match self {
            Action::Typing => "Typing",
            Action::UploadPhoto => "UploadPhoto",
            Action::RecordVideo => "RecordVideo",
            Action::UploadVideo => "UploadVideo",
            Action::RecordAudio => "RecordVideo",
            Action::UploadAudio => "UploadAudio",
            Action::UploadDocument => "UploadDocument",
            Action::FindLocation => "FindLocation"
        };

        tmp.into()
    }
}

/// A simple method for testing your bot's auth token. Requires no parameters. Returns basic
/// information about the bot in form of a User object.
#[derive(TelegramFunction, Serialize)]
#[call = "getMe"]
#[answer = "User"]
#[function = "get_me"]
pub struct GetMe;

#[derive(TelegramFunction,  Serialize)]
#[call = "getUpdates"]
#[answer = "Updates"]
#[function = "get_updates"]
pub struct GetUpdates {
#[serde(skip_serializing_if="Option::is_none")]
    offset: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    limit: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    timeout: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    allowed_updates: Option<Vec<String>>
}

/// Use this method to send text messages. On success, the sent Message is returned.
#[derive(TelegramFunction, Serialize)]
#[call = "sendMessage"]
#[answer = "Message"]
#[function = "message"]
pub struct Message {
    chat_id: Integer,
    text: String,
#[serde(skip_serializing_if="Option::is_none")]
    parse_mode: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    disable_web_page_preview: Option<bool>,
#[serde(skip_serializing_if="Option::is_none")]
    disable_notificaton: Option<bool>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_to_message_id: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_markup: Option<NotImplemented>
}

/// Use this method to send photos. On success, the sent Message is returned.
#[derive(TelegramFunction,  Serialize)]
#[call = "sendPhoto"]
#[answer = "Message"]
#[function = "photo"]
#[file_kind = "photo"]
pub struct SendPhoto {
    chat_id: Integer,
    photo: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    caption: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    disable_notification: Option<bool>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_to_message_id: Option<bool>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_markup: Option<NotImplemented>
}

/// Use this method to send audio files, if you want Telegram clients to display them in the music
/// player. Your audio must be in the .mp3 format. On success, the sent Message is returned. Bots
/// can currently send audio files of up to 50 MB in size, this limit may be changed in the future.
///
/// For sending voice messages, use the sendVoice method instead.
#[derive(TelegramFunction,  Serialize)]
#[call = "sendAudio"]
#[answer = "Message"]
#[function = "audio"]
#[file_kind = "audio"]
pub struct SendAudio {
    chat_id: Integer,
    audio: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    caption: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    duration: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    performer: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    title: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    disable_notification: Option<bool>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_to_message_id: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_markup: Option<NotImplemented>
}

/// Use this method to send general files. On success, the sent Message is returned. Bots can
/// currently send files of any type of up to 50 MB in size, this limit may be changed in the
/// future.
#[derive(TelegramFunction,  Serialize)]
#[call = "sendDocument"]
#[answer = "Message"]
#[function = "document"]
#[file_kind = "document"]
pub struct SendDocument {
    chat_id: Integer,
    document: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    caption: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    disable_notification: Option<bool>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_to_message_id: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_markup: Option<NotImplemented>
}

/// Use this method to send .webp stickers. On success, the sent Message is returned.
#[derive(TelegramFunction,  Serialize)]
#[call = "sendSticker"]
#[answer = "Message"]
#[function = "sticker"]
#[file_kind = "sticker"]
pub struct SendSticker {
    chat_id: Integer,
    sticker: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    disable_notification: Option<bool>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_to_message_id: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_markup: Option<NotImplemented>
}

/// Use this method to send video files, Telegram clients support mp4 videos (other formats may be
/// sent as Document). On success, the sent Message is returned. Bots can currently send video
/// files of up to 50 MB in size, this limit may be changed in the future.
#[derive(TelegramFunction,  Serialize)]
#[call = "sendVideo"]
#[answer = "Message"]
#[function = "video"]
#[file_kind = "video"]
pub struct SendVideo {
    chat_id: Integer,
    video: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    duration: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    width: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    height: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    caption: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    disable_notification: Option<bool>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_to_message_id: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_markup: Option<NotImplemented>
}

/// Use this method to send audio files, if you want Telegram clients to display the file as a
/// playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS
/// (other formats may be sent as Audio or Document). On success, the sent Message is returned.
/// Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the
/// future.
#[derive(TelegramFunction,  Serialize)]
#[call = "sendVoice"]
#[answer = "Message"]
#[function = "voice"]
#[file_kind = "voice"]
pub struct SendVoice {
    chat_id: Integer,
    voice: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    caption: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    duration: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    disable_notification: Option<bool>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_to_message_id: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_markup: Option<NotImplemented>
}

/// Use this method to send point on the map. On success, the sent Message is returned.
#[derive(TelegramFunction,  Serialize)]
#[call = "sendLocation"]
#[answer = "Message"]
#[function = "location"]
pub struct SendLocation {
    chat_id: Integer,
    latitude: f32,
    longitude: f32,
#[serde(skip_serializing_if="Option::is_none")]
    disable_notification: Option<bool>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_to_message_id: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_markup: Option<NotImplemented>
}

/// Use this method to send information about a venue. On success, the sent Message is returned.
#[derive(TelegramFunction,  Serialize)]
#[call = "sendVenue"]
#[answer = "Message"]
#[function = "venue"]
pub struct SendVenue {
    chat_id: Integer,
    latitude: f32,
    longitude: f32,
    title: String,
    address: String,
    foursquare_id: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    disable_notification: Option<bool>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_to_message_id: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_markup: Option<NotImplemented>
}

/// Use this method to send phone contacts. On success, the sent Message is returned.
#[derive(TelegramFunction,  Serialize)]
#[call = "sendContact"]
#[answer = "Message"]
#[function = "contact"]
pub struct SendContact {
    chat_id: Integer,
    phone_number: String,
    first_name: String,
    last_name: Option<String>,
#[serde(skip_serializing_if="Option::is_none")]
    disable_notification: Option<bool>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_to_message_id: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    reply_markup: Option<NotImplemented>
}

/// Use this method when you need to tell the user that something is happening on the bot's side.
/// The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients
/// clear its typing status). Returns True on success.
#[derive(TelegramFunction,  Serialize)]
#[call = "sendChatAction"]
#[answer = "Boolean"]
#[function = "chat_action"]
pub struct SendAction {
    chat_id: Integer,
    action: String
}

/// Use this method to get a list of profile pictures for a user. Returns a UserProfilePhotos
/// object.
#[derive(TelegramFunction,  Serialize)]
#[call = "getUserProfilePhotos"]
#[answer = "UserProfilePhotos"]
#[function = "get_user_profile_photos"]
pub struct GetUserProfilePhotos {
    user_id: Integer,
#[serde(skip_serializing_if="Option::is_none")]
    offset: Option<Integer>,
#[serde(skip_serializing_if="Option::is_none")]
    limit: Option<Integer>
}

/// Use this method to get basic info about a file and prepare it for downloading. For the moment,
/// bots can download files of up to 20MB in size. On success, a File object is returned. The file
/// can then be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>, where
/// <file_path> is taken from the response. It is guaranteed that the link will be valid for at
/// least 1 hour. When the link expires, a new one can be requested by calling getFile again.
#[derive(TelegramFunction,  Serialize)]
#[call = "getFile"]
#[answer = "File"]
#[function = "get_file"]
pub struct GetFile {
    file_id: String
}

/// Use this method to kick a user from a group or a supergroup. In the case of supergroups, the
/// user will not be able to return to the group on their own using invite links, etc., unless
/// unbanned first. The bot must be an administrator in the group for this to work. Returns True on
/// success.
#[derive(TelegramFunction,  Serialize)]
#[call = "kickChatMember"]
#[answer = "Boolean"]
#[function = "kick_chat_member"]
pub struct KickChatMember {
    chat_id: Integer,
    user_id: Integer
}

/// Use this method for your bot to leave a group, supergroup or channel. Returns True on
/// success.
#[derive(TelegramFunction,  Serialize)]
#[call = "leaveChat"]
#[answer = "Boolean"]
#[function = "leave_chat"]
pub struct LeaveChat {
    chat_id: Integer,
}

/// Use this method to unban a previously kicked user in a supergroup. The user will not return to
/// the group automatically, but will be able to join via link, etc. The bot must be an
/// administrator in the group for this to work. Returns True on success.
#[derive(TelegramFunction,  Serialize)]
#[call = "unbanChatMember"]
#[answer = "Boolean"]
#[function = "unban_chat_member"]
pub struct UnbanChatMember {
    chat_id: Integer,
    user_id: Integer
}

/// Use this method to get up to date information about the chat (current name of the user for
/// one-on-one conversations, current username of a user, group or channel, etc.). Returns a Chat
/// object on success.
#[derive(TelegramFunction,  Serialize)]
#[call = "getChat"]
#[answer = "Chat"]
#[function = "get_chat"]
pub struct GetChat {
    chat_id: Integer
}

/// Use this method to get a list of administrators in a chat. On success, returns an Array of
/// ChatMember objects that contains information about all chat administrators except other bots.
/// If the chat is a group or a supergroup and no administrators were appointed, only the creator
/// will be returned.
#[derive(TelegramFunction,  Serialize)]
#[call = "getChatAdministrators"]
#[answer = "Vector<objects::ChatMember>"]
#[function = "unban_chat_administrators"]
pub struct GetChatAdministrators {
    chat_id: Integer
}

/// Use this method to get the number of members in a chat. Returns Int on success.
#[derive(TelegramFunction,  Serialize)]
#[call = "getChatMembersCount"]
#[answer = "Integer"]
#[function = "get_chat_members_count"]
pub struct GetChatMemberCounts {
    chat_id: Integer
}

/// Use this method to get information about a member of a chat. Returns a ChatMember object on
/// success.
#[derive(TelegramFunction,  Serialize)]
#[call = "getChatMember"]
#[answer = "ChatMember"]
#[function = "get_chat_members_count"]
pub struct GetChatMember {
    chat_id: Integer,
    user_id: Integer
}

/// Use this method to send answers to callback queries sent from inline keyboards. The answer will
/// be displayed to the user as a notification at the top of the chat screen or as an alert. On
/// success, True is returned.
#[derive(TelegramFunction,  Serialize)]
#[call = "answerCallbackQuery"]
#[answer = "Boolean"]
#[function = "answer_callback_query"]
pub struct AnswerCallbackQuery {
    callback_query_id: String,
    text: Option<String>,
    show_alert: Option<bool>,
    url: Option<String>,
    cache_time: Option<Integer>
}

/// Use this method to send answers to an inline query. On success, True is returned.
/// No more than 50 results per query are allowed.
#[derive(TelegramFunction,  Serialize)]
#[call = "answerInlineQuery"]
#[answer = "Boolean"]
#[function = "answer_inline_query"]
pub struct AnswerInlineQuery {
    inline_query_id: String,
    results: Vec<Box<Serialize>>,
    cache_time: Option<Integer>,
    is_personal: Option<bool>,
    next_offset: Option<String>,
    switch_pm_text: Option<String>,
    switch_pm_parameter: Option<String>
}