tgbot/types/inline_mode/query/
mod.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    api::{Method, Payload},
5    types::{InlineQueryResult, InlineQueryResultsButton, Integer, Location, User},
6};
7
8#[cfg(test)]
9mod tests;
10
11/// Represents an incoming inline query.
12///
13/// When the user sends an empty query, your bot could return some default or trending results.
14#[serde_with::skip_serializing_none]
15#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
16pub struct InlineQuery {
17    /// Sender of the query.
18    pub from: User,
19    /// Unique identifier of the query.
20    pub id: String,
21    /// Offset of the results to be returned, can be controlled by the bot.
22    pub offset: String,
23    /// Text of the query; up to 256 characters.
24    pub query: String,
25    /// Type of the chat, from which the inline query was sent.
26    ///
27    /// Can be either “sender” for a private chat with the inline query sender,
28    /// “private”, “group”, “supergroup”, or “channel”.
29    /// The chat type should be always known for requests sent from official
30    /// clients and most third-party clients,
31    /// unless the request was sent from a secret chat.
32    pub chat_type: Option<InlineQueryChatType>,
33    /// Sender location, only for bots that request user location.
34    pub location: Option<Location>,
35}
36
37impl InlineQuery {
38    /// Creates a new `InlineQuery`.
39    ///
40    /// # Arguments
41    ///
42    /// * `from` - Sender of the query.
43    /// * `id` - Unique identifier of the query.
44    /// * `offset` - Offset of the results.
45    /// * `query` - Text of the query; up to 256 characters.
46    pub fn new<A, B, C>(from: User, id: A, offset: B, query: C) -> Self
47    where
48        A: Into<String>,
49        B: Into<String>,
50        C: Into<String>,
51    {
52        Self {
53            from,
54            id: id.into(),
55            offset: offset.into(),
56            query: query.into(),
57            chat_type: None,
58            location: None,
59        }
60    }
61
62    /// Sets a new chat type.
63    ///
64    /// # Arguments
65    ///
66    /// * `value` - Type of the chat, from which the inline query was sent.
67    pub fn with_chat_type(mut self, value: InlineQueryChatType) -> Self {
68        self.chat_type = Some(value);
69        self
70    }
71
72    /// Sets a new location.
73    ///
74    /// # Arguments
75    ///
76    /// * `value` - Sender location.
77    pub fn with_location(mut self, value: Location) -> Self {
78        self.location = Some(value);
79        self
80    }
81}
82
83/// Represents a type of the chat, from which the inline query was sent.
84#[derive(Clone, Copy, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
85#[serde(rename_all = "snake_case")]
86pub enum InlineQueryChatType {
87    /// A private chat with the sender of the inline query.
88    Sender,
89    /// A private chat.
90    Private,
91    /// A group chat.
92    Group,
93    /// A supergroup chat.
94    Supergroup,
95    /// A channel chat.
96    Channel,
97}
98
99/// Sends an answer to an inline query.
100///
101/// No more than 50 results per query are allowed.
102#[serde_with::skip_serializing_none]
103#[derive(Clone, Debug, Serialize)]
104pub struct AnswerInlineQuery {
105    inline_query_id: String,
106    results: Vec<InlineQueryResult>,
107    button: Option<InlineQueryResultsButton>,
108    cache_time: Option<Integer>,
109    is_personal: Option<bool>,
110    next_offset: Option<String>,
111}
112
113impl AnswerInlineQuery {
114    /// Creates a new `AnswerInlineQuery`.
115    ///
116    /// # Arguments
117    ///
118    /// * `inline_query_id` - Unique identifier of the answered query.
119    /// * `results` - An array of results.
120    pub fn new<A, B>(inline_query_id: A, results: B) -> Self
121    where
122        A: Into<String>,
123        B: IntoIterator<Item = InlineQueryResult>,
124    {
125        Self {
126            inline_query_id: inline_query_id.into(),
127            results: results.into_iter().collect(),
128            button: None,
129            cache_time: None,
130            is_personal: None,
131            next_offset: None,
132        }
133    }
134
135    /// Sets a new button.
136    ///
137    /// # Arguments
138    ///
139    /// * `value` - An object describing a button to be shown above inline query results.
140    pub fn with_button(mut self, value: InlineQueryResultsButton) -> Self {
141        self.button = Some(value);
142        self
143    }
144
145    /// Sets a new cache time.
146    ///
147    /// # Arguments
148    ///
149    /// * `value` - Maximum amount of time in seconds that the result
150    ///   of the inline query may be cached on the server;
151    ///   default - 300.
152    pub fn with_cache_time(mut self, value: Integer) -> Self {
153        self.cache_time = Some(value);
154        self
155    }
156
157    /// Sets a new value for the `is_personal` flag.
158    ///
159    /// # Arguments
160    ///
161    /// * `value` - Indicates whether the cache results on the server side
162    ///   are only for the user that sent the query;
163    ///   by default, results may be returned to any user who sends the same query.
164    pub fn with_is_personal(mut self, value: bool) -> Self {
165        self.is_personal = Some(value);
166        self
167    }
168
169    /// Sets a new next offset.
170    ///
171    ///
172    /// # Arguments
173    ///
174    /// * `value` - Offset that a client should send in the next query with
175    ///   the same text to receive more results.
176    ///
177    /// Pass an empty string if there are no more results or if you don‘t support pagination.
178    /// Offset length can’t exceed 64 bytes.
179    pub fn with_next_offset<T>(mut self, value: T) -> Self
180    where
181        T: Into<String>,
182    {
183        self.next_offset = Some(value.into());
184        self
185    }
186}
187
188impl Method for AnswerInlineQuery {
189    type Response = bool;
190
191    fn into_payload(self) -> Payload {
192        Payload::json("answerInlineQuery", self)
193    }
194}