23

I have created a little chat bot following the tutorial of Esther Crawford. This bot check the string that enter the user and respond with one of my json answer.

For example if I say "hello" the bot 'll respond "Hey, I'm so glad you set EstherBot up!"

script.json

{
    "HELLO": "Hey, I'm so glad you set EstherBot up!",
    "I LOVE YOU": "Awh, shucks! I love you too!",
    "CONNECT ME": "",
    "DISCONNECT": "Roger that, EstherBot is back."
}

My question is: How to edit my script.json in JavaScript?

For the moment when the user enters an unknown string, the bot will answer that it doesn't understand.

script.js

if (!_.has(scriptRules, upperText)) {
    return bot.say('Sorry I dont understand').then(() => 'speak');
}

How could I get this unknown string of the user and add it in my script.json file by editing in JavaScript my JSON-file ?

I want that my bot learn by itself, if he doesn't know the answer it should automatically add the question of the user to the script.json file, ask the user for an answer and then add this answer in the script.json file too.

Many thanks for your help! You'll find this project on git with the full code here.

1
  • 1
    One, do you want to change the in memory representation of the JSON file? Or the actual JSON file stored on the web server? Two, if the latter, the question is more "how can I post this new data, filter it securely, and save it to the JSON file?" Commented Apr 26, 2016 at 15:47

3 Answers 3

22

You can't save in a file using client-side script, you have to use some server-side scripting like PHP, NodeJS etc. to save something in a file.

For example in NodeJS you can use the fs library:

fs = require('fs');
var name = 'fileName.json';
var m = JSON.parse(fs.readFileSync(name).toString());
m.forEach(function(p){
    p.name= m.name;
});
fs.writeFileSync(name, JSON.stringify(m));

Hope it helps

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah Heroku provides support for NodeJS see : devcenter.heroku.com/articles/…
sorry m is not a function
8

Unfortunately, without having server-side code - that would take a request & store the file on the server - it is not posible to save files.
However you could use localStorage.

For example:

//If statement to check if localStorage already stored.
if (!localStorage.script) {

    localStorage.script = JSON.stringify({
"HELLO": "Hey, I'm so glad you set EstherBot up!",
"I LOVE YOU": "Awh, shucks! I love you too!",
"CONNECT ME": "",
"DISCONNECT": "Roger that, EstherBot is back."
}) ;

}

//This will load the localStorage data into an object in the varaible called botScript
var botScript = JSON.parse(localStorage.script) ;

function saveScript() {

    //This will save the current object to the localStorage.
    localStorage.script = JSON.stringify(botScript) ;

}

You can read more at http://www.w3schools.com/html/html5_webstorage.asp.
You could also use session storage if you want it to be temporary.

Comments

6

Assuming you got your json already loaded:

var json = '{"hola":"ciao"}';

//Parse the JSON: convert it into an object
var parsedJson =JSON.parse(json);

//add whatever you want
parsedJson.hi = 'bye';

Your json var will look like:

Object {hola: "ciao", hi: "bye"}

Then you can convert the object to string doing JSON.stringify(parsedJson) and write back to disk/DB if you're manipulating it in your backend (ie.: NodeJs).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.