代码之家  ›  专栏  ›  技术社区  ›  data_henrik

如何将botkit中间件与watson助手对话框服务器操作一起使用?

  •  0
  • data_henrik  · 技术社区  · 6 年前

    我跟着这个 tutorial to deploy a Slackbot with Watson Assistant 是的。本教程使用对话框中的服务器操作直接与数据库交互。要将Slack与Watson助手连接,本教程使用对话连接器。那很好,但是我对如何使用botkit和 Botkit Middleware provided by Watson Developer Cloud 是的。

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   data_henrik    6 年前

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