问题描述
我需要使用HTML网络聊天隐藏直接渠道的秘诀,尝试了此解决方案,但在获取信息时总是出现错误。我在process.env中获得了Azure门户中直接通道通道的秘密
Index.js
const dotenv = require('dotenv');
const path = require('path');
const restify = require('restify');
const bodyParser = require('body-parser');
const request = require('request');
const corsMiddleware = require('restify-cors-middleware');
const { BotFrameworkAdapter,MemoryStorage,ConversationState,UserState } = require('botbuilder');
const { EBOT } = require('./eBot');
const ENV_FILE = path.join(__dirname,'.env');
dotenv.config({ path: ENV_FILE || process.env.directLinesecret });
const cors = corsMiddleware({
origins: ['*']
});
const server = restify.createServer();
server.pre(cors.preflight);
server.use(cors.actual);
server.use(bodyParser.json({
extended: false
}));
server.listen(process.env.port || process.env.PORT || 3978,() => {
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"');
});
// Generates a Direct Line token
server.post('/directline/token',(req,res) => {
const options = {
method: 'POST',uri: 'https://directline.botframework.com/v3/directline/tokens/generate',headers: {
'Authorization': `Bearer ${process.env.directLinesecret}`
}};
request.post(options,(error,response,body) => {
if (!error && response.statusCode < 300) {
res.send({
token: body.token
});
} else {
res.status(500).send('Call to retrieve token from DirectLine Failed');
}
});
});
server.post('/api/messages',res) => {
adapter.processActivity(req,res,async (context) => {
await ebot.run(context);
});
});
和webchat.html:
<script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
<script>
(async function () {
const res = await fetch('https://directline.botframework.com/v3/directline/tokens/generate',{ method: 'POST' });
const webChatToken = await res.json();
window.WebChat.renderWebChat({
directLine: window.WebChat.createDirectLine({ token: webChatToken })
},document.getElementById('webchat'));
document.querySelector('#webchat > *').focus();
})().catch(err => console.error(err));
</script>
///更新
错误:
**无法加载资源:服务器的响应状态为403()
** webchat.js:2 POST https://directline.botframework.com/v3/directline/conversations 403
** webchat.js:2未捕获t {消息:“ ajax错误403”,xhr:XMLHttpRequest,请求:{…},状态:403,responseType:“ json”,…}
那是怎么回事?我想念什么?
解决方法
您的特定实现的问题是,尽管您设置了用于生成和提供令牌回Web聊天的API,但是您实际上无法调用该端点。代替
const res = await fetch('https://directline.botframework.com/v3/directline/tokens/generate',{ method: 'POST' });
您应该拥有
const res = await fetch('http://localhost:3978/directline/token',{ method: 'POST' });
正是这个原因,使调用将Direct Line秘密交换为令牌,然后将令牌返回给您的Web聊天实例。
,解决了这个问题,问题在于api令牌函数的制作方式,这是为了从index.js检索json响应而工作的一种方法:
org.example:ToolsIntegration:jar:1.0-SNAPSHOT
+- org.seleniumhq.selenium:selenium-chrome-driver:jar:4.0.0-alpha-6:compile
| +- com.google.auto.service:auto-service-annotations:jar:1.0-rc6:compile
| +- com.google.auto.service:auto-service:jar:1.0-rc6:compile
| | \- com.google.auto:auto-common:jar:0.10:compile
| +- com.google.guava:guava:jar:29.0-jre:compile
| | +- com.google.guava:failureaccess:jar:1.0.1:compile
| | +- com.google.guava:listenablefuture:jar:9999.0-empty-to-avoid-conflict-with-guava:compile
| | +- com.google.code.findbugs:jsr305:jar:3.0.2:compile
| | +- org.checkerframework:checker-qual:jar:2.11.1:compile
| | +- com.google.errorprone:error_prone_annotations:jar:2.3.4:compile
| | \- com.google.j2objc:j2objc-annotations:jar:1.3:compile
| +- org.seleniumhq.selenium:selenium-api:jar:4.0.0-alpha-6:compile
| +- org.seleniumhq.selenium:selenium-chromium-driver:jar:4.0.0-alpha-6:compile
| +- org.seleniumhq.selenium:selenium-devtools:jar:4.0.0-alpha-6:compile
| +- org.seleniumhq.selenium:selenium-http:jar:4.0.0-alpha-6:compile
| +- org.seleniumhq.selenium:selenium-json:jar:4.0.0-alpha-6:compile
| \- org.seleniumhq.selenium:selenium-remote-driver:jar:4.0.0-alpha-6:compile
| +- com.squareup.okhttp3:okhttp:jar:4.5.0:compile
| | \- org.jetbrains.kotlin:kotlin-stdlib:jar:1.3.70:compile
| | \- org.jetbrains:annotations:jar:13.0:compile
| +- com.squareup.okio:okio:jar:2.6.0:compile
| | \- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.3.70:compile
| +- com.typesafe.netty:netty-reactive-streams:jar:2.0.4:compile
| | +- io.netty:netty-handler:jar:4.1.43.Final:compile
| | \- org.reactivestreams:reactive-streams:jar:1.0.3:compile
| +- io.grpc:grpc-context:jar:1.28.0:compile
| +- io.netty:netty-buffer:jar:4.1.49.Final:compile
| | \- io.netty:netty-common:jar:4.1.49.Final:compile
| +- io.netty:netty-codec-http:jar:4.1.49.Final:compile
| | \- io.netty:netty-codec:jar:4.1.49.Final:compile
| +- io.netty:netty-transport-native-epoll:jar:4.1.49.Final:compile
| +- io.netty:netty-transport-native-epoll:jar:linux-x86_64:4.1.49.Final:compile
| +- io.netty:netty-transport-native-kqueue:jar:4.1.49.Final:compile
| +- io.netty:netty-transport-native-kqueue:jar:osx-x86_64:4.1.49.Final:compile
| +- io.netty:netty-transport-native-unix-common:jar:4.1.49.Final:compile
| +- io.netty:netty-transport:jar:4.1.49.Final:compile
| | \- io.netty:netty-resolver:jar:4.1.49.Final:compile
| +- io.opentelemetry:opentelemetry-api:jar:0.4.0:compile
| +- io.opentelemetry:opentelemetry-context-prop:jar:0.4.0:compile
| +- io.opentelemetry:opentelemetry-sdk:jar:0.4.0:compile
| +- io.projectreactor.netty:reactor-netty:jar:0.9.6.RELEASE:compile
| | +- io.netty:netty-codec-http2:jar:4.1.48.Final:compile
| | \- io.netty:netty-handler-proxy:jar:4.1.48.Final:compile
| +- io.projectreactor:reactor-core:jar:3.3.5.RELEASE:compile
| +- net.bytebuddy:byte-buddy:jar:1.10.9:compile
| +- org.apache.commons:commons-exec:jar:1.3:compile
| \- org.asynchttpclient:async-http-client:jar:2.12.1:compile
| +- org.asynchttpclient:async-http-client-netty-utils:jar:2.12.1:compile
| +- io.netty:netty-codec-socks:jar:4.1.48.Final:compile
| +- org.slf4j:slf4j-api:jar:1.7.30:compile
| \- com.sun.activation:javax.activation:jar:1.2.0:compile
+- org.testng:testng:jar:7.0.0:compile
| \- com.beust:jcommander:jar:1.72:compile
\- net.rcarz:jira-client:jar:0.5:compile
+- org.apache.httpcomponents:httpclient:jar:4.2.5:compile
| +- org.apache.httpcomponents:httpcore:jar:4.2.4:compile
| +- commons-logging:commons-logging:jar:1.1.1:compile
| \- commons-codec:commons-codec:jar:1.6:compile
+- org.apache.httpcomponents:httpmime:jar:4.2.5:compile
+- net.sf.json-lib:json-lib:jar:jdk15:2.4:compile
| +- commons-beanutils:commons-beanutils:jar:1.8.0:compile
| +- commons-collections:commons-collections:jar:3.2.1:compile
| +- commons-lang:commons-lang:jar:2.5:compile
| \- net.sf.ezmorph:ezmorph:jar:1.0.6:compile
+- joda-time:joda-time:jar:2.3:compile
+- junit:junit:jar:4.8.2:compile
+- org.mockito:mockito-all:jar:1.9.0:compile
\- commons-httpclient:commons-httpclient:jar:3.1:compile
我在问题中发帖的那个不是发送令牌响应,而是发出一个巨大的请求。希望对您有所帮助!