As read on ThoughtSpot’s blog – Written by Ashish Shubham
Member of Technical Staff


One of the harder problems that chatbot developers face is, how to maintain the context of conversation. While all the popular frameworks provide an opinionated take on how to maintain this context, none of them seem to be either simple or complete.

At ThoughtSpot we built Spot, a bot to get insights into your data. We used a reactive approach to maintain the conversation context.

This can be summarised as:

  • Whenever the bot send a message, set(push) <a matcher method m(), a context callback> on the stack.
  • On receiving a message, match the message using the matcher methods in FIFO order. Call the matched context callback.

The elegance of this approach is that the pushed callback captures the state of variables as a part of its closure context. When the callback is called, this state is retained and we can time travel back to the state when the context was actually set.

Let me illustrate with an example:

User: “List my dashboards”.
# Initialize an empty context for the user.

# Push </(sales pipeline|marketing|engineering release tracker)/,
# getChartsInDashboard>
Bot: Avaliable Dashbooards are:
1. Sales Pipeline
2. Marketing
3. Engineering Release tracker

User: “Marketing”
# Match “marketing” against the stack, we see it matches.
# And call getChartsInDashboard with {dashboardName}

# Push </([1–3])/, getChartInDashboard>
Bot: So these are the Charts in {dashboardName=marketing}, select the one you want to see.:
1. Monthly Qualified Leads(MQLs) Weekly.
2. Paid Traffic by source
3. Top 10 Webpages, L7

User: “sales pipeline”
# Now, since sales pipeline does not match the digit matcher we specified, it
# goes to the previous item in the stack where it finds a match. Thus calling
# getChartsInDashboard again.

Bot: Alright, here are the Charts in {dashboardName=sales pipeline}, select the one you want to see.:
1. Total Open Pipeline trend.
2. Open Pipelie by Close Q by Stage
3. S1s created monthly

# Win!

This was a simple example where we used context stack to match the user intent. Things become more interesting when we are able to use the closure variables in the called callback.

Bot-context is a simple yet powerful way to manage conversation context in your shiny smart chatbot. Its open source, you are just an npm installaway from using it.

Let us see the complete example using bot-context:



Thats it! The above code is all you need to handle all permutations and combinations of the answers the user may give. Please visit the github of the bot-context project for more details and discussion.

Leave a Reply