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
mod error;
mod serializers;
use std::future::Future;
use reqwest::multipart::Form;
use serde::Serialize;
use crate::requests::MultipartPayload;
use error::Error;
use serializers::MultipartSerializer;
pub(crate) fn to_form<T>(val: &mut T) -> Result<impl Future<Output = Form>, Error>
where
T: Serialize + MultipartPayload,
{
let mut form = val.serialize(MultipartSerializer::new())?;
let mut vec = Vec::with_capacity(1);
val.move_files(&mut |f| vec.push(f));
let iter = vec.into_iter();
let fut = async move {
for file in iter {
if file.needs_attach() {
let id = file.id().to_owned();
if let Some(part) = file.into_part() {
form = form.part(id, part.await);
}
}
}
form
};
Ok(fut)
}
pub(crate) fn to_form_ref<T: ?Sized>(val: &T) -> Result<impl Future<Output = Form>, Error>
where
T: Serialize + MultipartPayload,
{
let mut form = val.serialize(MultipartSerializer::new())?;
let mut vec = Vec::with_capacity(1);
val.copy_files(&mut |f| vec.push(f));
let iter = vec.into_iter();
let fut = async move {
for file in iter {
if file.needs_attach() {
let id = file.id().to_owned();
if let Some(part) = file.into_part() {
form = form.part(id, part.await);
}
}
}
form
};
Ok(fut)
}
#[cfg(test)]
mod tests {
use tokio::fs::File;
use super::to_form_ref;
use crate::{
payloads::{self, setters::*},
types::{
InputFile, InputMedia, InputMediaAnimation, InputMediaAudio, InputMediaDocument,
InputMediaPhoto, InputMediaVideo, InputSticker, MessageEntity, MessageEntityKind,
ParseMode,
},
};
#[tokio::test]
async fn issue_473() {
to_form_ref(
&payloads::SendPhoto::new(0, InputFile::file_id("0")).caption_entities([
MessageEntity {
kind: MessageEntityKind::Url,
offset: 0,
length: 0,
},
]),
)
.unwrap()
.await;
}
#[tokio::test]
async fn test_send_media_group() {
const CAPTION: &str = "caption";
to_form_ref(&payloads::SendMediaGroup::new(
0,
[
InputMedia::Photo(
InputMediaPhoto::new(InputFile::file("./media/logo.png"))
.caption(CAPTION)
.parse_mode(ParseMode::MarkdownV2)
.caption_entities(entities()),
),
InputMedia::Video(
InputMediaVideo::new(InputFile::file_id("17")).supports_streaming(true),
),
InputMedia::Animation(
InputMediaAnimation::new(InputFile::read(
File::open("./media/example.gif").await.unwrap(),
))
.thumb(InputFile::read(
File::open("./media/logo.png").await.unwrap(),
))
.duration(17),
),
InputMedia::Audio(
InputMediaAudio::new(InputFile::url("https://example.com".parse().unwrap()))
.performer("a"),
),
InputMedia::Document(InputMediaDocument::new(InputFile::memory(
&b"Hello world!"[..],
))),
],
))
.unwrap()
.await;
}
#[tokio::test]
async fn test_add_sticker_to_set() {
to_form_ref(&payloads::AddStickerToSet::new(
0,
"name",
InputSticker::Png(InputFile::file("./media/logo.png")),
"✈️⚙️",
))
.unwrap()
.await;
}
#[tokio::test]
async fn test_send_animation() {
to_form_ref(
&payloads::SendAnimation::new(0, InputFile::file("./media/logo.png"))
.caption_entities(entities())
.thumb(InputFile::read(
File::open("./media/logo.png").await.unwrap(),
))
.allow_sending_without_reply(true),
)
.unwrap()
.await;
}
fn entities() -> impl Iterator<Item = MessageEntity> {
<_>::into_iter([
MessageEntity::new(MessageEntityKind::Url, 0, 0),
MessageEntity::new(MessageEntityKind::Pre { language: None }, 0, 0),
MessageEntity::new(
MessageEntityKind::Pre {
language: Some(String::new()),
},
0,
0,
),
MessageEntity::new(MessageEntityKind::Url, 0, 0),
MessageEntity::new(
MessageEntityKind::TextLink {
url: "https://example.com".parse().unwrap(),
},
0,
0,
),
])
}
}