watson-developer-cloud / botkit-middleware

A middleware to connect Watson Conversation Service to different chat channels using Botkit
https://www.npmjs.com/package/botkit-middleware-watson
Apache License 2.0
207 stars 257 forks source link

Can't set context before middleware starts #203

Closed AutomataVM closed 4 years ago

AutomataVM commented 4 years ago

I wanted to add the user's name before watson middleware started it's operation, like this:

watsonMiddleware.before = function(message, conversationPayload, callback) {
//Passing values to conversation.
conversationPayload.context.user_name= 'user_name';
}

in my end, the next message text's output should be hello, $user_name ! It's nice to meet you

Though I get a watsonErrorwhen processing the answer

controller.hears(
    ['.*'],
    'message',
    async function(bot, message) {
    console.log(message);
      if (message.watsonError) {
        await bot.reply(
            message,
            "I'm sorry, but for technical reasons I can't respond to your message",
          );
    } else {

      await bot.reply(message, message.watsonData.output.text.join('\n'));
  }
},
);
   Error: Missing required parameters: workspace_id
       at Object.getMissingParams (C:\xampp\htdocs\webchatbot\node_modules\ibm-c
loud-sdk-core\lib\helper.js:94:11)
       at AssistantV1.message (C:\xampp\htdocs\webchatbot\node_modules\ibm-watso
n\assistant\v1.js:119:50)
       at C:\xampp\htdocs\webchatbot\node_modules\ibm-watson\assistant\v1.js:114
:23
       at new Promise (<anonymous>)
       at AssistantV1.message (C:\xampp\htdocs\webchatbot\node_modules\ibm-watso
n\assistant\v1.js:113:20)
       at Object.<anonymous> (C:\xampp\htdocs\webchatbot\node_modules\botkit-mid
dleware-watson\lib\utils.js:72:45)
       at Generator.next (<anonymous>)
       at C:\xampp\htdocs\webchatbot\node_modules\botkit-middleware-watson\lib\u
tils.js:23:71
Naktibalda commented 4 years ago

before hook function must return the payload:

watsonMiddleware.before = function(message, conversationPayload, callback) {
  //Passing values to conversation.
  conversationPayload.context.user_name= 'user_name';
  return conversationPayload;
}
AutomataVM commented 4 years ago

thank you! this is what I did

watsonMiddleware.before = async (message, payload) => {

  if (typeof(payload.context) === 'undefined') {
    var context = {
        context: {
        user_name: '',
        age: 0,
        residence: '',
        civil_state: '',
        prev_studies: '',
    }}
    Object.assign(payload, context);
}
  return payload;
};