In this blog post I will show how to personalize your Messenger Chatbot. It will send a special welcome to first time users, while providing historical information to returning users. This requires that my chatbot be able to maintain state. I need to save information about my users each time they visit and then read this information each time they return.
the Intwixt Data Modeler
The first step when reading and writing data is to define your models using the Data Modeler (https://my.intwixt.com/#/data). For my use case, I'll add a new global data model named Users. This model will track all unique users who contact my Messenger Chatbot and log their visit count.
Data Model Basics
The Intwixt Data Model format is based on the assumption that it should be effortless to learn the basics of the modeling system, while still be able to define complex concepts like arrays, objects, and anonymous elements as necessary. At it's most basic, defining a data model is no more difficult than entering a comma-separated list of field IDs. For instance, if you need to define a model that includes first, middle, and last names, this is the format (I added carriage returns for readability):
By default, all fields you define are assumed to be required. If you want to designate a field to be optional, you may prefix it with with a question mark. For example, in the following model, the middle field is optional.
If your data model defines data that will be aded to the Intwixt NoSQL Data Store, you may specify which fields get indexed, allowing you to query relevant data at a later time. For example, if you want to be able to find a user by their last name, you could add an index to the last field by prefixing it with an equals sign:
When saving data to the NoSQL Data Store you must also specify which field will serve as the primary key. If you do not provide this flag, Intwixt will save your model, but it will not auto-provision your NoSQL Data Store, as it will not know how to organize your data. In the following example, I have added a primary field named id. I designate it as the key field by prefixing it with a dollar sign.
In addition to optional (?), index (=), and key ($) flags, Intwixt has other flags to help you enhance your data model such as adding a custom title, tip, or default value. For example, if you want to add a custom title You can do so by prefixing the title with a double quote.
The Data Modeler includes a real-time view of any changes you make to your schema, allowing you to validate in real time that your data model is correct. Here is a video of the User data model being created. At the end of the video you can see that a NoSQL data table was automatically provisioned, because I included a primary key field ($psid) in the model.
The data model is defined and the noSQL data table has been provisioned. I will now create a Messenger Chatbot by importing one with a basic greeting message.
I will now extend my messenger bot by adding two additional botlets. The first will be used to add a new record to the User table when a first-time user contacts my bot. Notice how I take advantage of the HTTP Test Utility within the designer to test my bot in real time and validate that it properly saves user data.
The second botlet will increment the visit count for returning users. It takes advantage of the rich API offered by Intwixt's embedded NoSQL Data store. This botlet requires four local data models to define its HTTP signature. They define its request, response, error, and context models.
The request model defines the Page Scoped ID and identifies the User record with the field to increment.
$psid"Page Scoped ID
The response model defines two fields, most importantly the incremented value.
$psid"Page Scoped ID,
The error model defines a 404 error code that is returned when the botlet tries to increment the visit count for a user that doesn't exist.
404"User not found
The context model is used to tell the API where to look for the Page Scoped ID. Since I want this API to be properly RESTful, I'll use a path variable and the HTTP PATCH verb when it comes time to define it.
$psid"Page Scoped ID+path
I can now create the botlet that will increment the visit count. Its HTTP Signature will be based upon my local data models. I'll also test my bot using the HTTP Test Utility to validate it is properly configured.
With my two data botlets now complete and tested, I can modify my original Messenger Chatbot to return a more personalized greeting. When the bot receives a message, it will increment the visit count for the user. If that call fails with a 404, it means this is a first-time user. I handle the error and conclude by creating a new entry in my User table.
Here's a screenshot of the the message exchange on Messenger. Notice how the message count changes to 2 on the second exchange.
You can view your bot's data in real time as it's being added and updated. And you can manually edit the data. It's as easy as using a spreadsheet, allowing you to copy, paste, or export your data. In the following video I show the User data table. Note how a data record automatically appears and is updated as my bot interacts with new and returning users.
Sending a personalized greeting only scratches the surface of what's possible once you learn how to save state with your chatbot. It opens an entire new set of capabilities when modeling long term conversations with your users.
If you want to save time building this bot and would rather import it, go to your bot list at https://my.intwixt.com/#/bots and click IMPORT BOT. When the Import Bot dialog loads, choose the bot titled, "Personal Greeting (Messenger)".