pub trait SendMessageSetters: Sized + HasPayload<Payload = SendMessage> {
Show 16 methods
// Provided methods
fn text<T>(self, value: T) -> Self
where T: Into<String> { ... }
fn business_connection_id(self, value: BusinessConnectionId) -> Self { ... }
fn chat_id<T>(self, value: T) -> Self
where T: Into<Recipient> { ... }
fn ephemeral_message_parameters(
self,
value: EphemeralMessageParameters,
) -> Self { ... }
fn message_thread_id(self, value: ThreadId) -> Self { ... }
fn direct_messages_topic_id(self, value: TopicId) -> Self { ... }
fn parse_mode(self, value: ParseMode) -> Self { ... }
fn entities<T>(self, value: T) -> Self
where T: IntoIterator<Item = <Vec<MessageEntity> as IntoIterator>::Item> { ... }
fn link_preview_options(self, value: LinkPreviewOptions) -> Self { ... }
fn disable_notification(self, value: bool) -> Self { ... }
fn protect_content(self, value: bool) -> Self { ... }
fn allow_paid_broadcast(self, value: bool) -> Self { ... }
fn message_effect_id(self, value: EffectId) -> Self { ... }
fn suggested_post_parameters(self, value: SuggestedPostParameters) -> Self { ... }
fn reply_parameters(self, value: ReplyParameters) -> Self { ... }
fn reply_markup<T>(self, value: T) -> Self
where T: Into<ReplyMarkup> { ... }
}Expand description
Setters for fields of SendMessage
Provided Methods§
Sourcefn business_connection_id(self, value: BusinessConnectionId) -> Self
fn business_connection_id(self, value: BusinessConnectionId) -> Self
Setter for business_connection_id field.
Sourcefn chat_id<T>(self, value: T) -> Self
fn chat_id<T>(self, value: T) -> Self
Setter for chat_id field.
Examples found in repository?
More examples
examples/dialogue.rs (line 61)
60async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
61 bot.send_message("Let's start! What's your full name?").chat_id(msg.chat.id).await?;
62 dialogue.update(State::ReceiveFullName).await?;
63 Ok(())
64}
65
66async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
67 match msg.text() {
68 Some(text) => {
69 bot.send_message("How old are you?").chat_id(msg.chat.id).await?;
70 dialogue.update(State::ReceiveAge { full_name: text.into() }).await?;
71 }
72 None => {
73 bot.send_message("Send me plain text.").chat_id(msg.chat.id).await?;
74 }
75 }
76
77 Ok(())
78}
79
80async fn receive_age(
81 bot: Bot,
82 dialogue: MyDialogue,
83 full_name: String, // Available from `State::ReceiveAge`.
84 msg: Message,
85) -> HandlerResult {
86 match msg.text().map(|text| text.parse::<u8>()) {
87 Some(Ok(age)) => {
88 bot.send_message("What's your location?").chat_id(msg.chat.id).await?;
89 dialogue.update(State::ReceiveLocation { full_name, age }).await?;
90 }
91 _ => {
92 bot.send_message("Send me a number.").chat_id(msg.chat.id).await?;
93 }
94 }
95
96 Ok(())
97}
98
99async fn receive_location(
100 bot: Bot,
101 dialogue: MyDialogue,
102 (full_name, age): (String, u8), // Available from `State::ReceiveLocation`.
103 msg: Message,
104) -> HandlerResult {
105 match msg.text() {
106 Some(location) => {
107 let report = format!("Full name: {full_name}\nAge: {age}\nLocation: {location}");
108 bot.send_message(report).chat_id(msg.chat.id).await?;
109 dialogue.exit().await?;
110 }
111 None => {
112 bot.send_message("Send me plain text.").chat_id(msg.chat.id).await?;
113 }
114 }
115
116 Ok(())
117}examples/purchase.rs (line 88)
87async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
88 bot.send_message("Let's start! What's your full name?").chat_id(msg.chat.id).await?;
89 dialogue.update(State::ReceiveFullName).await?;
90 Ok(())
91}
92
93async fn help(bot: Bot, msg: Message) -> HandlerResult {
94 bot.send_message(Command::descriptions().to_string()).chat_id(msg.chat.id).await?;
95 Ok(())
96}
97
98async fn cancel(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
99 bot.send_message("Cancelling the dialogue.").chat_id(msg.chat.id).await?;
100 dialogue.exit().await?;
101 Ok(())
102}
103
104async fn invalid_state(bot: Bot, msg: Message) -> HandlerResult {
105 bot.send_message("Unable to handle the message. Type /help to see the usage.")
106 .chat_id(msg.chat.id)
107 .await?;
108 Ok(())
109}
110
111async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
112 match msg.text().map(ToOwned::to_owned) {
113 Some(full_name) => {
114 let products = ["Apple", "Banana", "Orange", "Potato"]
115 .map(|product| InlineKeyboardButton::callback(product, product));
116
117 bot.send_message("Select a product:")
118 .chat_id(msg.chat.id)
119 .reply_markup(InlineKeyboardMarkup::new([products]))
120 .await?;
121 dialogue.update(State::ReceiveProductChoice { full_name }).await?;
122 }
123 None => {
124 bot.send_message("Please, send me your full name.").chat_id(msg.chat.id).await?;
125 }
126 }
127
128 Ok(())
129}
130
131async fn receive_product_selection(
132 bot: Bot,
133 dialogue: MyDialogue,
134 full_name: String, // Available from `State::ReceiveProductChoice`.
135 q: CallbackQuery,
136) -> HandlerResult {
137 if let Some(product) = &q.data {
138 bot.send_message(format!(
139 "{full_name}, product '{product}' has been purchased successfully!"
140 ))
141 .chat_id(dialogue.chat_id())
142 .await?;
143 dialogue.exit().await?;
144 }
145
146 Ok(())
147}examples/error_handler.rs (line 9)
6async fn main() {
7 pretty_env_logger::init();
8 let handler = Update::filter_message().endpoint(|bot: Bot, message: Message| async move {
9 bot.send_message("Hello!").chat_id(message.chat.id).await?;
10 Ok::<(), teloxide::RequestError>(())
11 });
12
13 Dispatcher::builder(Bot::from_env(), handler)
14 .error_handler(Arc::new(report_error))
15 .build()
16 .dispatch()
17 .await;
18}
19
20async fn report_error(error: teloxide::RequestError, deps: DependencyMap) {
21 log::error!("Could not handle update: {error}");
22 let (Some(bot), Some(update)) = (deps.try_get::<Bot>(), deps.try_get::<Update>()) else {
23 return;
24 };
25 if let Some(chat) = update.chat() {
26 bot.send_message("Something went wrong. Please try again later.")
27 .chat_id(chat.id)
28 .await
29 .log_on_error()
30 .await;
31 }
32}examples/db_remember.rs (line 69)
64async fn start(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
65 match msg.text().map(|text| text.parse::<i32>()) {
66 Some(Ok(n)) => {
67 dialogue.update(State::GotNumber(n)).await?;
68 bot.send_message(format!("Remembered number {n}. Now use /get or /reset."))
69 .chat_id(msg.chat.id)
70 .await?;
71 }
72 _ => {
73 bot.send_message("Please, send me a number.").chat_id(msg.chat.id).await?;
74 }
75 }
76
77 Ok(())
78}
79
80async fn got_number(
81 bot: Bot,
82 dialogue: MyDialogue,
83 num: i32, // Available from `State::GotNumber`.
84 msg: Message,
85 cmd: Command,
86) -> HandlerResult {
87 match cmd {
88 Command::Get => {
89 bot.send_message(format!("Here is your number: {num}.")).chat_id(msg.chat.id).await?;
90 }
91 Command::Reset => {
92 dialogue.reset().await?;
93 bot.send_message("Number reset.").chat_id(msg.chat.id).await?;
94 }
95 }
96 Ok(())
97}
98
99async fn invalid_command(bot: Bot, msg: Message) -> HandlerResult {
100 bot.send_message("Please, send /get or /reset.").chat_id(msg.chat.id).await?;
101 Ok(())
102}Sourcefn ephemeral_message_parameters(self, value: EphemeralMessageParameters) -> Self
fn ephemeral_message_parameters(self, value: EphemeralMessageParameters) -> Self
Setter for ephemeral_message_parameters field.
Sourcefn message_thread_id(self, value: ThreadId) -> Self
fn message_thread_id(self, value: ThreadId) -> Self
Setter for message_thread_id field.
Sourcefn direct_messages_topic_id(self, value: TopicId) -> Self
fn direct_messages_topic_id(self, value: TopicId) -> Self
Setter for direct_messages_topic_id field.
Sourcefn parse_mode(self, value: ParseMode) -> Self
fn parse_mode(self, value: ParseMode) -> Self
Setter for parse_mode field.
Examples found in repository?
examples/deep_linking.rs (line 112)
99pub async fn send_message(
100 bot: Bot,
101 id: ChatId, // Available from `State::WriteToSomeone`
102 msg: Message,
103 dialogue: MyDialogue,
104 me: BotFullInfo,
105) -> HandlerResult {
106 match msg.text() {
107 Some(text) => {
108 // Trying to send a message to the user
109 let sent_result = bot
110 .send_message(format!("You have a new message!\n\n<i>{text}</i>"))
111 .chat_id(id)
112 .parse_mode(ParseMode::Html)
113 .await;
114
115 // And if no error is returned, success!
116 if sent_result.is_ok() {
117 bot.send_message(format!(
118 "Message sent!\n\nYour link is: {}?start={}",
119 me.tme_url().ok_or("The bot needs a public username for deep links")?,
120 msg.chat.id
121 ))
122 .chat_id(msg.chat.id)
123 .await?;
124 } else {
125 bot.send_message("Error sending message! Maybe user blocked the bot?")
126 .chat_id(msg.chat.id)
127 .await?;
128 }
129 dialogue.exit().await?;
130 }
131 None => {
132 bot.send_message("This bot can send only text.").chat_id(msg.chat.id).await?;
133 }
134 };
135 Ok(())
136}Sourcefn link_preview_options(self, value: LinkPreviewOptions) -> Self
fn link_preview_options(self, value: LinkPreviewOptions) -> Self
Setter for link_preview_options field.
Sourcefn disable_notification(self, value: bool) -> Self
fn disable_notification(self, value: bool) -> Self
Setter for disable_notification field.
Sourcefn protect_content(self, value: bool) -> Self
fn protect_content(self, value: bool) -> Self
Setter for protect_content field.
Sourcefn allow_paid_broadcast(self, value: bool) -> Self
fn allow_paid_broadcast(self, value: bool) -> Self
Setter for allow_paid_broadcast field.
Sourcefn message_effect_id(self, value: EffectId) -> Self
fn message_effect_id(self, value: EffectId) -> Self
Setter for message_effect_id field.
Sourcefn suggested_post_parameters(self, value: SuggestedPostParameters) -> Self
fn suggested_post_parameters(self, value: SuggestedPostParameters) -> Self
Setter for suggested_post_parameters field.
Sourcefn reply_parameters(self, value: ReplyParameters) -> Self
fn reply_parameters(self, value: ReplyParameters) -> Self
Setter for reply_parameters field.
Sourcefn reply_markup<T>(self, value: T) -> Selfwhere
T: Into<ReplyMarkup>,
fn reply_markup<T>(self, value: T) -> Selfwhere
T: Into<ReplyMarkup>,
Setter for reply_markup field.
Examples found in repository?
examples/purchase.rs (line 119)
111async fn receive_full_name(bot: Bot, dialogue: MyDialogue, msg: Message) -> HandlerResult {
112 match msg.text().map(ToOwned::to_owned) {
113 Some(full_name) => {
114 let products = ["Apple", "Banana", "Orange", "Potato"]
115 .map(|product| InlineKeyboardButton::callback(product, product));
116
117 bot.send_message("Select a product:")
118 .chat_id(msg.chat.id)
119 .reply_markup(InlineKeyboardMarkup::new([products]))
120 .await?;
121 dialogue.update(State::ReceiveProductChoice { full_name }).await?;
122 }
123 None => {
124 bot.send_message("Please, send me your full name.").chat_id(msg.chat.id).await?;
125 }
126 }
127
128 Ok(())
129}More examples
examples/buttons.rs (line 79)
63async fn message_handler(
64 bot: Bot,
65 msg: Message,
66 me: BotFullInfo,
67) -> Result<(), Box<dyn Error + Send + Sync>> {
68 if let Some(text) = msg.text() {
69 match BotCommands::parse(text, me.username().unwrap_or_default()) {
70 Ok(Command::Help) => {
71 // Just send the description of all commands.
72 bot.send_message(Command::descriptions().to_string()).chat_id(msg.chat.id).await?;
73 }
74 Ok(Command::Start) => {
75 // Create a list of buttons and send them.
76 let keyboard = make_keyboard();
77 bot.send_message("Debian versions:")
78 .chat_id(msg.chat.id)
79 .reply_markup(keyboard)
80 .await?;
81 }
82
83 Err(_) => {
84 bot.send_message("Command not found!").chat_id(msg.chat.id).await?;
85 }
86 }
87 }
88
89 Ok(())
90}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".