我正在使用 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;
我收到错误:“New & NonChannel & Message”类型上不存在属性“text”。
但是,如果我运行带有注释行的代码,则一切正常,并且我得到以下日志:
[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"}
其中文本属性存在并包含我从电报发送的数据。足够了如果我在调试模式下运行代码,我可以看到文本属性正确填充的对象。谁能给我一些soggest来编译代码并提取文本?
谢谢
这对我有用:
@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: string
添加到参数即可。
希望有帮助