ibm-cloud - 如何将 Botkit 中间件与 Watson Assistant 对话服务器操作结合使用?
问题描述
我按照本教程使用 Watson Assistant 部署了一个 Slackbot。本教程使用对话框中的服务器操作直接与数据库交互。要将 Slack 与 Watson Assistant 连接起来,本教程使用了对话连接器。这很好用,但我对如何使用 Botkit 和Watson Developer Cloud 提供的 Botkit 中间件做同样的事情很感兴趣。
如何使用无服务器操作,如何获取和传递必要的 API 密钥?
解决方案
实际上有代码演示了如何为 IBM Cloud Functions 配置 API 密钥并将其作为上下文变量传递给 Watson Assistant。它利用该before
方法将 API 密钥添加到上下文变量中。该值与其他与应用程序相关的凭据一起配置在单独的文件中。代码测试上下文变量和键是否存在,否则添加:
middleware.before = function(message, conversationPayload, callback) {
// Code here gets executed before making the call to Conversation.
// retrieve API Key from environment and split it into user / password
var arr=process.env.ICF_KEY.split(":");
// check if context exists
if (typeof(conversationPayload.context) === 'undefined') {
var context={context: {}}
Object.assign(conversationPayload, context);
}
// if credentials already exists, we don't have to add them
// else add credentials under private element in context
if (typeof(conversationPayload.context.icfcreds) === 'undefined') {
var privcontext = {"private": {icfcreds: {user: arr[0], password: arr[1]}}};
Object.assign(conversationPayload.context, privcontext);
}
// log the payload structure for debugging
// console.log(conversationPayload)
callback(null, conversationPayload);
}
推荐阅读
- react-native - 如何从 React Native 中的目录导入矢量图标?
- kendo-ui - Kendo UI 多选标记模式 - 基于键入的值进行过滤,而不将键入的文本传递给服务器
- javascript - 如何使用本机反应在firebase中显示日期
- c++ - 如何编写 CMakeLists.txt 来构建一些源代码及其示例代码?
- docker - 如何重用来自 `RUN --mount=type=cache` docker build 的缓存?
- sql - 无法实现外键约束 »FK_0e4022833a9efc062c01637e552« - 复合主键有问题?
- javascript - document.write 写入“对象”而不是文本
- mysql - MySQL 数据库或简单的 JSON 文件,哪一个会提高性能和存储效率?
- c - 如何从函数返回动态分配的二维矩阵?
- powershell - Powershell脚本任务中的TF Merge命令不起作用