botkit icon indicating copy to clipboard operation
botkit copied to clipboard

User ID in send middleware with the Web adapter

Open cbouvard opened this issue 3 years ago • 7 comments

When using a send middleware with the Web adapter, the user ID is not available in the message object during a conversation (i.e. Dialog). For instance, the middleware (bot, message, next) => { console.log(message); next(); } displays these data for a bot message:

{ type: 'message',
  text: 'typed!',
  inputHint: 'acceptingInput',
  channelData: {} }

A possible solution consists in extracting the user ID for the convo vars in the function makeOutgoing(dc, line, vars) of conversation.ts:

// copy user and channel from vars (for Web adapter)
if (!outgoing.user && vars.user) {
    outgoing.user = vars.user;
}

Then the message user can be leverage in the send middleware :tada:

{ type: 'message',
  text: 'typed!',
  inputHint: 'acceptingInput',
  channelData: {},
  user: '7a247882-bdab-c853-7175-f97d3978d663' // <= Yes!
}

Moreover, when a message is sent with bot.reply or bot.say, the user ID is not present in the traditional (legacy?) messager.user.

{ type: 'message',
  text: 'Echo: pizza',
  // ...
  recipient: { id: '7a247882-bdab-c853-7175-f97d3978d663' },
  // ...
  user: '7a247882-bdab-c853-7175-f97d3978d663', // <= It would be great to have this
  // ...
}

It could be great to add it in the function ensureMessageFormat(message) of botworker.ts:

const activity = {
    type: message.type || 'message',
    text: message.text,
    // ...
    user: message.recipient && message.recipient.id, 
    // ...
};

Can these points be integrated in a future release of Botkit? How can i make myself useful to the code?

Thanks, Chris

Context:

  • Botkit version: 4.10.0
  • Messaging Platform: Web (botbuilder-adapter-web in version 1.0.9)
  • Node version: v10.15.1
  • Os: MacOS

cbouvard avatar Sep 11 '20 14:09 cbouvard

Can you please send these in as a pull request? And can you include some test cases when you do?

benbrown avatar Sep 21 '20 20:09 benbrown

Hi @benbrown!

I have just submitted the related tiny PR.

Finally I did not add user to the activity object in botworker.ts, to respect the Activity type. But the recipient field can be leverage in a send middleware to get the user.

So, with this fix, a send middleware like this can be done:

(bot, message, next) => {
    if (message && message.type === 'message') {
        const user =
            message.user ||
            (message.recipient && message.recipient.id);
        
        // => Store here the bot message for the given user
    }
    next();
}

cbouvard avatar Sep 24 '20 09:09 cbouvard

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 25 '20 00:11 stale[bot]

Hey!

I have just added a comment to the related PR (#2043) in order to remove the stale flag.

The code is working nicely in my chatbots powered by Botkit 🚀

I hope the next release of Botkit will integrate this PR.

Cheers, Chris

cbouvard avatar Nov 25 '20 09:11 cbouvard

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 11 '21 02:06 stale[bot]

🆙 I still hope the next release of Botkit will integrate the related PR (#2043)...

cbouvard avatar Jun 26 '21 13:06 cbouvard

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 09 '22 02:01 stale[bot]