Jul 31 2023 08:28 AM
Hi, we develop a 3rd party app on the MS Teams store, which includes functionality to send proactive messages to users. The outline of the code we are using is as follows:
Our deployed bot listens to new users joining team where the bot is installed, using the
class WelcomeBot extends TeamsActivityHandler {
this.onTeamsMembersAddedEvent(async (membersAdded, teamInfo, context, next) => {
const members = [];
do {
var pagedMembers = await TeamsInfo.getPagedMembers(context, 100, continuationToken);
continuationToken = pagedMembers.continuationToken;
members.push(...pagedMembers);
}
await next();
} while (continuationToken !== undefined);
...
members.forEach(async (teamMember) => {
const ref = TurnContext.getConversationReference(context.activity);
ref.user = teamMember;
await context.adapter.createConversation(ref,
async (t1) => {
const conversationRef = TurnContext.getConversationReference(t1.activity);
// conversationRef is saved to the database for this user
if (statement that checks if this is a new user) {
await t1.adapter.continueConversation(conversationRef, async (t2) => {
await this.sendWelcomeCard(t2); // sends a welcome card for new users
});
}
);
});
}
To summarize the code above, for each member we detect in the team this bot is deployed to, we get their conversation reference (the entire object), and save it to the database for that user.
To send proactive messages in the future, we use code like this:
// using conversation = the same conversation JSON object that was saved to the database
BotConnector.MicrosoftAppCredentials.trustServiceUrl(conversation.serviceUrl);
adapter.continueConversation(conversation, async (t2) => {
const card = BotBuilder.CardFactory.heroCard(
title,
message,
[],
actions
);
await t2.sendActivity({ attachments: [card] });
}
Sometimes, this works for users for a long time (or forever), meaning the conversation object we saved to the DB is still the correct one we can pull up and use to send proactive messages. However, for other users (including myself), after a few days of use this saved conversation object is no longer valid to send messages to, and the users stop getting proactive messages.
There is a workaround we can do, which involves removing and re-adding our app to personal scope, or adding new users to the channel where the app/bot is installed, which runs the
Aug 01 2023 02:33 AM
Aug 07 2023 01:47 AM - edited Aug 07 2023 01:47 AM
@AlexK480 - Microsoft Teams supports proactive messaging, which allows your bot to initiate conversations with users without waiting for them to send a message first. By using proactive messages, you can ensure that your bot always has the latest conversation context. This way, you won't need to rely solely on responding to incoming messages.
Reference Document-https://learn.microsoft.com/en-us/microsoftteams/platform/resources/bot-v3/bot-conversations/bots-co...
We are checking with the internal team as well and let you know the update once we have any.
Aug 13 2023 11:55 AM
Aug 13 2023 10:45 PM
Aug 19 2023 01:20 PM
SolutionAug 19 2023 01:20 PM
Solution