guidone / node-red-contrib-chatbot

Visually build a full featured chat bot for Telegram, Facebook Messenger, Whatsapp and Slack with Node-RED. Almost no coding skills required.
http://red-bot.io
933 stars 189 forks source link

Canned Replies #58

Closed ram1505 closed 7 years ago

ram1505 commented 7 years ago

How can I create a canned connected conversation with anyone who initiates a chat via FB messenger? What I'm trying to do is to mimic API.AI with Redbot. I would like to get the response from a user for a multiple choice question and make the output as a single JSON formatted output.

For instance, you can think of a scenario like Q1. What is your Age? Q2. Male or Female? Q3. Occupation Q4. Phone Number

While asking these questions, I would like the chatbot to follow a set of pre-defined question that will guide the user to mandatorily give the expected output (like how API.AI and Rivescript does).

Can such a scenario be built using Redbot? I would like to mimic how the API.AI works with redbot.

guidone commented 7 years ago

That is an interesting question. Yes it's possible, I've simplified the answer with this flow

api_like_flow

The chatbot here is used like a state machine, in the default state it try to parse the user input trying to get age, gender and the occupation, when the state (the chat context variable "topic") turns into "complete" the flow proceed in the upper side of the flow. This is the purpose of the topic node

topic_node

The parse node tries to extract values from the natural language, I've added just a few combination, but there's no limit

listen_node

Refer to the listen node docs. For example the rule "I" + "am" + "[number]->age" is satisfied if the user sentence contains the keyword "I", the keyword "am" and a number (it doesn't matter if it's written in number or letters) and store it in the "age" variable of the chat context. In order to catch a sentence that doesn't match anything add a rule with just "*" as last element and provide any kind of feedback.

function_node

The function node gets all variables from the chat context and redirects the flow to the right output if some variable is missing, for example if the gender is missing it goes through the second output triggering a question with inline buttons.

If all variables are present and validated, the topic is switched to "complete" and the flow is redirected in the upper part.

Here is the exported flow

api_ai_flow.txt

ram1505 commented 7 years ago

Awesome! Thank you so much for such a detailed explanation. Let me try this out and feedback to you.

ram1505 commented 7 years ago

For some reason, the example that you gave is not working for me. It keeps throwing Age,, in my Telegram chat window.

ram1505 commented 7 years ago

Also, is it possible to use the Listen node with Quick replies in FB? I've plans to add some quick replies for each question, for instance

1) Fav color? (quick reply will have Red, Green, White etc) 2) Fav place? (quick reply will have something like London, Paris, Singapore etc) 3) Fav food? (Pasta, Pizza, etc)

ram1505 commented 7 years ago

Hi, Any clue on why the example flow is not working?

guidone commented 7 years ago

It keeps asking the age because the listen node doesn't understand the sentence and immediately ask the first question. The example flow is quite basic.

The usability of the flow can be improved adding a final rule with just a "*" and connect that to message node that gives back a "I didn't understand" message to the user.

The listen node rules need to be constantly tuned to better understand the user input, here is the doc of the listen node

https://github.com/guidone/node-red-contrib-chatbot#parse-sentences

It uses this library for natural language processing

https://github.com/nlp-compromise/compromise

I suggest to enable the debug of listen node and check the console to see how the library split the entities of each sentence and then improves the rules for a better match

ram1505 commented 7 years ago

What I mean was, it doesn't even prompt any questions for me. It straightaway prints on debug "Age,,," and when I notice the topic it says complete. I'm sorry, but did the flow work for you?

guidone commented 7 years ago

Sorry, it worked the first time and then I cleaned up adding an extra null that was breaking

api_ai_flow.txt

ram1505 commented 7 years ago

Worked perfectly now. Thanks. I'll experiment with this example.

ram1505 commented 7 years ago

Oh wait..once the variable gets stored, it wont ask me the questions second time? When I talk to the bot, it already is throwing out the recorded variables based on my previous chat.

guidone commented 7 years ago

Yes the context variables are persisted unless manually removed (with the context node) or node-red is restarted

ram1505 commented 7 years ago

Ok, I got it working by having 4 separate Context nodes right after Telegram Receiver that are set to Delete the 3 variables and the topic upon each new message received. Is there a simpler way? I tried the below function and it didn't work. Not sure if chat.delete is a valid command.

var chat = msg.chat();
chat.delete('age');
chat.delete('gender');
chat.delete('job');
chat.delete('topic');

return msg;`

guidone commented 7 years ago

.remove() is the method to delete a variable from the chatcontext