npm telegraf 4.12.0
v4.12.0

latest releases: 4.16.3, 4.16.2, 4.16.1...
18 months ago

Normally the most exciting features of a new release would be support for the latest Bot API. But in this update, it's session! This has been in the works for many months, and we're happy to bring it to you this release!

πŸ”’ Stable and safe session

Some of you may know that builtin session has been deprecated for quite a while. This was motivated by the fact that session is prone to race-conditions (#1372). This left the community in a grey area where they continued to use session despite the deprecation, since no clear alternative was provided. Added to this was the fact that there were no official database-backed sessions, and all unofficial async session middleware were affected by #1372.

This release finally addresses both of these long-running issues.

πŸƒπŸΌ No more race conditions

#1713 provides a reference-counted implementation resistant to race conditions. Session is now no longer deprecated, and can be used safely!

Note: You should read more about how to safely use session in the docs repo.

ℒ️ Official database adapters are here!

We're also happy to announce a revamped @telegraf/sessionβ€”this provides official store implementations for database-backed sessions via Redis, MongoDB, MySQL, MariaDB, PostgreSQL, and SQLite. Just install the drivers necessary for your database, and off you go! Since this package now only provides a store implementation, it's usable with builtin session, and effectively makes all implementations have the same safety as the core package. Check it out!

πŸ†— Default session

Additionally, session now accepts a defaultSession parameter. You no longer need a hacky middleware to do ctx.session ??= { count }.

// 🀒 Old way
bot.use(session());
bot.use((ctx, next) => {
  ctx.session ??= { count: 0 };
  return next();
});

// 😁 New way βœ…
bot.use(session({ defaultSession: () => ({ count: 0 }) }));
πŸ”Ί Bot API 6.5 support
  • Updated Typegram, added the following Markup.button helpers to request a user or chat:
  • Markup.button.userRequest
  • Markup.button.botRequest
  • Markup.button.groupRequest
  • Markup.button.channelRequest
  • Telegram::setChatPermissions and Context::setChatPermissions accept a new parameter for { use_independent_chat_permissions?: boolean } as documented in the API.
πŸ”Ί Bot API 6.4 support
  • Updated Typegram, added the following new methods to class Telegram and Context:
  • editGeneralForumTopic
  • closeGeneralForumTopic
  • reopenGeneralForumTopic
  • hideGeneralForumTopic
  • unhideGeneralForumTopic
  • Context::sendChatAction will automatically infer message_thread_id for topic messages.
  • Fix for 'this' Context of type 'NarrowedContext' is not assignable to method's 'this' of type 'Context<Update>'.
⚑️ RegExp support for commands!

Another long-standing problem was the lack of support for RegExp or case-insensitive command matching. This is here now:

bot.command("hello", ctx => ctx.reply("You sent a case-sensitive /hello"));
bot.command(/^hello$/i, ctx => ctx.reply("You sent a case-insensitive /hELLo"));
✍️ fmt helpers
  • New join fmt helper to combine dynamic arrays into a single FmtString.
import { fmt, bold, join } from "telegraf/format";

// elsewhere
bot.command("/fruits", async ctx => {
  const array = ["Oranges", "Apples", "Grapes"];
  const fruitList = join(array.map(fruit => bold(fruit)), "\n");
  const msg = fmt`Fruits to buy:\n${fruitList}`;
  await ctx.sendMessage(msg);
});
  • Fixed various bugs in fmt helpers, so things like bold(italic("telegraf")) will now work as expected.

Don't miss a new telegraf release

NewReleases is sending notifications on new releases.