MS Copilot studio agent read user information sent via custom web app API

Discussão em 'Angular' iniciado por rumi, Fevereiro 1, 2025.

  1. rumi

    rumi Guest

    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

    https://europe.directline.botframework.com/v3/directline/conversations/8E94ugYROtqJnOZ9tj3m01-eu/activities

    private loadWebChat(): Observable<void> {
    const styleOptions = {
    hideUploadButton: true,
    suggestedActionLayout: 'flow',
    suggestedActionsStackedLayoutButtonTextWrap: true,
    botAvatarInitials: 'WP',
    userAvatarInitials: 'ME',
    botAvatarImage:
    'https://umbracodev001.blob.core.windows.net/chat-bot/ChatBot.png',
    };

    const tokenEndpointURL = new URL(
    'https://defaultxxxxxxxxxxxxxxxxx.79.environment.api.powerplatform.com/powervirtualagents/botsbyschema/cr405_agentUJXC7g/directline/token?api-version=2022-03-01-preview'
    );

    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: 'john.deo@gmai.com',
    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();
    })
    );


    }

    [​IMG]

    Continue reading...

Compartilhe esta Página