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

How can I use Botkit Middleware with Watson Assistant dialog server actions?

我关注了这个tutorial to deploy a Slackbot with Watson Assistant. The tutorial uses server actions in the dialog to directly interface with a database. To connect Slack with Watson Assistant the tutorial uses the Conversation connector. That works fine, but I am interested in how to do the same with Botkit and the Botkit Middleware provided by Watson Developer Cloud

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

实际上有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);
}