0

my question is regarding the implementation of this snippet of javascript code into my chatbot:

smooch.conversations.sendMessage('app_5790dca8cab9a256005c0148', {
    text: 'Fabric',
    role: 'appMaker',
    actions: [
      {
        type: 'postback',
        text: 'fabric instructions',
        payload: 'egg_fabric'
      }
    ]
}).then(() => {
    // async code
});

Here is my script.json file that corresponds to this specific postback:

"HOW CAN I REMOVE AN EGG STAIN?": "%[Fabric](postback:egg_fabric) %[Surface](postback:egg_surface)",

The reason I am asking this question is because I want to have multiple "surface" and "fabric" buttons throughout the chat but I want the answer the bot spits out to correspond to the question most recently asked. Thanks for your help!

Andrew Lavers
  • 8,023
  • 1
  • 33
  • 50

2 Answers2

0

What you're looking for can't be done this with script.json, you'll have to define your own state functions in script.js, and your own postback event handling.

In this answer there are some suggestions as to how you can do this with the smooch-bot-example project (which is what estherbot was forked from).

To give you some context, script.js is where the actual bot logic lives. The script.json you're looking at is a shortcut that was introduced with estherbot to make it easier to define keyword-based bots. When your bot runs, the JSON actually gets compiled into states as if the were defined in script.js in the first place. What you're attempting to build will require more than the static keyword --> behaviour mapping that the script.json is limited to.

Community
  • 1
  • 1
Andrew Lavers
  • 8,023
  • 1
  • 33
  • 50
0

Following up on Andrew's answer above:

Check the smooch-bot-example project, but maybe start by looking into the webhook implementation file for the postbacks (heroku/index.js) instead of the bot logic/flow definition file (script.js) file:

Within the heroku folder of the repo, check the index.js file. The function handlePostback(req, res) (should be around line 109) should help you get started. E.g. change it to something like this:

From:

function handlePostback(req, res) {
    const postback = req.body.postbacks[0];
    if (!postback || !postback.action) {
        res.end();
    }
    createBot(req.body.appUser).say(`You said: ${postback.action.text} (payload was: ${postback.action.payload})`)
    .then(() => res.end());
}

To something like:

function handlePostback(req, res) {
    const postback = req.body.postbacks[0];
    if (!postback || !postback.action) {
        res.end();
    }

    const sAction = postback.action.text;
    const sPayload = postback.action.payload;

    switch (sAction) {
     case "egg_fabric":
        //something something
        break;
     case "egg_surface":
        //something something
        break;        
     default:
        //something something
    }
}

Hope that helps.

54chi
  • 1