Skip to content
↑ ↓ move↵ openEvery page also as Markdown: add .md

Guide

Sending messages

Sending messages

Client::send_message sends text to a chat and returns the message Telegram stored. Any Message can then answer, edit or delete itself.

Example

use vaegram::prelude::*;

let sent = client.send_message("me", "Deploy finished in 42s").await?;
println!("message {} in chat {}", sent.id, sent.chat.id);

The request is only sent when it is awaited; the awaited value is the new Message.

How it works

Text with formatting

A plain string is sent as it is, with no formatting. Build styled text with Text:

use vaegram::prelude::*;

let text = Text::new().bold("Deploy").plain(" finished in ").code("42s");
assert_eq!(text.as_str(), "Deploy finished in 42s");
assert_eq!(text.entities().len(), 2);
See it run
Saved Messagesyour own chat
  1. Deploy finished in 42s

Entities

  1. bold, offset 0, length 6"Deploy"
  2. code, offset 19, length 3"42s"

bold, italic, underline, strike, spoiler and code take just the text. link also takes its URL, mention the user id, custom_emoji the emoji’s document id, and pre the language for highlighting. date_time and blockquote take more:

use vaegram::prelude::*;

let text = Text::new()
    .date_time("in two hours", 1_700_000_000, "r")
    .plain(" ")
    .blockquote("moved to Friday", false);

date_time takes the text, a unix_time: i32, and a &str whose letters pick the styles: r relative, t short time, T long time, d short date, D long date and w weekday. blockquote takes the text and an expandable flag, which hides the quote behind an expand button. Text keeps the plain string plus entities, and its offsets count UTF-16 units, as Telegram does.

Every send takes impl Into<Text>, and every edit impl Into<MessageText>; a &str, a String and a built Text all convert.

Parse modes

Instead of building a Text, you can send markup and let it be parsed:

client.send_message(123456789, "<b>bold</b>").html().await?;
client.send_message(123456789, "**bold**").markdown().await?;
client.send_message(123456789, "**bold**").parse_mode(ParseMode::Default).await?;

ParseMode is one of Disabled (send as is), Html, Markdown, or Default (both, with a backslash escaping). A send uses its own mode if set, else the client’s default from Client::builder(..).parse_mode(..), which is Disabled unless you change it.

A mode only applies to a Text made straight from a string. A built Text or one from Text::from_html / Text::from_markdown is already parsed and goes out unchanged. Text::parse(text, mode) does the same work and never fails: Disabled leaves the text plain, and nesting deeper than 64 levels is dropped.

Text parsed from user input must be escaped first, or a < in it breaks the markup:

use vaegram::escape_html;

let name = "Tom & Jerry <3";
let message = format!("<b>{}</b>", escape_html(name));
assert_eq!(message, "<b>Tom &amp; Jerry &lt;3</b>");

escape_markdown does the same for Markdown and Default.

Replies

message.reply("Got it").await?;
message.answer("Same chat and topic, without the quote").await?;

reply is answer().reply_to(this message). answer sends to the same chat, topic and business connection without pointing at a message.

To reply to a message in another chat, or to quote part of it, build ReplyParameters:

message
    .client()
    .send_message(-1001234567890, "look at this")
    .reply_to(ReplyParameters::new(7).chat_id(-1001234567890).quote("the part I mean"))
    .await?;

reply_to accepts a bare i32 message id too. quote_position places the quote by UTF-16 offset; Telegram rejects a quote whose text is not in the replied message.

Editing and deleting

let edited = message.edit_text("new text").await?;
message.edit_caption("new caption").await?;
message.edit_reply_markup(None).await?;
message.delete().await?;
message.client().delete_messages(message.chat.id, &[10, 11]).await?;

Edits return the edited Message. client.delete_messages takes 1 to 100 ids and deletes for everyone unless .revoke(false) says only for this account.

What a send returns

A Message with id, chat, date, the sender, and text() (or caption() for media) plus entities(). Media sends return a Message the same way.

Options

.disable_notification(), .protect_content(), .reply_to(..), .message_thread_id(..), .schedule_date(..) and .reply_markup(..) chain on the request before awaiting. Keyboards are on Keyboards.

Common mistakes

  • Sending <b>hi</b> without .html() shows the tags. A mode is needed for markup, and Disabled is the default.
  • Formatting user text with HTML tags but not escaping it: one & breaks every tag after it. Run it through escape_html.
  • Using reply where a plain message belongs: it points at the previous message, which groups show as a quote.
  • Quoting text that differs from the replied message by one character: Telegram answers with an error, not a trimmed quote.
  • Reading text() on a photo message: media keeps its text in caption(), and text() is empty there.
  • Deleting an empty list or more than 100 ids: awaiting fails with Error::InvalidArgument.

See also