BOT Framework Directline API 调用 header 属性
BOT Framework Directline API call with header attributes
我想调用我的 BOT 框架 Directline API,其中包含额外的 header 属性以及直线密码。如何在我的 BOT 框架代码中提取它并将其放入 IBOTStore 以供将来使用。我想知道我是否可以在意图级别读取 header 属性?
这是我解析 alexa API 请求的示例代码:
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 框架
public async Task None(IDialogContext context, LuisResult result)
{
try
{...............}
感谢您的提前支持!!
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.
根据我的测试,我们在 Direct Line API 请求中指定的附加自定义 headers 将被删除,机器人应用程序将不会收到自定义 headers。
如果您想传递一些有用的信息并将其保存在 Bot 数据存储中,以便您可以在 LUIS 对话框或机器人应用程序中的其他代码逻辑中检索它。您可以尝试通过直线 API 发送 event
activity,在请求的 body 中指定有用的信息,然后在您的机器人应用程序中您可以提取event
activity 中的数据并将数据存储在 UserData
或 ConversationData
等
中
正如@Fei Han 所说,自定义 headers 被剥离并且未到达应用程序,因此这不是发送信息的好方法。
在 activity 上发送自定义数据的一个好方法是通过频道数据。对于 non-Direct 线路频道,有一个教程介绍如何做到这一点 here。
幸运的是,对于 Direct Line,channelData 属性 上的数据没有 pre-formatted 架构,您需要将 JSON 保留在其中,因此您基本上可以根据需要形成数据。
我想调用我的 BOT 框架 Directline API,其中包含额外的 header 属性以及直线密码。如何在我的 BOT 框架代码中提取它并将其放入 IBOTStore 以供将来使用。我想知道我是否可以在意图级别读取 header 属性?
这是我解析 alexa API 请求的示例代码:
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 框架
public async Task None(IDialogContext context, LuisResult result)
{
try
{...............}
感谢您的提前支持!!
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.
根据我的测试,我们在 Direct Line API 请求中指定的附加自定义 headers 将被删除,机器人应用程序将不会收到自定义 headers。
如果您想传递一些有用的信息并将其保存在 Bot 数据存储中,以便您可以在 LUIS 对话框或机器人应用程序中的其他代码逻辑中检索它。您可以尝试通过直线 API 发送 event
activity,在请求的 body 中指定有用的信息,然后在您的机器人应用程序中您可以提取event
activity 中的数据并将数据存储在 UserData
或 ConversationData
等
正如@Fei Han 所说,自定义 headers 被剥离并且未到达应用程序,因此这不是发送信息的好方法。
在 activity 上发送自定义数据的一个好方法是通过频道数据。对于 non-Direct 线路频道,有一个教程介绍如何做到这一点 here。 幸运的是,对于 Direct Line,channelData 属性 上的数据没有 pre-formatted 架构,您需要将 JSON 保留在其中,因此您基本上可以根据需要形成数据。