首页 > 解决方案 > 如何将 Botkit 中间件与 Watson Assistant 对话服务器操作结合使用?

问题描述

我按照本教程使用 Watson Assistant 部署了一个 Slackbot。本教程使用对话框中的服务器操作直接与数据库交互。要将 Slack 与 Watson Assistant 连接起来,本教程使用了对话连接器。这很好用,但我对如何使用 Botkit 和Watson Developer Cloud 提供的 Botkit 中间件做同样的事情很感兴趣。

如何使用无服务器操作,如何获取和传递必要的 API 密钥?

标签: ibm-cloudwatson-conversationbotkitwatson-assistantibm-cloud-functions

解决方案


实际上有代码演示了如何为 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);
}

推荐阅读