botkit
botkit copied to clipboard
User ID in send middleware with the Web adapter
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
Can you please send these in as a pull request? And can you include some test cases when you do?
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();
}
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.
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
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.
🆙 I still hope the next release of Botkit will integrate the related PR (#2043)...
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.