简体   繁体   中英

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.

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 , ie user = getUserFromMongo , which gives me a dict in Python then I can just do user['first_name'] . The codes belows will explain this simplicity:

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

My user object data on MongoDB 用户

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.

i have also used mysql for chatbot but i have used NodeJS for the backend app.For that mysql module would be very helpful.

You need to store users' current state for the question answer session and also store the answer itself from the user and you need to make a switch or if-else-if case for asking questions to user based on its state as switch(state) and in cases of switch just update it's state.and you have user's facebook-id in event object of chatbot so that you can store data of each user individually with their state and question-answer in different table.

For eg define flags{1,2,3}

user's state will be 1 in begining so ask him for eg question-1 only,and store this as answer-1, you can do this by it's state checking, and after this update status to 2.

so,in this way you can ask each individual student question as per their state and answer him.

I've done the same in exact above manner.

Hope this would be helpful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM