angular - MS Copilot studio agent read user information sent via custom web app API - Stack Overflow

admin2025-04-17  4

I have a chat bot agent created in copilot studio. I have configured my angular app to use this agent for chat. This agent is only available for logged in users and we want to share user context with the chat bot.We are able to pass the UserInfo via api call when the connection is established but not able to read/extract this info at the agent level. I have created topic and messages and created variables to extract this info but those variables are always empty. A screenshot of the event activity is attached below

 private loadWebChat(): Observable<void> {
 const styleOptions = {
  hideUploadButton: true,
  suggestedActionLayout: 'flow',
  suggestedActionsStackedLayoutButtonTextWrap: true,
  botAvatarInitials: 'WP',
  userAvatarInitials: 'ME',
  botAvatarImage:
    '.png',
};

const tokenEndpointURL = new URL(
  ''
);

const locale = document.documentElement.lang || 'en';

const directLineURL$ = this.http.get<{ channelUrlsById: { directline: string } }>(
  `${tokenEndpointURL.origin}/powervirtualagents/regionalchannelsettings?api-version=2022-03-01-preview`
).pipe(map((response) => response.channelUrlsById.directline));

const token$ = this.http.get<{ token: string }>(
  tokenEndpointURL.toString()
).pipe(map((response) => response.token));

return directLineURL$.pipe(
  switchMap((directLineURL) =>
    token$.pipe(
      map((token) => ({ directLineURL, token }))
    )
  ),
  map(({ directLineURL, token }) => {
    const webchatContainer = document.getElementById('webchat');
    if (webchatContainer) {
      // Cleanup: Remove any React DOM already rendered
      while (webchatContainer.firstChild) {
        webchatContainer.removeChild(webchatContainer.firstChild);
      }

      const directLine = (window as any).WebChat.createDirectLine({
        domain: new URL('v3/directline', directLineURL).toString(),
        token,
      });

      directLine.connectionStatus$.subscribe({
        next: (value: number) => {
          if (value === 2 && this.userAccounts?.length > 0) {
            const activity = {
              type: 'event',
              name: 'conversation start',
              value: {
                userAccounts: this.userAccounts,
                user: {
                  id: 100,
                  email: '[email protected]',
                  displayName: 'John Doe'
                }
              },
              locale,
              localTimezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
            };

            directLine.postActivity(activity).pipe(take(1)).subscribe({                
              error: (err) => console.error('Failed to post activity:', err),
            });
          }
        },
      });

      (window as any).WebChat.renderWebChat(
        { directLine, locale, styleOptions },
        webchatContainer
      );
    }
  }),
  catchError((err) => {
    console.error(err);
    return of();
  })
);

}

转载请注明原文地址:http://anycun.com/QandA/1744820370a88069.html