如何将Botkit中间件与Watson Assistant对话框服务器操作配合使用?

时间:2018-06-26 13:26:19

标签: ibm-cloud watson-conversation botkit watson-assistant ibm-cloud-functions

我遵循了这个tutorial to deploy a Slackbot with Watson Assistant。本教程使用对话框中的服务器操作直接与数据库进行交互。要将Slack与Watson Assistant连接,本教程使用了会话连接器。效果很好,但我对如何使用Botkit和Botkit Middleware provided by Watson Developer Cloud感兴趣。

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

1 个答案:

答案 0 :(得分:1)

实际上有code that demonstrates how to configure the API key for IBM Cloud Functions and pass it as context variable to 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);
}