简体   繁体   中英

Microsoft Bot Framework: Same name across sessions

I have the following code which attempts to set the user's name during a chat session. Eg if the user writes the message "Hi, my name is Bob" , the bot will respond by greeting the user with his name, but only if the name was labeled as a "name" entity.

The issue is that the name is globally set for every user that is currently chatting with the bot and not for the current user only. The bot will, in other words, call every user currently chatting Bob in this case.

var intents = new builder.IntentDialog({ recognizers: [recognizer] })

    // Check if the message user sent matches LUIS intent "greetingsWithName"
    .matches('greetingsWithName', (session, args) => {
        // Attempt to set content of LUIS entity "name" to variable "name" 
        name = builder.EntityRecognizer.findEntity(args.entities, 'navn');

        // Check whether the name was successfully set or not
        if (!name) {
            session.send('Sorry, I didn't catch your name.')');
        } else {
        session.userData.userName = name.entity;
        session.send('Hi, ' + session.userData.userName);
    });

Later on I check in code whether the user has given his name to the bot or not during the session. If so, then say goodbye to the user with his name:

.matches('goodBye', (session) => {
    if (!session.userData.userName) {
        session.send('Good bye!');
    } else {
        session.send('Good bye, ' + session.userData.userName);
    }
})

bot.dialog('/', intents);

Here is the HTML code from the file that initiates the web chat session:

<!DOCTYPE html>
<html>
  <head>
    <link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
  </head>
  <body>
    <div id="bot"/>
    <script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
    <script>
      BotChat.App({
        directLine: { secret: direct_line_secret },
        user: { id: 'userid' },
        bot: { id: 'botid' },
        resize: 'detect'
      }, document.getElementById("bot"));
    </script>
  </body>
</html>

You are storing the name of the user in userData , which is logical.

But in your tester, you set the same user id for every webchat user:

user: { id: 'userid' }

So every user will point to the same userData as the key is a combination of the channel name and the user id ( activity.ChannelId , activity.From.Id in C#).

You can avoid this by generating unique identifiers for every webchat user, for example using https://www.npmjs.com/package/uuid-random

In that case, user: { id: uuid() } .

Moreover if you need to set a display name, add name: '...' to the definition of user: user: { id: uuid(), name: 'You' } .

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