Skip to main content

Notifications

Copilot Studio - General
Answered

API integration witn Power Virtual Agent Chat bot

Posted on by

Hi, 

 

We created an PVA Chat bot and we would like to integrate the chat bot in an application.

Is there an API we can use because all i found in channel is the use of an iframe but this does not match our requirements.

 

Thank you

Categories:
  • adilei Profile Picture
    adilei on at
    Re: API integration witn Power Virtual Agent Chat bot

    Maybe start a new thread? You can tag me there. 

  • JohanT Profile Picture
    JohanT 35 on at
    Re: API integration witn Power Virtual Agent Chat bot

    According to https://learn.microsoft.com/en-us/microsoft-copilot-studio/advanced-end-user-authentication?tabs=web#useraccesstoken you should supply User.AccessToken. I'm using the rest api, and it is a bit unclear how to do it. I guess the bearer token in the header still is reserved for the directLineSecret? Should i supply it in a body? If so what key should I use? Also, is there any documentation for this. I'm making a nativ iOS app, and most documentations seems to be about web.

  • adilei Profile Picture
    adilei on at
    Re: API integration witn Power Virtual Agent Chat bot

    When manual authentication is configured, Copilot Studio will send a sign-in request activity when the sign-in topic is triggered. Your client should obtain an access token for a user that is signed-in and send it over Directline. The pattern is similar to the client-side logic here: CopilotStudioSamples/BuildYourOwnCanvasSamples/3.single-sign-on/index.html at master · microsoft/CopilotStudioSamples (github.com)

     

     

  • soundharya Profile Picture
    soundharya 2 on at
    Re: API integration witn Power Virtual Agent Chat bot

    Thank you Henry, this is really helpful!

    This works fine when security is set to None, do you have any idea or tried on how to do for SSO login or for manual authentication? 

  • HenryJammes Profile Picture
    HenryJammes on at
    Re: API integration witn Power Virtual Agent Chat bot

    Awesome, great to see this is helpful @kmoens! Feel free to also accept my post as the solution 

  • Re: API integration witn Power Virtual Agent Chat bot

    Hi Henry,

     

    This is perfect !!!

     

    Thank you so much

  • Verified answer
    HenryJammes Profile Picture
    HenryJammes on at
    Re: API integration witn Power Virtual Agent Chat bot

    Sure @kmoens,

     

    I'll break it down to you below. These are basic examples, please refer to the documentation to learn more about all the options and parameters you can use. The Direct Line endpoints may also be different by region, but this is also documented. 

     

    #1 Generate a Direct Like Token:

     

    The first thing you want to do is generating a Direct Line token for the conversation.

    You can get it under Settings > Channels, in "Mobile App":

    HenryJammes_0-1698317174695.png

     

    So just do a GET on the URL, and it will return the token as well as a conversationid.

     

    E.g. in Postman for my test bot endpoint: 

     

    https://c53bf00279234d1cb5ae3265093d59.e1.environment.api.powerplatform.com/powervirtualagents/botsbyschema/cr507_testChatbot/directline/token?api-version=2022-03-01-preview

     

    HenryJammes_1-1698317374474.png

     

     

    #2 Initiate Conversation

     

    Then, still using Bearer Token authentication with the retrieved token in step #1, do a POST on:

     

    https://directline.botframework.com/v3/directline/conversations

     

    This will also return the streamUrl if you want to use WebSocket (Receive activities from the bot - Bot Service | Microsoft Learn)

     

    E.g. in Postman:

    HenryJammes_2-1698317574018.png

     

    #3 Send startConversation event

     

    You can trigger the automatic start of the conversation by sending the startConversation event

    Here, still using Bearer Token authentication with the retrieved token in step #1, do a POST on this URL.
    Notice that now you also need to pass the conversationId in the URL:

     

     

    https://directline.botframework.com/v3/directline/conversations/{{ConversationId}}/activities

     

    In the body, send:

     

    {
     "type": "event",
     "name": "startConversation"
    }

     

     

    E.g. in Postman:

    HenryJammes_3-1698317794797.png

     

    #4 Send message

     

    To send a message, still using Bearer Token authentication with the retrieved token in step #1, and still with the conversationId, do a POST on this URL:

     

    https://directline.botframework.com/v3/directline/conversations/{{ConversationId}}/activities

     

     

     

     

    In the body, send:

     

    {
     "type": "message",
     "text": "When are you closed"
    }

     

     

    E.g. in Postman:

    HenryJammes_4-1698317938565.png

     

    #5 Get responses

     

    You can return all the chatbot activities (send and received messages), still using Bearer Token authentication with the retrieved token in step #1, and still with the conversationId. Do a GET on this URL:

     

    https://directline.botframework.com/v3/directline/conversations/{{ConversationId}}/activities

     

     

    E.g. in Postman:

    HenryJammes_5-1698318097367.png

     

    Let me know if this helps

    Henry

     

     

     

  • Re: API integration witn Power Virtual Agent Chat bot

    Hi, thank you.

     

    Can i find some examples somewhere how to start the conversation ?

     

  • HenryJammes Profile Picture
    HenryJammes on at
    Re: API integration witn Power Virtual Agent Chat bot

    Hi @kmoens,

     

    Yes, that would be the Direct Line API: API reference - Direct Line API 3.0 - Bot Service | Microsoft Learn

     

    Henry

Helpful resources

Quick Links

Exciting News for Copilot Studio Communi…

Get ready to experience a whole new level of engagement with the Copilot Studio…

Celebrating the May Super User of the…

LaurensM is an exceptional contributor to the Power Platform Community…

Check out the Copilot Studio Cookbook…

We are excited to announce our new Copilot Cookbook Gallery in the Community…

Leaderboard

#1
renatoromao Profile Picture

renatoromao 6,459

#2
Pstork1 Profile Picture

Pstork1 1,997

#3
Expiscornovus Profile Picture

Expiscornovus 1,708

Leaderboard