tgbot/types/inline_mode/result/mod.rs
1use serde::{Deserialize, Serialize};
2
3use self::raw::RawInlineQueryResult;
4pub use self::{
5 article::*,
6 audio::*,
7 contact::*,
8 document::*,
9 game::*,
10 gif::*,
11 location::*,
12 mpeg4_gif::*,
13 photo::*,
14 sticker::*,
15 venue::*,
16 video::*,
17 voice::*,
18};
19use crate::types::{Location, User, WebAppInfo};
20
21#[cfg(test)]
22mod tests;
23
24mod article;
25mod audio;
26mod contact;
27mod document;
28mod game;
29mod gif;
30mod location;
31mod mpeg4_gif;
32mod photo;
33mod raw;
34mod sticker;
35mod venue;
36mod video;
37mod voice;
38
39/// Represents a result of an inline query.
40#[allow(clippy::large_enum_variant)]
41#[derive(Clone, Debug, derive_more::From, Deserialize, PartialEq, Serialize)]
42#[serde(try_from = "RawInlineQueryResult", into = "RawInlineQueryResult")]
43pub enum InlineQueryResult {
44 /// Link to an article or web page.
45 Article(InlineQueryResultArticle),
46 /// Link to an MP3 audio file.
47 Audio(InlineQueryResultAudio),
48 /// Link to an MP3 audio file stored on the Telegram servers.
49 CachedAudio(InlineQueryResultCachedAudio),
50 /// Link to a file stored on the Telegram servers.
51 CachedDocument(InlineQueryResultCachedDocument),
52 /// Link to an animated GIF file stored on the Telegram servers.
53 CachedGif(InlineQueryResultCachedGif),
54 /// Link to a video animation
55 /// (H.264/MPEG-4 AVC video without sound) stored on the Telegram servers.
56 CachedMpeg4Gif(InlineQueryResultCachedMpeg4Gif),
57 /// Link to a photo stored on the Telegram servers.
58 CachedPhoto(InlineQueryResultCachedPhoto),
59 /// Link to a sticker stored on the Telegram servers.
60 CachedSticker(InlineQueryResultCachedSticker),
61 /// Link to a video file stored on the Telegram servers.
62 CachedVideo(InlineQueryResultCachedVideo),
63 /// Link to a voice message stored on the Telegram servers.
64 CachedVoice(InlineQueryResultCachedVoice),
65 /// Contact with a phone number.
66 Contact(InlineQueryResultContact),
67 /// Link to a file.
68 Document(InlineQueryResultDocument),
69 /// Game.
70 Game(InlineQueryResultGame),
71 /// Link to an animated GIF file.
72 Gif(InlineQueryResultGif),
73 /// Location on a map.
74 Location(InlineQueryResultLocation),
75 /// Link to a video animation (H.264/MPEG-4 AVC video without sound).
76 Mpeg4Gif(InlineQueryResultMpeg4Gif),
77 /// Link to a photo.
78 Photo(InlineQueryResultPhoto),
79 /// Venue.
80 Venue(InlineQueryResultVenue),
81 /// Link to a page containing an embedded video player or a video file.
82 Video(InlineQueryResultVideo),
83 /// Link to a voice recording in an OGG container encoded with OPUS.
84 Voice(InlineQueryResultVoice),
85}
86
87/// Represents a button to be shown above inline query results.
88#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
89pub struct InlineQueryResultsButton {
90 text: String,
91 #[serde(flatten)]
92 button_type: InlineQueryResultsButtonType,
93}
94
95impl InlineQueryResultsButton {
96 /// Creates a new `InlineQueryResultsButton` for a web app.
97 ///
98 /// # Arguments
99 ///
100 /// * `text` - Label text on the button.
101 /// * `web_app_info` - Description of the Web App that will be launched
102 /// when the user presses the button;
103 /// the Web App will be able to switch back to the inline mode
104 /// using the method `switchInlineQuery` inside the Web App.
105 pub fn for_web_app<T>(text: T, web_app_info: WebAppInfo) -> Self
106 where
107 T: Into<String>,
108 {
109 Self {
110 text: text.into(),
111 button_type: InlineQueryResultsButtonType::WebApp(web_app_info),
112 }
113 }
114
115 /// Creates a new `InlineQueryResultsButton` for a parameter of the `/start` message.
116 ///
117 /// # Arguments
118 ///
119 /// * `text` - Label text on the button.
120 /// * `start_parameter` - Deep-linking parameter for the `/start` message
121 /// sent to the bot when a user presses the button;
122 /// 1-64 characters, only A-Z, a-z, 0-9, _ and - are allowed.
123 pub fn for_start_parameter<A, B>(text: A, start_parameter: B) -> Self
124 where
125 A: Into<String>,
126 B: Into<String>,
127 {
128 Self {
129 text: text.into(),
130 button_type: InlineQueryResultsButtonType::StartParameter(start_parameter.into()),
131 }
132 }
133}
134
135#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
136#[serde(rename_all = "snake_case")]
137enum InlineQueryResultsButtonType {
138 WebApp(WebAppInfo),
139 StartParameter(String),
140}
141
142/// Represents a result of an inline query
143/// that was chosen by the user and sent to their chat partner.
144#[serde_with::skip_serializing_none]
145#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
146pub struct ChosenInlineResult {
147 /// The user that chose the result.
148 pub from: User,
149 /// The query that was used to obtain the result.
150 pub query: String,
151 /// The unique identifier for the result that was chosen.
152 pub result_id: String,
153 /// Identifier of the sent inline message.
154 ///
155 /// Available only if there is an inline keyboard attached to the message.
156 /// Will be also received in callback queries and can be used to edit the message.
157 pub inline_message_id: Option<String>,
158 /// Sender location, only for bots that require user location.
159 pub location: Option<Location>,
160}
161
162impl ChosenInlineResult {
163 /// Creates a new `ChosenInlineResult`.
164 ///
165 /// # Arguments
166 ///
167 /// * `from` - The user that chose the result.
168 /// * `query` - The query that was used to obtain the result.
169 /// * `result_id` - Unique identifier of the chosen result.
170 pub fn new<A, B>(from: User, query: A, result_id: B) -> Self
171 where
172 A: Into<String>,
173 B: Into<String>,
174 {
175 Self {
176 from,
177 query: query.into(),
178 result_id: result_id.into(),
179 inline_message_id: None,
180 location: None,
181 }
182 }
183
184 /// Sets a new inline message ID.
185 ///
186 /// # Arguments
187 ///
188 /// * `value` - Identifier of the sent inline message.
189 pub fn with_inline_message_id<T>(mut self, value: T) -> Self
190 where
191 T: Into<String>,
192 {
193 self.inline_message_id = Some(value.into());
194 self
195 }
196
197 /// Sets a new location.
198 ///
199 /// # Arguments
200 ///
201 /// * `value` - Sender location.
202 pub fn with_location(mut self, value: Location) -> Self {
203 self.location = Some(value);
204 self
205 }
206}