tgbot/types/definitions/inline_mode/
query.rs

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