如何跟踪用户对node.js中特定chatbot消息的回复

我不知道如何赶上一个特定的聊天问题的用户回复? 我的意思是,例如,如果用户询问chatbot的天气,chatbot通过询问用户在哪个城市回应。 然后我想跟踪用户对这个问题的反应。 这样城市就可以用来呼叫城市的天气了。 我不知道如何跟踪用户对这个问题的答复。 有人知道这是可能的吗?

为了让多个用户同时访问聊天机器人,最好跟踪每个用户以及每个用户的对话状态。 在Messenger API的情况下,这将是:

const users = {} const nextStates = { 'What country are you in?': 'What city are you in?', 'What city are you in?': 'Let me look up the weather for that city...' } const receivedMessage = (event) => { // keep track of each user by their senderId const senderId = event.sender.id if (!users[senderId].currentState){ // set the initial state users[senderId].currentState = 'What country are you in?' } else { // store the answer and update the state users[senderId][users[senderId].currentState] = event.message.text users[senderId].currentState = nextStates[users[senderId.currentState]] } // send a message to the user via the Messenger API sendTextMessage(senderId, users[senderId].currentState) } 

然后,您将获得存储在users对象中的每个用户的答案。 您也可以使用数据库来存储这个。

当chatbot提出问题时,我通过设置一个全局variables来解决它

 global.variable = 1; 

当用户回复传入的文本消息事件被触发,我可以检查是否设置了全局标志。 这表明这是问题提出后的用户回复。 然后,我可以从该消息中获取消息文本城市。 这在我的情况很好,但如果有人知道更好的select,请让我知道