Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intercepting Conversation answer to query database #24

Closed
clalevee opened this issue Jan 3, 2017 · 7 comments
Closed

Intercepting Conversation answer to query database #24

clalevee opened this issue Jan 3, 2017 · 7 comments
Assignees

Comments

@clalevee
Copy link

clalevee commented Jan 3, 2017

I'm trying to develop a bot using botkit-middleware-watson for FB messenger.
Dialog Conversation <-> Messenger runs good.

But now I want to intercept answer from Watson Conversation (event middleware.after ?), query a DB and answer directly to Conversation without using Messenger. Conversation will then interpret/format and answer to the user, using data retrieved from DB.

So my program have to (in event middleware.after ?)

  • receive conversationResponse
  • query DB, and get answer (son)
  • update Conversation Payload
  • answer to Conversation

Do you have any idea ?

@aarora91 aarora91 self-assigned this Jan 3, 2017
@stevenoh93
Copy link
Contributor

You can do this in the after callback as you said, but you'll have to call Conversation on your own.

middleware.after = function(message, conversationResponse, callback) {
    // Call db
    mydb.makeQuery(queryString, function(result) {
        payload.context.dataToChange = result;
        conversation.message(payload, function (err, response) {
            callback(null, response);
        });
    });
}

@clalevee
Copy link
Author

clalevee commented Jan 4, 2017

here is the test I did :

middleware.after = function(message, conversationResponse, callback) {
    
   if (conversationResponse.context) {
         conversation.message({
              workspace_id: process.env.WORKSPACE_ID,
              input: {'text': 'Test message'},
              context: conversationResponse.context
         }, function (err, response) {
               if (err)
                  console.log('error:', err);
               else
                  console.log(JSON.stringify(response, null, 2));
               callback(null, response);
         });
   }
   else callback(null, conversationResponse);

} 

But, I don't receive any answer from Conversation or see log message in console...
Can conversation (from "conversation.message" function) be used outside of botkit middleware ?

@stevenoh93
Copy link
Contributor

Sorry, should've said middleware.conversation. Give that a try and let us know if that works.

@stevenoh93 stevenoh93 self-assigned this Jan 4, 2017
@clalevee
Copy link
Author

clalevee commented Jan 4, 2017

It's better, it nearly works, I can call Conversation and receive a new payload
My new code

 middleware.conversation.message({
                          workspace_id: process.env.WORKSPACE_ID,
                          input: {'text': 'SQL100'},
                          context: conversationResponse.context
                     }, function (err, response) {
                           if (err)
                              console.log('error:', err);
                           else {
                             console.log("Generated anwser ");
                             console.log(response);
                             callback(null, response);
                           }
 });

But, even if response seems good ( console.log(response) ), the callback doesn't generate an answer to Messenger.

@clalevee
Copy link
Author

clalevee commented Jan 4, 2017

[INFO]
This app is running on Bluemix.
To be able to detect that conversation.message is not defined, I had to add Try-Catch.

@stevenoh93
Copy link
Contributor

Is there no response coming back on Messenger or are you getting something outdated?

If you want to change the response that's being sent back to Messenger, you have to update the message object passed in as a parameter. So right before the callback, you'd want to do this:

message.watsonData = response;

@clalevee
Copy link
Author

clalevee commented Jan 4, 2017

I was something outdated..
So, I added message.watsonData = response before callback as mentioned and it works !!

middleware.conversation.message({
                          workspace_id: process.env.WORKSPACE_ID,
                          input: {'text': 'SQL100'},
                          context: conversationResponse.context
                     }, function (err, response) {
                           if (err) {
                              console.log('error:', err);
                              ....
                            }
                           else {
                             console.log("Generated anwser ");
                             console.log(response);
                             message.watsonData = response;
                             callback(null, response);
                           }
 });

Now, i just have to really call DB.
Thx for your rapid and high quality assistance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants