Search code examples
botframeworkmicrosoft-teamsmessageadaptive-cardsteams-toolkit

teams toolkit app notification bot welcome message


I am using teams toolkit version 4 and I created an app with two tabs. One is a notification bot configured with restify and other one is tab. The notification bot did install with adaptive cards as I wanted adaptive cards only. This is not a problem. But,I cannot quite find a way to fire the first welcome adaptive card in the notification bot automatically.

This is my botActivityHandler.js file

enter image description here

For development , I used the Invoke web request API and issued from my powershell to see the first welcome adaptive card.

This is the index.js file

enter image description here

Finally this is the init file

enter image description here

Any suggestions how can a display the the welcome card everytime user switches tabs and selects the notificiation bot tab

This is the folder structure for my bot

enter image description here

I tried reading documentation but it is not give a clear pointer to how to achieve this and I cannot find anything about this on the internet at any other source either. Every solution I see is using powershell parallely to manually send requests to see the adaptive card. I want this for the user to happen but not manually. So, they do not have to have a command line and powershell to execute rest api request.

Here is the app running in teams with two tabs. enter image description here


Solution

  • The Bot Framework SDK supports welcome message that you may need to implement your own TeamsActivityHandler and its onMembersAdded method.

    If your app is created via Teams Toolkit, probably you already have following code:

    /// initialize.ts
    const bot = new ConversationBot...
    
    /// index.ts
    server.post("/api/messages", async (req, res) => {
      await bot.requestHandler(req, res);
    });
    

    To add welcome message, you may need to add your own TeamsActivityHandler:

    /// myBot.ts
    class MyBot extends TeamsActivityHandler {
      constructor() {
        ...
        this.onMembersAdded = ...
      }
    }
    

    Then, use it to handle bot event:

    /// init somewhere
    const bot = new ConversationBot...
    const myBot = new MyBot...
    
    /// The "/api/messages" handler
    server.post("/api/messages", async (req, res) => {
      await bot.requestHandler(req, res, async (context) => {
        await myBot.run(context);
      });
    });
    

    You can find more welcome message samples from the Bot Framework SDK document.

    And there's also another question about adding welcome message to notification bot.



    ============= Update ==============

    The BotActivityHandler implementation looks right, but it seems something wrong in index.js:

    • please try bot.adapter.process(req, res, ... instead of bot.adapter.processActivity(req, res, ...?
    • if still not work, please log req.body and context.activiy.text to see if received message is correct
    server.post("/api/messages", async (req, res) => {
      /// log req.body ...
      bot.adapter.process(req, res, async(context) => {
        /// log context.activity.text ...
        await botActivityHandler.run(context);
      });
    });