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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
// Copyright 2020 - developers of the `grammers` project.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! This module contains several functions to build a reply markup usable by bots when sending
//! messages through [`crate::InputMessage::reply_markup`].
//!
//! Each function returns a concrete builder-like type that may be further configured via their
//! inherent methods.
//!
//! The trait is used to group all types as "something that may be used as a reply markup".
use super::button;
use grammers_tl_types as tl;
#[doc(hidden)]
pub struct Markup {
pub raw: tl::enums::ReplyMarkup,
}
/// Trait used by types that can be interpreted as a raw reply markup.
pub trait ReplyMarkup {
fn to_reply_markup(&self) -> Markup;
}
/// Structure holding the state for inline reply markups.
///
/// See [`inline`] for usage examples.
pub struct Inline {
raw: tl::types::ReplyInlineMarkup,
}
/// Structure holding the state for keyboard reply markups.
///
/// See [`keyboard`] for usage examples.
pub struct Keyboard {
raw: tl::types::ReplyKeyboardMarkup,
}
/// Structure holding the state for reply markups that hide previous keyboards.
///
/// See [`hide`] for usage examples.
pub struct Hide {
raw: tl::types::ReplyKeyboardHide,
}
/// Structure holding the state for reply markups that force a reply.
///
/// See [`force_reply`] for usage examples.
pub struct ForceReply {
raw: tl::types::ReplyKeyboardForceReply,
}
impl ReplyMarkup for Inline {
fn to_reply_markup(&self) -> Markup {
Markup {
raw: self.raw.clone().into(),
}
}
}
impl ReplyMarkup for Keyboard {
fn to_reply_markup(&self) -> Markup {
Markup {
raw: self.raw.clone().into(),
}
}
}
impl ReplyMarkup for Hide {
fn to_reply_markup(&self) -> Markup {
Markup {
raw: self.raw.clone().into(),
}
}
}
impl ReplyMarkup for ForceReply {
fn to_reply_markup(&self) -> Markup {
Markup {
raw: self.raw.clone().into(),
}
}
}
/// Define inline buttons for a message.
///
/// These will display right under the message.
///
/// You cannot add images to the buttons, but you can use emoji (simply copy-paste them into your
/// code, or use the correct escape sequence, or using any other input methods you like).
///
/// You will need to provide a matrix of [`button::Inline`], that is, a vector that contains the
/// rows from top to bottom, where the rows consist of a vector of buttons from left to right.
/// See the [`button`] module to learn what buttons are available.
///
/// # Examples
///
/// ```
/// # async fn f(client: &mut grammers_client::Client, chat: &grammers_client::types::Chat) -> Result<(), Box<dyn std::error::Error>> {
/// use grammers_client::{InputMessage, reply_markup, button};
///
/// let artist = "Krewella";
/// client.send_message(chat, InputMessage::text("Select song").reply_markup(&reply_markup::keyboard(vec![
/// vec![button::text(format!("Song by {}", artist))],
/// vec![button::text("Previous"), button::text("Next")],
/// ]))).await?;
/// # Ok(())
/// # }
/// ```
pub fn inline<B: Into<Vec<Vec<button::Inline>>>>(buttons: B) -> Inline {
Inline {
raw: tl::types::ReplyInlineMarkup {
rows: buttons
.into()
.into_iter()
.map(|row| {
tl::types::KeyboardButtonRow {
buttons: row.into_iter().map(|button| button.raw).collect(),
}
.into()
})
.collect(),
},
}
}
/// Define a custom keyboard, replacing the user's own virtual keyboard.
///
/// This will be displayed below the input message field for users, and on mobile devices, this
/// also hides the virtual keyboard (effectively "replacing" it).
///
/// You cannot add images to the buttons, but you can use emoji (simply copy-paste them into your
/// code, or use the correct escape sequence, or using any other input methods you like).
///
/// You will need to provide a matrix of [`button::Inline`], that is, a vector that contains the
/// rows from top to bottom, where the rows consist of a vector of buttons from left to right.
/// See the [`button`] module to learn what buttons are available.
///
/// See the return type for further configuration options.
///
/// # Examples
///
/// ```
/// # async fn f(client: &mut grammers_client::Client, chat: &grammers_client::types::Chat) -> Result<(), Box<dyn std::error::Error>> {
/// use grammers_client::{InputMessage, reply_markup, button};
///
/// client.send_message(chat, InputMessage::text("What do you want to do?").reply_markup(&reply_markup::keyboard(vec![
/// vec![button::text("Accept")],
/// vec![button::text("Cancel"), button::text("Try something else")],
/// ]))).await?;
/// # Ok(())
/// # }
/// ```
pub fn keyboard<B: Into<Vec<Vec<button::Keyboard>>>>(buttons: B) -> Keyboard {
Keyboard {
raw: tl::types::ReplyKeyboardMarkup {
resize: false,
single_use: false,
selective: false,
persistent: false,
rows: buttons
.into()
.into_iter()
.map(|row| {
tl::types::KeyboardButtonRow {
buttons: row.into_iter().map(|button| button.raw).collect(),
}
.into()
})
.collect(),
placeholder: None,
},
}
}
/// Hide a previously-sent keyboard.
///
/// See the return type for further configuration options.
///
/// # Examples
///
/// ```
/// # async fn f(client: &mut grammers_client::Client, chat: &grammers_client::types::Chat) -> Result<(), Box<dyn std::error::Error>> {
/// use grammers_client::{InputMessage, reply_markup};
///
/// client.send_message(chat, InputMessage::text("Bot keyboards removed.").reply_markup(&reply_markup::hide())).await?;
/// # Ok(())
/// # }
/// ```
pub fn hide() -> Hide {
Hide {
raw: tl::types::ReplyKeyboardHide { selective: false },
}
}
/// "Forces" the user to send a reply.
///
/// This will cause the user's application to automatically select the message for replying to it,
/// although the user is still able to dismiss the reply and send a normal message.
///
/// See the return type for further configuration options.
///
/// # Examples
///
/// ```
/// # async fn f(client: &mut grammers_client::Client, chat: &grammers_client::types::Chat) -> Result<(), Box<dyn std::error::Error>> {
/// use grammers_client::{InputMessage, reply_markup};
///
/// let markup = reply_markup::force_reply().single_use();
/// client.send_message(chat, InputMessage::text("Reply me!").reply_markup(&markup)).await?;
/// # Ok(())
/// # }
/// ```
pub fn force_reply() -> ForceReply {
ForceReply {
raw: tl::types::ReplyKeyboardForceReply {
single_use: false,
selective: false,
placeholder: None,
},
}
}
impl Keyboard {
/// Requests clients to resize the keyboard vertically for optimal fit (e.g., make the
/// keyboard smaller if there are just two rows of buttons). Otherwise, the custom keyboard
/// is always of the same height as the virtual keyboard.
pub fn fit_size(mut self) -> Self {
self.raw.resize = true;
self
}
/// Requests clients to hide the keyboard as soon as it's been used.
///
/// The keyboard will still be available, but clients will automatically display the usual
/// letter-keyboard in the chat – the user can press a special button in the input field to
/// see the custom keyboard again.
pub fn single_use(mut self) -> Self {
self.raw.single_use = true;
self
}
/// Force the reply to specific users only.
///
/// The selected user will be either the people @_mentioned in the text of the `Message`
/// object, or if the bot's message is a reply, the sender of the original message.
pub fn selective(mut self) -> Self {
self.raw.selective = true;
self
}
}
impl Hide {
/// Hide the keyboard for specific users only.
///
/// The selected user will be either the people @_mentioned in the text of the `Message`
/// object, or if the bot's message is a reply, the sender of the original message.
pub fn selective(mut self) -> Self {
self.raw.selective = true;
self
}
}
impl ForceReply {
/// Requests clients to hide the keyboard as soon as it's been used.
///
/// The keyboard will still be available, but clients will automatically display the usual
/// letter-keyboard in the chat – the user can press a special button in the input field to
/// see the custom keyboard again.
pub fn single_use(mut self) -> Self {
self.raw.single_use = true;
self
}
/// Force the reply to specific users only.
///
/// The selected user will be either the people @_mentioned in the text of the `Message`
/// object, or if the bot's message is a reply, the sender of the original message.
pub fn selective(mut self) -> Self {
self.raw.selective = true;
self
}
}