步骤1:我克隆了BotFramework核心机器人示例:dotnetcore/13.13.core bot
步骤2:我克隆了BotFramework网络聊天示例“通过使组件可小型化”https://github.com/microsoft/BotFramework-WebChat/tree/master/samples/12.customization-minimizable-web-chat来定制网络聊天
第3步:现在我想将what示例与Core示例连接,这基本上意味着我将Bot URL放入MinimizableWebChat.js网络聊天文件中的以下代码行: const =等待获取(‘Bot.azurewebsites.net/directline/令牌’,{方法:'POST‘});
但是第三步不起作用。网络聊天不连接到我的机器人。看来,Core示例没有实现令牌服务器,因此web聊天示例和核心Bot示例无法连接。这个解释正确吗?如果是,请帮助我将令牌服务器添加到C#中的Core示例中?
PS:内核Bot示例是否真的缺少令牌服务器?这个示例叫做核心机器人,我想每个机器人都需要一个令牌服务器?!
非常感谢!
发布于 2019-09-23 20:03:31
Core示例是否真的缺少令牌服务器?这个示例叫做核心机器人,我想每个机器人都需要一个令牌服务器?!
不,不是真的。如果您正在构建您自己的webchat客户端,比如在webchat示例中,您可以拥有一个令牌服务器来将秘密交换为令牌。但是,如果你没有建立你自己的网络聊天,或者你不关心用一个秘密来交换一个令牌,或者你想使用任何其他的渠道,你不需要一个。
尽管如此,我在C# 这里中有一个工作秘密->令牌交换。就像网络聊天回购中的节点版本一样。以下是我为将该示例从节点转换为C#所做的更改:
在我的html文件中:
<body>
<h2>Index??</h2>
<div id="webchat" role="main" />
<script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
<script>
(async function () {
const res = await fetch('/directline/token', { method: 'POST' });
const { token } = await res.json();
window.WebChat.renderWebChat({
directLine: window.WebChat.createDirectLine({ token }),
}, document.getElementById('webchat'));
})();
</script>
</body>为了抓住这个帖子请求,我不得不给机器人添加几样东西。在启动类中,我需要确保我的令牌模型被看到:
services.Configure<DLSModel>(Configuration.GetSection("DirectLine"));
然后,我将DSLModel添加到我的模型中。

模型本身非常简单:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace JJDirectLineBot.Models
{
public class DLSModel
{
public string DirectLineSecret { get; set; }
}
}基本上,所有这些都会说:“去她的应用程序中寻找她的直线式秘密。”我肯定有个更简单的方法,但我就是这么做的。
然后,您需要一个控制器来实际发出请求:
namespace JJDirectLineBot.Controllers
{
public class TokenController : ControllerBase
{
private readonly IOptions<DLSModel> dlSecret;
public TokenController(IOptions<DLSModel> dls)
{
dlSecret = dls;
}
[Route("/directline/token")]
[HttpPost]
public async Task<string> TokenRequest()
{
var secret = dlSecret.Value.DirectLineSecret;
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(
HttpMethod.Post,
$"https://directline.botframework.com/v3/directline/tokens/generate");
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", secret);
var userId = $"dl_{Guid.NewGuid()}";
request.Content = new StringContent(
JsonConvert.SerializeObject(
new { User = new { Id = userId } }),
Encoding.UTF8,
"application/json");
var response = await client.SendAsync(request);
string token = string.Empty;
if (response.IsSuccessStatusCode)
{
var body = await response.Content.ReadAsStringAsync();
return body;
}
else
{
//Error();
return token;
}
}
}
}https://stackoverflow.com/questions/58053597
复制相似问题