Ismini V.
Ismini V.

Reputation: 11

Can you specify the "scope" of a keyword in botkit, so the keyword can show something different after a specific thread?

The bot I'm trying to build has a question structure as such:

Would you like to learn about the news, or the weather?

-The weather

For which city would you like to check?

-New york

The aim is for the specified keyword New York, the bot answers with the specific weather url. If you answer "news" and then "new york", it would answer the url that is about news in new york. There are a lot of alternative choices in the bot, so it is difficult to find a solution to seperate the keywords with the same name.

Upvotes: 0

Views: 27

Answers (1)

stacj
stacj

Reputation: 1121

I got you a simple example you can work with.

First we define some kind of structure to hold every answer tree and it's leaves. This is a meaningful format of the context you may use:

{
  q: "question",
  a: {
    "answer1": {
      q: "question2",
      a: {
        "answer1": {
          ...
        },
        "answer2": {
          ...
        }
      }
    },
    "answer2": {
      q: "question2",
      a: ...
    }
  }
}

Here you got some code to make the main logic of the simple bot logic even clearer:

const ctx = {q:"Would you like to know about weather or news?",a:{weather:{q:"For which city do you want ot know the current weather? (london or berlin)",a:{london:{q:"Blablbla weather... London!"},berlin:{q:"Blablabla weather... Berlin!"}}},news:{q:"For which city do you want ot know the current news? (london or berlin)",a:{london:{q:"Blablbla news... London!"},berlin:{q:"Blablabla news... Berlin!"}}}}};

// create button with text and callback arguments
const createButton = (txt, cb) => {
  const b = document.createElement("button");
  b.innerText = txt;
  b.onclick = () => {
    [...document.body.querySelectorAll("button")].forEach(e => e.remove());
    cb(txt.toLowerCase())
  }
  document.body.appendChild(b);
}


// your main loop
const bot = (ctx) => {
  alert(ctx.q); // display / alert question
  const cb = (a) => (ctx = ctx.a[a], bot(ctx)); // callback goes to next inner context
  ctx.a && Object.keys(ctx.a).forEach(e => createButton(e, cb)); // create buttons
}

// startup call
bot(ctx);

Upvotes: 0

Related Questions