You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, it's not so clear that how to treat the conversation reference for different activity types, or how to get correct value for 1:1 chat / group / team channel. For example:
On team deleted, I need to remove/forget the conversation reference
Get the team's conversation reference instead of the channel post one
Would it worth to add some utilities/helpers to simplify the whole flow?
Solution
Based on current SDK, here's one solution to use a beforeTurnHandler to manage conversation references on all incoming activities.
import{TurnState}from"@microsoft/teams-ai";import{TurnContext,Activity,ConversationReference}from"botbuilder";exportconstconversationReferences: Record<string,any>={};exportasyncfunctionbeforeTurnHandler(context: TurnContext,state: TurnState): Promise<boolean>{constactivityType=context.activity.type;constconversationReference=getConversationReference(context.activity);letoperation="";// determine operation per activity types and detailsif(activityType==="installationUpdate"){constaction=context.activity.action?.toLowerCase();if(action==="add"){operation="add";}else{operation="remove";}}elseif(activityType==="conversationUpdate"){consteventType=context.activity.channelData?.eventTypeasstring;if(eventType==="teamDeleted"){operation="remove";}elseif(eventType==="teamRestored"){operation="add";}}elseif(activityType==="message"){operation="check";}// update conversationReferencesif(operation==="add"){conversationReferences[getKey(conversationReference)]=conversationReference;}elseif(operation==="remove"){deleteconversationReferences[getKey(conversationReference)];}elseif(operation==="check"){if(conversationReferences[getKey(conversationReference)]===undefined){conversationReferences[getKey(conversationReference)]=conversationReference;}}returntrue;}// get conversation reference for a 1:1 chat, or group chat, or teamfunctiongetConversationReference(activity: Activity): Partial<ConversationReference>{constreference=TurnContext.getConversationReference(activity);constconversationType=reference?.conversation?.conversationType;if(conversationType==="channel"){constteamId=activity?.channelData?.team?.id;constchannelId=activity.channelData?.channel?.id;// `teamId === channelId` means General channel. Ignore non-General channel.if(teamId!==undefined&&(channelId===undefined||teamId===channelId)){constteamReference=JSON.parse(JSON.stringify(reference));teamReference.conversation.id=teamId;returnteamReference;}}returnreference;}// get a key of conversation referencefunctiongetKey(reference: Partial<ConversationReference>): string{return`_${reference.conversation?.tenantId}_${reference.conversation?.id}`;}
Wondering if some of the above code parts could be added to SDK to simplify user's code.
Additional Context
No response
The text was updated successfully, but these errors were encountered:
Scenario
Per https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-howto-proactive-message?view=azure-bot-service-4.0&tabs=csharp#retrieve-and-store-the-conversation-reference, to proactively messaging a person/group/team, I need to get the
ConversationReference
first.However, it's not so clear that how to treat the conversation reference for different activity types, or how to get correct value for 1:1 chat / group / team channel. For example:
Would it worth to add some utilities/helpers to simplify the whole flow?
Solution
Based on current SDK, here's one solution to use a
beforeTurnHandler
to manage conversation references on all incoming activities.Wondering if some of the above code parts could be added to SDK to simplify user's code.
Additional Context
No response
The text was updated successfully, but these errors were encountered: