可以获取“文字”来自Nestjs-Telegraf上下文消息的字段

发布于 2025-02-03 01:36:03 字数 1488 浏览 3 评论 0原文

我正在使用Nestjs-Telegraf包装器编写电报的机器人。我想通过电报发送消息文本。这是我的处理程序:

  @On('text')
  async onText(@Ctx() ctx: Context) {
    const msg = ctx.message;
    const update = ctx.update;
    const from = msg.from;
    const chat = msg.chat;
    const date = msg.date;
    // **const text = msg.text;**

    // this.logger.debug('Context - ' + JSON.stringify(ctx));
    this.logger.debug('update - ' + JSON.stringify(update));
    this.logger.debug('text message - ' + JSON.stringify(msg));
    this.logger.debug(`message id ${msg.message_id}`);
    this.logger.debug(`from id ${from.id}`);
    this.logger.debug('from ' + JSON.stringify(from));
    this.logger.debug('chat ' + JSON.stringify(chat));
    this.logger.debug('date ' + JSON.stringify(date));
    // **this.logger.debug('text ' + JSON.stringify(text));**

    ctx.reply('You send ' + msg);
  }

如果我尝试构建代码删除在行上的评论,

const text = msg.text;

我会收到错误:属性“文本”不存在于type'&非通道&信息。 编译

错误:

[Nest] 148003  - 31/05/2022, 12:36:28   DEBUG [BotService] text message - {"message_id":361,"from":{...},"chat":{..."},"date":1653992754,"text":"this is a message text"}

文本属性存在并包含我从电报发送的数据。如果我以调试模式运行代码,就足够了,我可以看到正确填充文本属性的对象。谁能给我一些最湿的来编译代码并提取文本?

谢谢

在调试中运行代码

I'm writing a telegram's bot using nestjs-telegraf wrapper. I want to get message text sent by telegram. This is my handler:

  @On('text')
  async onText(@Ctx() ctx: Context) {
    const msg = ctx.message;
    const update = ctx.update;
    const from = msg.from;
    const chat = msg.chat;
    const date = msg.date;
    // **const text = msg.text;**

    // this.logger.debug('Context - ' + JSON.stringify(ctx));
    this.logger.debug('update - ' + JSON.stringify(update));
    this.logger.debug('text message - ' + JSON.stringify(msg));
    this.logger.debug(`message id ${msg.message_id}`);
    this.logger.debug(`from id ${from.id}`);
    this.logger.debug('from ' + JSON.stringify(from));
    this.logger.debug('chat ' + JSON.stringify(chat));
    this.logger.debug('date ' + JSON.stringify(date));
    // **this.logger.debug('text ' + JSON.stringify(text));**

    ctx.reply('You send ' + msg);
  }

If I try to build the code removing the comment on line

const text = msg.text;

I got the error: Property 'text' does not exist on type 'New & NonChannel & Message.
compilation error

But, if I run the code with the line commented everithing works and I got this log:

[Nest] 148003  - 31/05/2022, 12:36:28   DEBUG [BotService] text message - {"message_id":361,"from":{...},"chat":{..."},"date":1653992754,"text":"this is a message text"}

where the text property exists and contains the data I sent from telegram. Enough If I run code in debug mode, I can see the object with the text property filled correctly. Can anyone give me some soggest to compile the code and extract the text?

Thanks

running code in debug

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

口干舌燥 2025-02-10 01:36:03

这对我有用:

@WizardStep(1)
  async password(
    @Ctx() ctx: Scenes.WizardContext & SessionContext,
    @Message('text') msg: string,
  ) {
    const isEmail = await this.checkEmail(msg);

    if (!isEmail) {
      await ctx.reply('Введенная почта некорректна, попробуйте еще раз');
      return;
    }

    ctx.session.email = msg;
    await ctx.reply('Введите пароль:');
    ctx.wizard.next();
  }

只需添加@message('text)msg:字符串到params。
希望它有帮助

This worked for me:

@WizardStep(1)
  async password(
    @Ctx() ctx: Scenes.WizardContext & SessionContext,
    @Message('text') msg: string,
  ) {
    const isEmail = await this.checkEmail(msg);

    if (!isEmail) {
      await ctx.reply('Введенная почта некорректна, попробуйте еще раз');
      return;
    }

    ctx.session.email = msg;
    await ctx.reply('Введите пароль:');
    ctx.wizard.next();
  }

Just add @Message('text) msg: string to params.
Hope it helps

望喜 2025-02-10 01:36:03

如此讨论,typescript试图告诉您,不是每一个来自的消息,用户是短信。即使您只听“文本”更新,上下文也必须范围缩小到您正在等待的特定消息类型。

此解决方法对我有用:

如果我在等待短信,

@Hears('hello')
  async sayHello(@Ctx() ctx: CustomContext & { message: Message.TextMessage }) {}

如果我在等待位置,则

@On('location')
  async locate(
    @Ctx() ctx: CustomContext & { message: Message.LocationMessage },
  ) {}

如果您的位置混合,

@Ctx() ctx: CustomContext & { message: Message.LocationMessage & Message.TextMessage}

则您可以上下文而不是customcontext。这是我使用类型会话商店创建的自定义上下文。
您可以查看此文章github.com/feathers-studio/telegraf-docs/tree/master/examples“ rel =“ nofollow noreferrer”> repo 示例以了解更多信息

As mentioned in this discussion, typescript is trying to tell you that not every message coming from a user is a text message. Even if you are listening only 'text' updates, Context must be narrowed to specific message types you are waiting for.

This workaround worked for me:

If I'm waiting for text message

@Hears('hello')
  async sayHello(@Ctx() ctx: CustomContext & { message: Message.TextMessage }) {}

If I am waiting for example location

@On('location')
  async locate(
    @Ctx() ctx: CustomContext & { message: Message.LocationMessage },
  ) {}

if mixed

@Ctx() ctx: CustomContext & { message: Message.LocationMessage & Message.TextMessage}

You can you Context instead of CustomContext. It is my created custom context with a typed session store.
You can check out this article or Telegraf repo with examples to find out more

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文