JS:
Код: Выделить всё
document.addEventListener('DOMContentLoaded', function() {
const botDiv = document.getElementById('bot-div');
const botHeader = document.getElementById('bot-header');
const webchat = document.getElementById('webchat');
botHeader.addEventListener('click', toggleBotWindow);
function toggleBotWindow() {
const isCollapsed = botDiv.classList.toggle('collapsed');
botDiv.setAttribute('aria-expanded', !isCollapsed);
webchat.classList.toggle('hidden', isCollapsed)
}
async function fetchToken() {
const response = await fetcher.postJson('/ChatBot/GetToken');
const tokenData = await response.json();
return tokenData.token;
}
async function inittalizeBot() {
try {
const token = fetchToken();
window.WebChat.renderWebChat({
directLine: window.WebChat.createDirectLine({
token: token
}),
userID: 'YOUR_USER_ID', //optional
username: 'Web Chat User', //optional
locale: 'en-US'
}, webchat);
const styleSet = window.WebChat.createStyleSet({
rootHeight: '100%',
rootWidth: '50%',
backgroundColor: 'azure2'
});
} catch (error) {
console.error('Fehler Erstellen des Bots', error);
}
}
inittalizeBot();
});
Код: Выделить всё
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
namespace Web.Controllers
{
public class ChatBotController : Controller
{
private readonly string directLineSecret = "Imagine my Secret here";
[HttpPost]
public async Task GetToken()
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", directLineSecret);
var response = await client.PostAsync("https://directline.botframework.com/v3/directline/tokens/generate", null);
if (response.IsSuccessStatusCode)
{
var tokenResponse = await response.Content.ReadAsStringAsync();
var token = JObject.Parse(tokenResponse)["token"].ToString();
return Json(new { token });
}
return Json(new { error = "Unable to generate token" });
}
}
}
}
Я пробовал это несколько раз с другим кодом, но ничего не получилось, может быть, я просто чего-то не вижу?
Если я делаю запрос с помощью GIT Bash, он работает и возвращает мне токен.
Я хочу получить тот же токен от моего контроллера. Возможно, вы видите мою ошибку.
Подробнее здесь: https://stackoverflow.com/questions/788 ... e-web-chat