【发布时间】:2020-01-17 07:22:00
【问题描述】:
问题:
我正在努力了解如何获得令牌。我知道我为什么要使用它们,但我只是不明白如何获得它们。所有使用令牌的样本都只是从“https://webchat-mockbot.azurewebsites.net/directline/token”或类似的东西中获取它们。如何在我的机器人中创建此路径?
描述您考虑过的替代方案
我能够创建一些适用于我的 JS-Bot 的东西:
const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log(`\n${ server.name } listening to ${ server.url }`);
console.log('\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator');
console.log('\nTo talk to your bot, open the emulator select "Open Bot"');
});
server.post('/token-generate', async (_, res) => {
console.log('requesting token ');
try {
const cres = await fetch('https://directline.botframework.com/v3/directline/tokens/generate', {
headers: {
authorization: `Bearer ${ process.env.DIRECT_LINE_SECRET }`
},
method: 'POST'
});
const json = await cres.json();
if ('error' in json) {
res.send(500);
} else {
res.send(json);
}
} catch (err) {
res.send(500);
}
});
但我不知道如何使用我的 C#-Bot 来执行此操作(我切换到 C#,因为我比 JS 更了解它)。
在我的 C#-Bot 中只有这个:
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
namespace ComplianceBot.Controllers
{
// This ASP Controller is created to handle a request. Dependency Injection will provide the Adapter and IBot
// implementation at runtime. Multiple different IBot implementations running at different endpoints can be
// achieved by specifying a more specific type for the bot constructor argument.
[Route("api/messages")]
[ApiController]
public class BotController : ControllerBase
{
private readonly IBotFrameworkHttpAdapter _adapter;
private readonly IBot _bot;
public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
{
_adapter = adapter;
_bot = bot;
}
[HttpGet, HttpPost]
public async Task PostAsync()
{
// Delegate the processing of the HTTP POST to the adapter.
// The adapter will invoke the bot.
await _adapter.ProcessAsync(Request, Response, _bot);
}
}
}
我可以在这里添加一条新路线吗?像 [Route("directline/token")] ?
我知道我可以用一个额外的“令牌服务器”来做到这一点(我不知道如何实现它,但我知道这会起作用),但如果可能的话,我想用我已经存在的c#-bot 就像我用我的 JS-Bot 做的那样。
【问题讨论】: