botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

[Question] Unable to strip @mentions

Open briwilcox opened this issue 8 years ago • 1 comments

Bot Info

  • Node.js
  • "3.13.1",
  • Teams
  • Azure Bot Service

Issue Description

Trying to follow the documentation here: https://docs.microsoft.com/en-us/microsoftteams/platform/scenarios/bots-channel-conversations#mentions

I also tried stripping the @mention using the only code I could find regarding the BotBuilder-MicrosoftTeams plugin here: https://github.com/OfficeDev/BotBuilder-MicrosoftTeams/blob/master/Node/samples/app.js

Code Example

const builder = require('botbuilder');
const restify = require('restify');
const cognitiveServices = require('botbuilder-cognitiveservices');
var teams = require("botbuilder-teams");

var stripBotAtMentions = new teams.StripBotAtMentions();
bot.use(stripBotAtMentions);


...


bot.on('conversationUpdate', function (message) {
    console.log(message);
    var text = message.text;
    if (message.entities) {
        message.entities
            .filter(entity => ((entity.type === "mention") && (entity.mentioned.id.toLowerCase() === botId)))
            .forEach(entity => {
                text = text.replace(entity.text, "");
            });
        text = text.trim();
    }

var event = teams.TeamsMessage.getConversationUpdateData(text);
});

bot.dialog('/', qnaMakerDialog);

Reproduction Steps

  1. Message bot 1on1, works fine
  2. Message bot in a teams channel, fails to understand

Expected Behavior

Mentions to be stripped allowing bot to understand incoming text.

Actual Results

Mentions are not stripped.

briwilcox avatar Feb 08 '18 01:02 briwilcox

Thank you for bringing this to our attention. The documentation appears to be incorrect. I'll notify the docs team, so we can get that updated. It should actually be message.address.bot.id.toLowerCase(); ... instead of botId.

I'm not sure why the botbuilder-teams npm package's .StripBotAtMentions didn't work for you. I just tried v0.1.7 and it successfully removed the mentions from teams and a qna maker bot works as expected. The source code can be found here: https://github.com/OfficeDev/BotBuilder-MicrosoftTeams/blob/2593ffd0e497361c3c06e7e18db74033d4866cc0/Node/src/TeamsMiddleware.ts

Modifying it to .js, the following works as expected on the MS Teams channel:

var message = session.message;
var botMri = message.address.bot.id.toLowerCase();
var botAtMentions = message.entities && message.entities.filter(
    (entity) => (entity.type === "mention") && (entity.mentioned.id.toLowerCase() === botMri));
    
if (botAtMentions && botAtMentions.length) {
    message.text = botAtMentions.reduce((previousText, entity) => {
        return previousText.replace(entity.text, "").trim();
    }, message.text);
}

EricDahlvang avatar Feb 08 '18 04:02 EricDahlvang