可以获取“文字”来自Nestjs-Telegraf上下文消息的字段
我正在使用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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我有用:
只需添加
@message('text)msg:字符串
到params。希望它有帮助
This worked for me:
Just add
@Message('text) msg: string
to params.Hope it helps
如此讨论,typescript试图告诉您,不是每一个来自的消息,用户是短信。即使您只听“文本”更新,上下文也必须范围缩小到您正在等待的特定消息类型。
此解决方法对我有用:
如果我在等待短信,
如果我在等待位置,则
如果您的位置混合,
则您可以
上下文
而不是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
If I am waiting for example location
if mixed
You can you
Context
instead ofCustomContext
. 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