Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Copilot Studio - General
Answered

I want to retrieve user bot conversations using direct line api

(0) ShareShare
ReportReport
Posted on by 78

Hi Team,

 

I tried retrieving below highlighted conversations using Direct Line API 3.0,

but getting an error saying that "Token not valid for this conversation"

 

argeraju_0-1704294357120.png

 

Generated token using Mobile Channel Endpoint URL.

argeraju_1-1704294396513.png

 

In the above GET call, used System.Conversation.Id value

argeraju_2-1704294573102.png

 

 

can you please help me out with this issue?

Categories:
  • argeraju Profile Picture
    78 on at
    Re: I want to retrieve user bot conversations using direct line api

    Hi Michael,
    Thank you so much for quick reply.
    Is it possible handoff conversations to a Generic Engagement Hub.

    Hand off chatbot conversations to any generic engagement hub - Microsoft Copilot Studio | Microsoft Learn

    Below Github repo is not having complete code for this implementation, if possible, can you please provide complete code for this.
    CopilotStudioSamples/ConnectToEngagementHub at master · microsoft/CopilotStudioSamples · GitHub

    Is this solution works for both Teams and Web?

  • michael-jiang Profile Picture
    Microsoft Employee on at
    Re: I want to retrieve user bot conversations using direct line api

    you can build you own user and assistant transcript and keep it in a global variable to do the same thing.

  • argeraju Profile Picture
    78 on at
    Re: I want to retrieve user bot conversations using direct line api

    Hi Henry,

    Thank you so much for providing workaround solution. Appreciated for responding to our queries quickly.

    Is there any solution available for PVA bot to receive teams bot conversations like we have it for bot framework?
    Messages in bot conversations - Teams | Microsoft Learn

  • HenryJammes Profile Picture
    on at
    Re: I want to retrieve user bot conversations using direct line api

    The token gets generated when initiating the conversation from the chat client. Not during conversation runtime, as the conversation has already started.

     

    For Teams, the only workaround I can think of is capturing all user and bot messages as part of a global variable. For users messages, you can use the "message received" trigger event. For bot messages there is no equivalent, so you'd need to append to your variable in each topic, after a bot message.

     

    Henry

  • argeraju Profile Picture
    78 on at
    Re: I want to retrieve user bot conversations using direct line api

    Is there any workaround or solution available for teams channel?

     

    Can't we generate token inside copilot studio using below api endpoint?

    argeraju_0-1704372567366.png

     

  • HenryJammes Profile Picture
    on at
    Re: I want to retrieve user bot conversations using direct line api

    My example works for the default canvas using the latest code provided here: https://learn.microsoft.com/en-us/microsoft-copilot-studio/customize-default-canvas?tabs=web#customize-the-default-canvas-simple, as long as you pass the token value as a context variable to your bot.

     

    This won't work in the Teams channel, as it isn't using Direct Line but a specific channel instead.

  • argeraju Profile Picture
    78 on at
    Re: I want to retrieve user bot conversations using direct line api

    Hi Henry,
    Thank you so much quick reply. Appreciated for all help.
    Provided example worked for me. But It is not taking token value from the canvas. When I am providing manually through a questions, then able to retrieve user conversations as shown below.

    argeraju_3-1704372066250.png

     

     

    User will access this bot from Teams App also. There we will not be having option to trigger 'StartConversation' event like how we did it from the canvas.

     

    do you have solution for Teams Channel?

    Also, is there any way to generate token using API endpoints inside copilot studio like how we are generating token using below code?

     

     

     

    argeraju_2-1704371839622.png

     

  • Verified answer
    HenryJammes Profile Picture
    on at
    Re: I want to retrieve user bot conversations using direct line api

    Hi  @argeraju 

     

    I actually tried something new to get the transcript, you may disregard the "Escalate" approach.

    This works for conversations happening on the Direct Line channel (website, custom app, etc.)

     

    I basically:

    • Pass the Direct Line token as a global variable from the canvas at conversation start
    • When I want to generate the transcript, I:
      • Make an HTTP call to the Direct Line API with the conversation ID and token 
      • Filter the activities table to the messages
      • Build an adaptive card to display them

         

         

     

    HenryJammes_4-1704306386802.png

     

    To pass the Direct Line token, I just add this value to the default canvas:

    https://learn.microsoft.com/en-us/microsoft-copilot-studio/customize-default-canvas?tabs=web#customize-the-default-canvas-simple 

     

    HenryJammes_5-1704306411359.png

     

     

    On the Copilot Studio side, I create a custom topic where I ask the questions to capture the input Global variable(s):

     

    HenryJammes_2-1704306110552.png

     

     

    For my example, I create a Transcript topic, where I make an HTTP call to the Direct Line API to retrieve the conversation activities. I use the System.Conversation.Id and Global.DirectLineToken variables:

     

    HenryJammes_3-1704306191235.png

     

    And then with some Power Fx formula I isolate the messages to build the transcript as a table and then display it to the user as an adaptive card.

     

    Here's the full topic YAML:

     

    kind: AdaptiveDialog
    beginDialog:
     kind: OnRecognizedIntent
     id: main
     intent:
     triggerQueries:
     - Transcript
    
     actions:
     - kind: HttpRequestAction
     id: Uaai35
     url: "=\"https://directline.botframework.com/v3/directline/conversations/\" & System.Conversation.Id & \"/activities\""
     headers:
     Authorization: "=\"Bearer \" & Global.DirectLineToken"
    
     response: Topic.Transcript
     responseSchema:
     kind: Record
     properties:
     activities:
     type:
     kind: Table
     properties:
     attachments:
     type:
     kind: Table
     properties:
     content:
     type:
     kind: Record
     properties:
     $schema: String
     body:
     type:
     kind: Table
     properties:
     text: String
     type: String
     wrap: Boolean
    
     type: String
     version: String
    
     contentType: String
    
     channelId: String
     conversation:
     type:
     kind: Record
     properties:
     id: String
    
     entities:
     type:
     kind: Table
    
     from:
     type:
     kind: Record
     properties:
     id: String
     name: String
     role: String
    
     id: String
     inputHint: String
     listenFor:
     type:
     kind: Table
    
     membersAdded:
     type:
     kind: Table
    
     membersRemoved:
     type:
     kind: Table
    
     name: String
     reactionsAdded:
     type:
     kind: Table
    
     reactionsRemoved:
     type:
     kind: Table
    
     replyToId: String
     serviceUrl: String
     speak: String
     suggestedActions:
     type:
     kind: Record
     properties:
     actions:
     type:
     kind: Table
     properties:
     text: String
     title: String
     type: String
     value: String
    
     to:
     type:
     kind: Table
    
     text: String
     textFormat: String
     textHighlights:
     type:
     kind: Table
    
     timestamp: String
     type: String
    
     - kind: SetVariable
     id: setVariable_Ej7JF3
     variable: Topic.TranscriptTable
     value: |-
     =ForAll(
     Filter(Topic.Transcript.activities, type = "message"),
     { 
     from: If(from.role = "bot", "Bot: ", "User: "), 
     text: text
     }
     )
    
     - kind: SendActivity
     id: sendActivity_tVLuEw
     activity:
     attachments:
     - kind: AdaptiveCardTemplate
     cardContent: |-
     ={
     type: "AdaptiveCard",
     '$schema': "http://adaptivecards.io/schemas/adaptive-card.json",
     version: "1.5",
     body: [
     {
     type: "Container",
     items: [
     {
     type: "TextBlock",
     text: "Conversation ID: " & System.Conversation.Id,
     wrap: true,
     size: "Small",
     weight: "Lighter"
     },
     {
     type: "TextBlock",
     text: "Date: " & Now(),
     wrap: true,
     size: "Small",
     weight: "Lighter"
     }
     ]
     },
     {
     type: "Table",
     columns: [
     {
     width: "1"
     },
     {
     width: "4"
     }
     ],
     rows: ForAll(Topic.TranscriptTable,
     {
     type: "TableRow",
     cells: [
     {
     type: "TableCell",
     items: [
     {
     type: "TextBlock",
     text: from,
     wrap: true,
     weight: "Bolder"
     }
     ]
     },
     {
     type: "TableCell",
     items: [
     {
     type: "TextBlock",
     text: text,
     wrap: true
     }
     ]
     }
     ]
     }
     ),
     firstRowAsHeaders: false
     }
     ]
     }
  • argeraju Profile Picture
    78 on at
    Re: I want to retrieve user bot conversations using direct line api

    Hi Henry,

    Thank you so much for quick reply, appreciated.

    I have added mentioned event in Escalate Topic. Here I don't know what value should be provided to this event.

    argeraju_1-1704305369968.png

     

    argeraju_0-1704305049301.png

    I can able to download transcript JSON file but it is not having any values, it is just empty.

     

    can you please provide a sample code?
    Also, provide sample code or endpoint for retrieving those transcripts using Direct line API.

  • HenryJammes Profile Picture
    on at
    Re: I want to retrieve user bot conversations using direct line api

    The Direct Line token is unique per conversation, I don't think it's available as part of the native system variable. 

    To retrieve the conversation transcript over the Direct Line API, Copilot Studio must escalate to a live agent (and so send a a handoff.initiate event, that will contain variable values and the full technical JSON transcript as an activity attachment).

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Thomas Rice – Community Spotlight

We are honored to recognize Thomas Rice as our March 2025 Community…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,508 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,440 Most Valuable Professional

Leaderboard