Skip to content

Commit

Permalink
Merge pull request #35 from dmitrizzle/v1.2.1
Browse files Browse the repository at this point in the history
V1.2.1
  • Loading branch information
dmitrizzle authored Jul 13, 2017
2 parents 2b7fd1c + cf30454 commit 0e3f20b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"authors": [
"dmitrizzle <[email protected]>"
],
"description": "Simple chatbot bubble creator based on JSON",
"description": "Simple chatbot bubble creator based on JSON conversation structure",
"license": "MIT",
"private": true,
"ignore": [
Expand Down
19 changes: 13 additions & 6 deletions component/Bubbles.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ function Bubbles(container, self, options) {
inputCallbackFn = options.inputCallbackFn || false; // should we display an input field?

var standingAnswer = "ice"; // remember where to restart convo if interrupted

var _convo = {}; // local memory for conversation JSON object
//--> NOTE that this object is only assigned once, per session and does not change for this
// constructor name during open session.



Expand All @@ -23,7 +27,7 @@ function Bubbles(container, self, options) {
var inputWrap = document.createElement("div");
inputWrap.className = "input-wrap";
var inputText = document.createElement("textarea");
inputText.setAttribute("placeholder", "Type your question...");
inputText.setAttribute("placeholder", "Ask me anything...");
inputWrap.appendChild(inputText);
inputText.addEventListener("keypress", function(e){ // register user input
if(e.keyCode == 13){
Expand All @@ -35,7 +39,7 @@ function Bubbles(container, self, options) {
// callback
typeof callbackFn === "function" ? callbackFn({
"input" : this.value,
"convo" : convo,
"convo" : _convo,
"standingAnswer": standingAnswer
}) : false;
this.value = "";
Expand All @@ -61,12 +65,15 @@ function Bubbles(container, self, options) {

// accept JSON & create bubbles
this.talk = function(convo, here) {
this.convo = convo;
this.reply(this.convo[here]);

// all further .talk() calls will append the conversation with additional blocks defined in convo parameter
_convo = Object.assign(_convo, convo); // POLYFILL REQUIRED FOR OLDER BROWSERS

this.reply(_convo[here]);
here ? standingAnswer = here : false;
}
this.reply = function(turn) {
turn = typeof turn !== "undefined" ? turn : this.convo.ice;
turn = typeof turn !== "undefined" ? turn : _convo.ice;
questionsHTML = "";
if(turn.reply !== undefined){
turn.reply.reverse();
Expand All @@ -88,7 +95,7 @@ function Bubbles(container, self, options) {
// navigate "answers"
this.answer = function(key){
var func = function(key){ typeof window[key] === "function" ? window[key]() : false; }
this.convo[key] !== undefined ? (this.reply(this.convo[key]), standingAnswer = key) : func(key);
_convo[key] !== undefined ? (this.reply(_convo[key]), standingAnswer = key) : func(key);
};

// api for typing bubble
Expand Down
6 changes: 3 additions & 3 deletions component/examples/4-advanced-keyboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@
// this is where you may want to connect this script to NLC backend.
inputCallbackFn: function(o){

// do this if no answer matched
// add error conversation block & recall it if no answer matched
var miss = function(){
givemeBubbles.talk({
"ice": {
"i-dont-get-it": {
"says":
[
"Sorry, I don't get it 😕. Pls repeat? Or you can just click below 👇",
],
"reply" : o.convo[o.standingAnswer].reply
},
})
}, "i-dont-get-it")
}

// do this if answer found
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "chat-bubble",
"version": "1.0.0",
"description": "Simple chatbot bubble creator based on JSON",
"version": "1.2.0",
"description": "Simple chatbot bubble creator based on JSON conversation structure",
"main": "component/Bubble.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down

0 comments on commit 0e3f20b

Please sign in to comment.