I am trying to create many scenes with an inline keyboard and the ability of transitioning from one scene to another after selecting an option.
I have an error with the examId not been a property of session.
Error: Property 'examId' does not exist on type 'SceneSession<MySessionData>'.ts(2339)
The only prorperty of session is __scenes
import { Telegraf, Scenes, session, Context, Markup } from 'telegraf';
// Define scene constants
const SCENES = {
EXAMS: 'exams-scene',
YEARS: 'years-scene',
} as const;
// Extend SceneSessionData to include custom fields
interface MySessionData extends Scenes.SceneSessionData {
examId?: number;
year?: string;
subject?: string;
paper?: string;
currentPage?: number;
lastActivity?: number;
}
interface MyContext extends Scenes.SceneContext<MySessionData> {
session: Scenes.SceneSession<MySessionData>;
scene: Scenes.SceneContextScene<MyContext, MySessionData>;
}
// Initialize scenes
const examsScene = new Scenes.BaseScene<MyContext>(SCENES.EXAMS);
const yearsScene = new Scenes.BaseScene<MyContext>(SCENES.YEARS);
// Exam selection scene
examsScene.enter(async (ctx) => {
const exams = [
{ id: 1, title: 'GCE O-Level' },
{ id: 2, title: 'GCE A-Level, BACALUREATE' },
{ id: 3, title: 'IGCSE' },
{ id: 4, title: 'SAT' },
];
const keyboard = Markup.inlineKeyboard([
...exams.map((exam) => [
Markup.button.callback(exam.title, `select_exam:${exam.id}`),
]),
[Markup.button.callback('« Main Menu', 'main_menu')],
]);
await ctx.reply('Select an exam:', keyboard);
});
// Handle exam selection
examsScene.action(/^select_exam:(\d+)$/, async (ctx) => {
ctx.session.examId = parseInt(ctx.match[1], 10); // Store examId in the session
await ctx.reply(`You selected exam ID: ${ctx.session.examId}`);
await ctx.scene.enter(SCENES.YEARS); // Proceed to the next scene
});
I am trying to create many scenes with an inline keyboard and the ability of transitioning from one scene to another after selecting an option.
I have an error with the examId not been a property of session.
Error: Property 'examId' does not exist on type 'SceneSession<MySessionData>'.ts(2339)
The only prorperty of session is __scenes
import { Telegraf, Scenes, session, Context, Markup } from 'telegraf';
// Define scene constants
const SCENES = {
EXAMS: 'exams-scene',
YEARS: 'years-scene',
} as const;
// Extend SceneSessionData to include custom fields
interface MySessionData extends Scenes.SceneSessionData {
examId?: number;
year?: string;
subject?: string;
paper?: string;
currentPage?: number;
lastActivity?: number;
}
interface MyContext extends Scenes.SceneContext<MySessionData> {
session: Scenes.SceneSession<MySessionData>;
scene: Scenes.SceneContextScene<MyContext, MySessionData>;
}
// Initialize scenes
const examsScene = new Scenes.BaseScene<MyContext>(SCENES.EXAMS);
const yearsScene = new Scenes.BaseScene<MyContext>(SCENES.YEARS);
// Exam selection scene
examsScene.enter(async (ctx) => {
const exams = [
{ id: 1, title: 'GCE O-Level' },
{ id: 2, title: 'GCE A-Level, BACALUREATE' },
{ id: 3, title: 'IGCSE' },
{ id: 4, title: 'SAT' },
];
const keyboard = Markup.inlineKeyboard([
...exams.map((exam) => [
Markup.button.callback(exam.title, `select_exam:${exam.id}`),
]),
[Markup.button.callback('« Main Menu', 'main_menu')],
]);
await ctx.reply('Select an exam:', keyboard);
});
// Handle exam selection
examsScene.action(/^select_exam:(\d+)$/, async (ctx) => {
ctx.session.examId = parseInt(ctx.match[1], 10); // Store examId in the session
await ctx.reply(`You selected exam ID: ${ctx.session.examId}`);
await ctx.scene.enter(SCENES.YEARS); // Proceed to the next scene
});
Share
Improve this question
asked Jan 20 at 17:01
Yagi91Yagi91
537 bronze badges
1 Answer
Reset to default -1I found this article very helpful for the same issue