简体   繁体   中英

Is it possible to maintain user sessions for facebook messenger bot?

I'm working on a facebook messenger bot, the webhook is written in node.js which listens for messages from user and responds back, the flow of conversation is guided and structured in modules. Each module has a module name and bot-statement and a reference of a child modules. Forex ample there is a welcome module with the statement "Hello I'm great bot i can offer following informations 1:- Movies 2:-Music" now the welcome module has the references of two child modules Movies and Music, the conversation has to progress in one of these two directions based on user response, for example if the user responds Movies, the bot will pick up the module statement from the movies module and send back to facebook , so the reply to user for movies will be like "exciting, what kind of movies do you want to know about? action, adventure, sci-fi" and again the user responds back with the thing he wants to know about and the bot picks up the module statement and send back to the user. now this all works good but the problem here is, if there are two users say User A and User B. user A says Hi, bot says welcome what you want to know movies or music? user responds Movies and the bot throws what kind of movies action adventure or sci-fi, but then there comes a user B who says hi so the bot responds with same current module that is for user A. here is the code

var sender;
var ModID=0;
exports.Facebook_bot = function(req, res) {
// console.log(req.body);
var messaging_events = req.body.entry[0].messaging;
// console.log(messaging_events);
for (var i = 0; i < messaging_events.length; i++) {
  var event = messaging_events[i];
  sender = event.sender.id;
  //  console.log('this is event',event);
  if (event.message && event.message.text) {
    var msg = event.message.text;
    console.log( "this is recived text",msg);
    // sendText(sender, "text echo" + msg.substring(0, 100));
  }
}
res.send(200);

if (ModID == 0) {
  ModID = "71072";
  botSchema.findOne({ Bot_name: "My Bot" }, function(err, data) {
    for (var i = 0; i < data.Modules.length; i++) {
      var back = data.Modules[i].ModuleResponse;

      if (data.Modules[i].ModuleID === ModID) {
        for (var j = 0; j < back.length; j++) {
          console.log("back", back[j].Response);

          if (back[j].Response === msg || back[j].Response === "-input-") {
            //got to module ModID
            ModID = back[j].TransBotID;
            // console.log("else",ModID);
            // console.log(back[j]);
          }
        }
      }
    }
  }).then(function(data) {
    for (var i = 0; i < data.Modules.length; i++) {
      if (data.Modules[i].ModuleID === ModID) {
        sendText(
          sender,
          data.Modules[i].ModuleStatement.substring(0, 500)
        );
        sendUI(sender,data.Modules[i].FBUI) 
        console.log(data.Modules[i].FBUI);

      } else if (ModID === null) {
        console.log(false);
      }
    }
  });;
} else {
  botSchema
    .findOne({ Bot_name: "HIA Airport" }, function(err, data) {
      // console.log(data)
      for (var i = 0; i < data.Modules.length; i++) {
        var back = data.Modules[i].ModuleResponse;
        // console.log(back);
        if (data.Modules[i].ModuleID === ModID && back != null) {
          for (var j = 0; j < back.length; j++) {
            console.log("back 2", back[j].Response);

            if (back[j].Response === msg || back[j].Response === "-input-") {
              ModID = back[j].TransBotID;
              console.log("Go to :", ModID);
            }
          }
        }
      }
    })
    .then(function(data) {
      for (var i = 0; i < data.Modules.length; i++) {
        if (data.Modules[i].ModuleID === ModID) {
          sendText(
            sender,
            data.Modules[i].ModuleStatement.substring(0, 500)
          );
          console.log(data.Modules[i].ModuleStatement);
        } else if (ModID === null) {
          console.log(false);
        }
      }
    });
}
//end if

}; the variables sender and ModID are defined globally i want it to be defined seperately for each new user so that the state of conversation for user B is not dependent on state of conversation for user B, how can i do it? I hope it makes sense, please do ask if you need to know anything further.

You should block scope sender and then pass it from module to module. That will ensure each incoming PSID will follow its own path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM