Context
- Telegraf.js Version: 4.16.3
- Node.js Version: v22.11.0
- Operating System: Win 10
Minimal Example Code Reproducing the Issue
import {session, Telegraf} from "telegraf";
const bot = new Telegraf(process.env.BOT_TOKEN)
bot.use(session());
bot.command("start", async (ctx) => {
console.log(ctx.session); //undefined
ctx.session.count = 0; // TypeError: Cannot set properties of undefined (setting 'count')
await ctx.reply("Hello, I am a bot")
});
bot.launch();
Using ctx.session without defining a defaultSession like
bot.use( session({ defaultSession: () => ({}) }) );
Expected Behavior
I expected that session is not undefined. Also the docs don't say anything about having to define a defaultSession. For me reading through the examples, it looks like session should be able to be used without having to define a defaultSession.
Not really a big deal but I think a disclaimer or something in the docs would make it clear, that you have to define a defaultSession.
The docs
API Reference
Current Behavior
Crashing with TypeError because ctx.session is undefined.
Error Message and Logs (export DEBUG='telegraf:*')
TypeError: Cannot set properties of undefined (setting 'count')
at file:///C:/Users/torben/Desktop/telegraf-session-bug/main.js:8:23
at C:\Users\torben\Desktop\telegraf-session-bug\node_modules\.pnpm\telegraf@4.16.3\node_modules\telegraf\lib\composer.js:397:28
at C:\Users\torben\Desktop\telegraf-session-bug\node_modules\.pnpm\telegraf@4.16.3\node_modules\telegraf\lib\composer.js:165:111
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async execute (C:\Users\torben\Desktop\telegraf-session-bug\node_modules\.pnpm\telegraf@4.16.3\node_modules\telegraf\lib\composer.js:518:17)
at async C:\Users\torben\Desktop\telegraf-session-bug\node_modules\.pnpm\telegraf@4.16.3\node_modules\telegraf\lib\composer.js:519:21
at async execute (C:\Users\torben\Desktop\telegraf-session-bug\node_modules\.pnpm\telegraf@4.16.3\node_modules\telegraf\lib\composer.js:518:17)
at async C:\Users\torben\Desktop\telegraf-session-bug\node_modules\.pnpm\telegraf@4.16.3\node_modules\telegraf\lib\composer.js:519:21
at async C:\Users\torben\Desktop\telegraf-session-bug\node_modules\.pnpm\telegraf@4.16.3\node_modules\telegraf\lib\session.js:104:13
at async execute (C:\Users\torben\Desktop\telegraf-session-bug\node_modules\.pnpm\telegraf@4.16.3\node_modules\telegraf\lib\composer.js:518:17)
Context
Minimal Example Code Reproducing the Issue
Using
ctx.sessionwithout defining adefaultSessionlikebot.use( session({ defaultSession: () => ({}) }) );Expected Behavior
I expected that session is not undefined. Also the docs don't say anything about having to define a
defaultSession. For me reading through the examples, it looks like session should be able to be used without having to define a defaultSession.Not really a big deal but I think a disclaimer or something in the docs would make it clear, that you have to define a defaultSession.
The docs
API Reference
Current Behavior
Crashing with TypeError because
ctx.sessionis undefined.Error Message and Logs (
export DEBUG='telegraf:*')