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!
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.
#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.
We're also happy to announce a revamped Additionally, session now accepts a Another long-standing problem was the lack of support for RegExp or case-insensitive command matching. This is here now:
π Stable and safe session
ππΌ No more race conditions
β’οΈ Official database adapters are here!
@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
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
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
Telegram
and Context
:
editGeneralForumTopic
closeGeneralForumTopic
reopenGeneralForumTopic
hideGeneralForumTopic
unhideGeneralForumTopic
Context::sendChatAction
will automatically infer message_thread_id
for topic messages.
'this' Context of type 'NarrowedContext' is not assignable to method's 'this' of type 'Context<Update>'
.
β‘οΈ RegExp support for commands!
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
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);
});
bold(italic("telegraf"))
will now work as expected.