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

Guide

Migration 0.3 to 0.4

Migration 0.3 to 0.4

Every user-visible change from vaegram 0.3.0 to 0.4.0-nightly.2, with the old code and its replacement.

Most 0.3.0 code compiles unchanged. What follows is what does not compile, plus the defaults and behaviour that changed under code that still does.

Example

The shutdown every 0.4.0 service needs. Only the close line is new.

bot.run_until_signal().await?; // stop on Ctrl-C or SIGTERM
bot.close().await;             // wait for the final save

Handlers did not change

on_message and the rest still take Fn(T), and the client rides inside the update: msg.answer(..), query.answer(). The first nightly (0.4.0-nightly.1) passed the client as a first argument; nightly.2 reverted that.

// 0.4.0-nightly.1 only:
app.on_message(filter, |client, msg| async move { ... });

// 0.3.0 and 0.4.0-nightly.2:
app.on_message(filter, |msg| async move { msg.answer("hi").await?; Ok(()) });

If you wrote against nightly.1, drop the extra parameter; nothing else changes. Client::builder(name, api_id, api_hash) is unchanged, and .bot_api(token) is the new way to switch that client to the HTTP Bot API.

Client lifecycle

close().await is new: it stops the client and waits for the final save without blocking a runtime thread, once run has returned (called during run, it leaves the save and close to run as it returns). disconnect() still blocks the thread it runs on for up to 10 seconds. Dropping the last clone now saves in the background.

// 0.3.0, on a runtime thread: disconnect() blocked that thread for the final save
client.disconnect();
client.run_until_signal().await?; // stop on Ctrl-C or SIGTERM
client.close().await;             // wait for the final save

Returning from main waits for a save a drop started; std::process::exit and Runtime::shutdown_background do not, so await close before either.

User accounts are rate-limited by default

A 0.3.0 user account had no client-side limits. 0.4.0 has no unlimited preset. A bot that leaves rate_limits unset gets no limiter; an account that is not a bot gets RateLimits::user_account(), about a person’s pace, a message a second per chat and 20 a minute in a group, and nothing switches that off. RateLimits::default() is no way back either: it is the bots’ pace, 20 messages a second. The closest to 0.3.0 is a high finite rate in the categories you care about, since limit refuses a rate that is not finite and above 0, with the per-chat limits off:

use vaegram::prelude::*;
use vaegram::session::{RateCategory, RateLimits};

let _app = Client::builder("me", 12345, "0123456789abcdef0123456789abcdef")
    .rate_limits(
        RateLimits::user_account()
            .no_per_chat()
            .limit(RateCategory::Message, 1000.0, 1000.0),
    );

That pace from a person risks a ban. Prefer RateLimits::user_account() with the few categories you need loosened; see Flood limits.

Behaviour that changed without breaking the build

  • A command name is 1 to 32 letters, digits or underscores. /usr/bin/python3: or /start: no longer counts as a command.
  • send_reaction adds the reaction to recent by default. .add_to_recent(false) restores the 0.3.0 behaviour.
  • The update state is saved within about 15 seconds of moving (10 seconds, then the storage’s flush within 5), so a hard kill replays about the last 15 seconds of updates on the next run. A handler that must not act twice should check.

Changed signatures

delete_messages is a request builder, and revoke(false) is new:

client.delete_messages(chat, &[10, 11]).await?;                 // for everyone, as before
client.delete_messages(chat, &[10, 11]).revoke(false).await?;   // only for this account

CallbackQuery::answer_ephemeral returns the builder, not Result: drop the ?.

// 0.3.0
query.answer_ephemeral("Only you see this")?.await?;
query.answer_ephemeral("Only you see this").await?;

get_app_config returns AppConfig instead of raw Json; get(key) and json() read it, and get_config is new and typed.

let config = client.get_app_config().await?;
let _limit = config.get("reactions_user_max");

transcribe_audio returns Transcription: it derefs to the old TranscribedAudio, and wait(timeout) gets the final text.

use std::time::Duration;

let mut transcription = client.transcribe_audio(chat, 9).await?;
let text = transcription.wait(Duration::from_secs(30)).await?;

get_sticker_set, reply_sticker and answer_sticker take a reference, not only a short name; a &str still works.

let set = client.get_sticker_set("cats_by_bot").await?;
let again = client.get_sticker_set(set.set_ref()).await?;

RichBlock::expandable_blockquote takes the blocks the quote contains, not one RichText, and the RichBlockKind::ExpandableBlockQuote variant it reads back now carries blocks where it carried text.

// 0.3.0
RichBlock::expandable_blockquote("one line")
// 0.4.0
RichBlock::expandable_blockquote([RichBlock::paragraph("one line")])

Types that changed traits

  • ChatAction is no longer Copy: its emoji-interaction variants carry text. Clone it when it is used twice: client.send_chat_action(chat, action.clone()).await?;
  • Sticker is #[non_exhaustive]: a struct literal or an exhaustive match no longer compiles; read its fields.

Renamed and retyped fields

Two User fields take their Bot API names: has_topics_sidebar is now has_topic_sidebar, and inline_needs_location is now needs_inline_query_location. The values are unchanged, as are the Bot API fields they mirror.

// 0.3.0
let sidebar = user.has_topics_sidebar;
let location = user.inline_needs_location;
// 0.4.0
let sidebar = user.has_topic_sidebar;
let location = user.needs_inline_query_location;

TransactionPartner::AffiliateProgram’s sponsor_user is Option<User>, not User: an affiliate commission can arrive with no sponsoring bot named, and 0.3.0 had no way to say so. Match or unwrap it like any other optional field.

Sessions and storage need no action

The .session file format and the VAE_ session-string format are unchanged from 0.3.0, so both load as they are. Two operational differences:

  • PostgresStorage now holds a lock on its schema: a second client on the same schema gets ResourceBusy until the first closes.
  • In Bot API mode (bot_api) nothing is stored: peers stay in memory, and api_id and api_hash are unused.

New Cargo features, all off by default

vaegram = { git = "https://github.com/rjriajul/vaegram", branch = "nightly", features = [
    "bot-api",        # talk to the HTTP Bot API through the same client
    "webhook-server", # the built-in webhook receiver
    "macros",         # #[derive(BotCommands)]
    "dialogue-json",  # dialogue states in a JSON file
    "serde",          # Serialize on received types
] }

None changes code that does not use it; macros pulls in the new vaegram-macros crate.

Additions that need no change

Router, middleware and Dep; DialogueRouter; Conversation; #[derive(BotCommands)]; the test kit (Client::offline, recording, scripted); Client::webhook with set_webhook, delete_webhook and get_webhook_info; seven new Error::is_* helpers and From<RpcError> for Error. Error and ClientEvent were already #[non_exhaustive], so new variants break no match.

Common mistakes

  • Copying a nightly.1 handler signature (|client, msg|) into nightly.2, where it takes |msg|.
  • Leaving the ? on answer_ephemeral, which now returns a builder.
  • Calling disconnect() and exiting at once, then losing the final save; await close() instead.
  • Upgrading a user account and finding sends suddenly paced. That is the new default.
  • Running an MTProto bot that ran on the cloud Bot API before. Call logOut there once; until then Telegram keeps its updates on the Bot API server and the MTProto login receives none.

See also