Search code examples
botframeworkdirect-line-botframework

BOT Framework Directline API call with header attributes


I would like to call my BOT framework Directline API with additional header attributes along with the direct line secret. How to extract that in my BOT framework code and put it in the IBOTStore for the future usage. I am wondering if I could read the header attribute in the intent level?

Here is my sample code to parse the alexa API request:

 var directLineSecret = ConfigurationManager.AppSettings["directlinesecret"];
        _client = new DirectlineClient(directLineSecret, "alexa" + Guid.NewGuid().ToString().Replace("-", ""));

var client = new HttpClient
        {
            BaseAddress = new Uri("https://directline.botframework.com")
        };

        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", _secret);
        client.DefaultRequestHeaders.Add("ETag", "*");
        client.DefaultRequestHeaders.Add("channel", _from);

        var response =
            await client.GetAsync("/api/tokens/", HttpCompletionOption.ResponseHeadersRead)
            .ConfigureAwait(false);
        response.EnsureSuccessStatusCode();

        response =
            await client.PostAsJsonAsync("/api/conversations/", new object())
            .ConfigureAwait(false);
        response.EnsureSuccessStatusCode();

        var conversationInfo =
            await response.Content.ReadAsAsync<JObject>()
            .ConfigureAwait(false);

        _conversationId = (string)conversationInfo["conversationId"];
        var scopedToken = (string)conversationInfo["token"];

        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", scopedToken);

BOT Framework

 public async Task None(IDialogContext context, LuisResult result)
    {
        try
        {...............}

Thank you for your advance support !!


Solution

  • As @Fei Han stated, custom headers are stripped and do not reach the application, so this is not a good way to send information.

    A good way to send custom data on an activity is through channel data. For non-Direct Line channels, there is a tutorial on how to do that here. Luckily for Direct Line, there is no pre-formatted schema for data on the channelData property that you need to keep your JSON in, so you can essentially form the data however you would like.