[英]node-telegram-bot-api / How to get user message on a question?
So, i'm actually tired to find any asks about that...所以,我真的很累找到任何关于这个的问题......
I need to get user message only after bot question and nowhere else like:我只需要在机器人问题之后才需要获取用户消息,而其他地方都没有:
bot: What is your name?
机器人:你叫什么名字?
user: Oleg
用户:奥列格
bot: Hi, Oleg
机器人:嗨,奥列格
I am also using require system with module.exports, so i'm really confused, how to deal with my problem我也在使用带有module.exports的require系统,所以我真的很困惑,如何处理我的问题
EXAMPLE CODE示例代码
const mw = require('./example_module');
bot.onText(/\/help/, async (data) => {
try {
mw.cout.userlog(data);
await cw.help.main(bot, data);
} catch (e) {
mw.cout.err(e.name)
}
});
you can do that with a database or just a JSON file by storing a user state property.您可以通过存储用户 state 属性来使用数据库或仅使用 JSON 文件来执行此操作。 For example, here you are asking the user their name.
例如,在这里您要询问用户的姓名。 And you can set the state property for the user in your DB that "setName".
您可以为数据库中“setName”的用户设置 state 属性。 And when the user replies, check DB and find what was the last state.
当用户回复时,检查 DB 并找到最后的 state 是什么。 Here we have set the state to "setName".
在这里,我们将 state 设置为“setName”。 Then do the rest.
然后做rest。
Otherwise, just with the node-telegram-bot-api
, you can do this but a slight difference is that you have to receive their name as a reply text.否则,只需使用
node-telegram-bot-api
,您就可以做到这一点,但略有不同的是,您必须接收他们的名字作为回复文本。 Here's the code:这是代码:
bot.onText(/\/help/, async msg => {
const namePrompt = await bot.sendMessage(msg.chat.id, "Hi, what's your name?", {
reply_markup: {
force_reply: true,
},
});
bot.onReplyToMessage(msg.chat.id, namePrompt.message_id, async (nameMsg) => {
const name = nameMsg.text;
// save name in DB if you want to ...
await bot.sendMessage(msg.chat.id, `Hello ${name}!`);
});
});
And that's it.就是这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.