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
use crate::{
methods::Method,
request::{Form, Request},
types::{ChatId, InputFile, Integer, Message, ParseMode, ReplyMarkup, ReplyMarkupError},
};
#[derive(Debug)]
pub struct SendDocument {
form: Form,
}
impl SendDocument {
pub fn new<C, D>(chat_id: C, document: D) -> Self
where
C: Into<ChatId>,
D: Into<InputFile>,
{
let mut form = Form::new();
form.insert_field("chat_id", chat_id.into());
form.insert_field("document", document.into());
SendDocument { form }
}
pub fn thumb<V>(mut self, value: V) -> Self
where
V: Into<InputFile>,
{
self.form.insert_field("thumb", value.into());
self
}
pub fn caption<S: Into<String>>(mut self, value: S) -> Self {
self.form.insert_field("caption", value.into());
self
}
pub fn parse_mode(mut self, parse_mode: ParseMode) -> Self {
self.form.insert_field("parse_mode", parse_mode);
self
}
pub fn disable_notification(mut self, value: bool) -> Self {
self.form.insert_field("disable_notification", value);
self
}
pub fn reply_to_message_id(mut self, value: Integer) -> Self {
self.form.insert_field("reply_to_message_id", value);
self
}
pub fn reply_markup<R: Into<ReplyMarkup>>(mut self, value: R) -> Result<Self, ReplyMarkupError> {
let value = value.into();
self.form.insert_field("reply_markup", value.serialize()?);
Ok(self)
}
}
impl Method for SendDocument {
type Response = Message;
fn into_request(self) -> Request {
Request::form("sendDocument", self.form)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
request::{RequestBody, RequestMethod},
types::ForceReply,
};
#[test]
fn send_document() {
let request = SendDocument::new(1, InputFile::file_id("file-id"))
.thumb(InputFile::file_id("file-id"))
.caption("caption")
.parse_mode(ParseMode::Markdown)
.disable_notification(true)
.reply_to_message_id(1)
.reply_markup(ForceReply::new(true))
.unwrap()
.into_request();
assert_eq!(request.get_method(), RequestMethod::Post);
assert_eq!(request.build_url("base-url", "token"), "base-url/bottoken/sendDocument");
if let RequestBody::Form(form) = request.into_body() {
assert_eq!(form.fields["chat_id"].get_text().unwrap(), "1");
assert!(form.fields["document"].get_file().is_some());
assert!(form.fields["thumb"].get_file().is_some());
assert_eq!(form.fields["caption"].get_text().unwrap(), "caption");
assert_eq!(form.fields["parse_mode"].get_text().unwrap(), "Markdown");
assert_eq!(form.fields["disable_notification"].get_text().unwrap(), "true");
assert_eq!(form.fields["reply_to_message_id"].get_text().unwrap(), "1");
assert_eq!(
form.fields["reply_markup"].get_text().unwrap(),
r#"{"force_reply":true}"#
);
} else {
panic!("Unexpected request body");
}
}
}