Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store answers from users in Facebook bot chat?

Building a Facebook messenger bot using Claudia JS and plan on hosting on AWS Lambda.

I want to ask the user a series of questions.

When a user responds with an answer, I need to save that for later and once I have all the information I need, I will pass the answers to a function.

What is the best way to save this information?

I was thinking some caching layer such as redis but because that is stored in RAM I will lose it when lamda server shuts down. Mongodb apparently has a lot of overheads when connecting but will at least be persistent.

Perhaps just a simple mySQL server?

How does everybody else do it? I feel like there is a simple solution that I am missing.

like image 302
Stretch0 Avatar asked Jan 05 '23 09:01

Stretch0


1 Answers

I will first answer the part about how I'm doing it: I'm using a MongoDB. I toyed with the ideas you mentioned, but quickly crossed out in-memory solutions (Memcached, Redis) with the same reason. My final solution came down to either a relational DB or a noSQL like MongoDB. To be honest, at my project's scale, I did not think about robustly comparing performance between DB types.

With my particular feature "roadmap," I decided to go with Mongo to approach a more "OOP" style when dealing with the user "object" without having to explicitly define a user class, thanks to the normalized structure of Mongo. I understand the same could be done for MySQL, too, just that processing json data is more "object-like" for me and flask, i.e. user = getUserFromMongo, which gives me a dict in Python then I can just do user['first_name']. The codes belows will explain this simplicity:

Interact with MongoDB (Somehow this was feeling like... not having to write SQL commands for simple database interaction in Rails)

My user object data on MongoDB user

Finally, as to how I manage user input, I adopted Wit.ai's concept of context. I don't know how they do it exactly, but a context to me is the type of conversation purpose that is going on. I use it like a stack, and as soon as the current context is done, pop it off the context data of the user. For every message the bot receives, the program will get the current context and direct the flow. Whenever an unknown error occurs (exceptions handling), most likely because the user is saying something the bot doesn't understand, I clear the context data, too.

The good part about MongoDB is that I can shape the context however I want and treat it just as an object. A simple one is like {name: yelp-search, stage:ask-for-user-location}, and I imagine complex ones could be built on that structure, too. Of course, a stack implementation of the context does not deal with complex conversation with complex past reference.

I put my project on Github if you want to take a look at it.

like image 82
Hung Tran Avatar answered Jan 10 '23 07:01

Hung Tran