最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to fix issues with ctx when transitioning scenes in Telegraf and typescript? - Stack Overflow

programmeradmin2浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default -1

I found this article very helpful for the same issue

发布评论

评论列表(0)

  1. 暂无评论