Search code examples
botframeworkmicrosoft-teamsteams-toolkit

How to use ConversationBot from @microosft/teamsfx and EventHandler from botbuiler in the microsoft team app


I am developing an app using teamsfx toolkit and I have two important use case.

Need to send notifications to particular users via API I also need to send a welcome message to newly installed users. I am using ConversationBot from @microosft/temasfx library to set adapter config and send notifications to users using API/notification URL like

const bot = new ConversationBot({
  // The bot id and password to create BotFrameworkAdapter.
  adapterConfig: {
    appId: config.botId,
    appPassword: config.botPassword,
  }}
 // and when the server.post api use this to send notification
 for (const target of await Mybot.notification.installations()) {

I also need to extend an EventHandler class to override

this.onMembersAdded(async (context, next) => {() 

method to send a welcome message. But I am unable to like these two classes in the same app.

I have

const bot = new ConversationBot

Like I also have

class WelcomeBot extends ActivityHandler
{
this.onMemebersAdded(){//sending welcome message}

So the question is how to link both in the same bot app so that I can send a welcome Message using EventHandler and send Notifications via ConversationBot?


Solution

  • If your app is created via Teams Toolkit, probably you have following code (normally in index.js|ts):

    server.post("/api/messages", async (req, res) => {
      await bot.requestHandler(req, res);
    });
    

    Here bot is a ConversationBot. To integrate your WelcomeBot with ConversationBot, please change the code await bot.requestHandler(req, res); to await bot.adapter.processActivity(req, res, (context) => welcomeBot.run(context));

    The full code may look like:

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

    The details behind are - All bot operations depend on Adapter. The ConversationBot provides a helper method requestHandler to simplify the code. But if you'd like to add your own bot logic to the Adapter, you need to explicitly get Adapter via ConversationBot.adapter, then add your own logic.