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