2

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

catBuddy
  • 367
  • 1
  • 3
  • 15

1 Answers1

2

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);
  });
});
Qianhao Dong
  • 153
  • 6
  • Is it also possible to just initiate or trigger a http post dynamically at run time at app or tab load with azure functions.?? I mean there must be some function that runs at loading or after install something... I just cannot find it .. Actually I am not using SDK but teams toolkit( I think this is different from SDK, it is azure functions) – catBuddy Feb 27 '23 at 12:53
  • Do you mean that your tab is hosted by azure functions? And would like the action to be triggered by some user first loading that tab? – Qianhao Dong Feb 28 '23 at 01:57
  • AFAIK, teams tab depends on teams-js client SDK which only works on frontend but not backend. So the workaround may be - get tab context via `microsoftTeams.app.getContext()`, then send user info `context.user` to your backend azure function to see if it's the first time that user opens your tab (may need certain storage to store all existing users). – Qianhao Dong Feb 28 '23 at 02:01
  • Yes "Do you mean that your tab is hosted by azure functions? And would like the action to be triggered by some user first loading that tab? " is correct – catBuddy Feb 28 '23 at 02:51
  • From tab you can get user info via teams-js client SDK `microsoftTeams.app.getContext()`. Perhaps you can have some connections between your frontend and backend, to use that user info to trigger something. – Qianhao Dong Feb 28 '23 at 03:54
  • Hey I figured that the SDK will also work with the toolkit setup and so I went with the first way you descibed that is using the TeamsActivityHandler. The code did compile and run. I just do not see any text message or card automatically (without invoking web request from command line). I fire the "Hello" text message or any other text message on local debug session, but there is no reply. I added the screesnhots of my code in the original post above . Please see those images and comment if you can figure out the problem. My goal is to deploy on azure for dev and then publish to teams. – catBuddy Feb 28 '23 at 05:42
  • Updated the answer as well - it seems something wrong in `index.js` – Qianhao Dong Feb 28 '23 at 06:52
  • Yes that works now but I see two message all at once instead of just one. I tried commenting the server.post request to trigger notification in the index file but I still see two cards posted at once and I want just one. Any idea what could be going wrong ? – catBuddy Feb 28 '23 at 12:45
  • Are those welcome messages or notification messages? I'd suggest to add breakpoint or log to see how many `sendActivity` calls and how many `installations` returned. Or, if you meet this issue when F5 locally, try 1)delete file `.notification.localstore.json`. 2)re-launch your app. 3)send any message to the bot. 4)try notification again. – Qianhao Dong Mar 01 '23 at 06:28
  • So, I commented out the install part already, removed support for notification. You see this function bot.adapter.process(req, res, async(context) => { /// log context.activity.text ... await botActivityHandler.run(context); }); }); is being called twice is the issue. I logged context and saw it is hitting and printing twice and I see two cards – catBuddy Mar 01 '23 at 07:45
  • For the twice calls, the detailed body data should be different. E.g., `type`, `id`, or any other field. It seems a Teams issue but you can de-dup by certain field from your code. – Qianhao Dong Mar 01 '23 at 09:46