简体   繁体   English

node-telegram-bot-api / 如何获取有关问题的用户消息?

[英]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机器人:嗨,奥列格

how it should work它应该如何工作

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.

相关问题 如何使用 node-telegram-bot-api 获取 Telegram 中上一条消息的消息 ID? - How to get the message id of previous message in Telegram using node-telegram-bot-api? 如何使用 Node.js node-telegram-bot-api 从电报机器人订阅和取消订阅用户? - How to subscribe and unsubscribe a user from a telegram bot using Node.js node-telegram-bot-api? 通过`node-telegram-bot-api`模块接收用户消息 - Receive user Messages via `node-telegram-bot-api` module node-telegram-bot-api 中的消息延迟 - Delay for messages in node-telegram-bot-api 如何使用node-telegram-bot-api为每个用户执行相同的代码? - How to execute same code for each user with node-telegram-bot-api? 如何在 node-telegram-bot-api 中仅删除 bot.on() function? - How to remove in node-telegram-bot-api only the bot.on() function? 如果我使用 node-telegram-bot-api 创建电报机器人对象,为什么对话流机器人会停止在电报中响应? - Why dialogflow bot stop responding in telegram if i create a telegram bot object with node-telegram-bot-api? 使用node-telegram-bot-api顺序执行功能 - Sequential execution of functions with node-telegram-bot-api node-telegram-bot-api: CHAT_ADMIN_REQUIRED - node-telegram-bot-api: CHAT_ADMIN_REQUIRED 使用Express,Socket.io和Node-Telegram-Bot-Api结束Mocha测试 - Ending Mocha Test Using Express, Socket.io and Node-Telegram-Bot-Api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM