Send Conversation History API returning HTTP 400 BotFramework

225 Views Asked by At

I'm trying to sendConversation History API provided by Microsoft in BotFramework Documentation. It takes Transcript object (array of activities) as the input. But I've getting HTTP 400 Bad Request when using trying that.

I'm making a POST request on this and authenticating it using JWT Token generated by client credentials for the bot.

https://directline.botframework.com/v3/conversations/HBoZPkTiBYOAadigqEqFaJ-us/activities/history

This is the error I get.

{
    "error": {
        "code": "BadSyntax",
        "message": "Invalid or missing activities"
    }
}

This is the activity array payload that I'm using

[    
    {
        "type": "message",
        "id": "HBoZPkTiBYOAadigqEqFaJ-us|0000003",
        "timestamp": "2022-09-05T08:10:00.3574378Z",
        "localTimestamp": "2022-09-05T13:39:57.633+05:30",
        "localTimezone": "Asia/Calcutta",
        "serviceUrl": "https://directline.botframework.com/",
        "channelId": "directline",
        "from": {
            "id": "dl_uID1",
            "name": "userName"
        },
        "conversation": {
            "id": "HBoZPkTiBYOAadigqEqFaJ-us"
        },
        "recipient": {
            "id": "bot-name-dev@abcd",
            "name": "bot-name-dev"
        },
        "textFormat": "plain",
        "locale": "en-GB",
        "text": "hi",
        "channelData": {
            "clientActivityID": "1662365337633rmq6e8mbm09",
            "clientTimestamp": "2022-09-05T08:09:57.633Z"
        }
    }
]

I'm not able to figure out what's the correct format of activity object that it is expecting.

2

There are 2 best solutions below

0
akum1143 On BEST ANSWER

After connecting with Microsoft, I found this is the correct payload for this API.

{
  "activities": [
   
       {
    "type": "message",
    "id": "whatever-conversation-id-in|0000000",
    "timestamp": "2022-11-20T20:14:26.242902Z",
    "serviceUrl": "https://directline.botframework.com/",
    "channelId": "directline",
    "from": {
        "id": "dl_test_id",
        "name": ""
    },
    "conversation": {
        "id": "whatever-conversation-id-in"
       
    },
    "recipient": {
        "id": "ABC_bot",
        "name": "ABC_bot"
    },
    "text": "Hello Bot"
}
]
}
0
Sairam Tadepalli On

Create the bot and get it connected to the Direct Line. Get the secret keys of the site that need to connect. Before calling SendConversationHistory if the message is delivered to the recipient, it will create the bad request error message as it is unable to get the conversation history as the message was already received. SendConversationHistory must be called before sending the message then the Conversation History will be tracked. The below screens will be helpful to create the DirectLine connection to the bot created. Then needed to add the trusted sites to DirectLine. Then proceed with the code sample mentioned below which takes the conversation history.

enter image description here

Click on Create a resource

enter image description here

Search for bot and click on create

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Select Channels

enter image description here

enter image description here

Get the App Service Extension Keys and click on Default_site

enter image description here

Click on the toggle and enable the enhanced authentication options and add the website which we need to communicate. In the code block of the current working application, to get the conversation history, use the following code block.

foreach (var a in activities)
{
    incrementId++;
    // Use your own ID format
    a.Id = string.Concat("history|", incrementId.ToString().PadLeft(7, '0'));
    a.ChannelData = null;
    a.Conversation = new ConversationAccount(id: convId);

    if (a.From.Name == message.Recipient.Name)
    {
        // Bot to recipient connection 
        a.Recipient = message.From;
        a.From = message.Recipient;
    }
    else
    {
        // User to bot connection (here bot will be the recipient)
        a.Recipient = message.Recipient;
        a.From = message.From;
    }
}