Git Product home page Git Product logo

botbuilder-strip-mentions's Introduction

Purpose

For use with the Microsoft Bot Builder SDK v4, particularly when developing a bot for Microsoft Teams. Users will often @mention the bot or other users within a Team, which pollutes incoming messages with possibly undesired <at> tags. This package helps strip these <at> tags from the message.

For example, if the user typed

@Teams Bot Schedule a meeting with @John Smith

then your bot would receive the string

<at>Teams Bot</at> Schedule a meeting with <at>John Smith</at>

and this package could produce the string

Schedule a meeting with John Smith

Usage

Option 1: Middleware

Your bot can use() this middleware as usual:

const stripMentions = new StripMentions();
adapter.use(stripMentions);

The stripped text is placed in context.request.text. You can retrieve the unstripped text using the getOriginalText() method. (Note that this may not be the truly original text, particularly if you're using other middleware that modifies context.request.text before this one.)

adapter.processRequest(req, res, context => {
    if (context.request.type === ActivityTypes.Message) {
        const originalText = stripMentions.getOriginalText(context);
        console.log(`Original text: ${originalText}`);
        console.log(`Stripped text: ${context.request.text}`);
    }
});

You can configure how the tags are stripped for your bot vs. for other users via the StripMentions constructor. See Configuration for details.

// These are the default values for the middleware
new StripMentions({
    botBehavior: 'whole',
    userBehavior: 'tags'
})

Option 2: Calling without middleware

Alternatively, you can call the strip logic yourself without using middleware:

adapter.processRequest(req, res, context => {
    if (context.request.type === ActivityTypes.Message) {
        const mentions = context.request.entities.filter(e => e.type === 'mention');
        const strippedText = stripMentions(context.request.text, mentions, 'tags');
    }
});

You can configure how the tags are stripped using the last parameter to stripMentions(). See Configuration for details.

Configuration

The valid behaviors are

  • whole: The tags and contained text are removed
  • tags: Only the tags are removed
  • none: Nothing is removed

For example, for this example utterance from before:

@Teams Bot Schedule a meeting with @John Smith

the middleware could produce the following:

Bot behavior User behavior Stripped text
whole tags Schedule a meeting with John Smith
tags tags Teams Bot Schedule a meeting with John Smith
whole whole Schedule a meeting with

botbuilder-strip-mentions's People

Contributors

navzam avatar

Watchers

James Cloos avatar James Mann avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.