-
Notifications
You must be signed in to change notification settings - Fork 3
To and From Hash
Most wit_bot objects can be converted to a hash to be stored for later use, and then back into the original object.
This is extremely useful if you are using messages that come to you via webhooks, where you can't easily have an in-memory representation of every message or thread (although entities, intents, and states are stored in memory).
Objects that are convertible to a hash respond to #to_hash
. Although the object itself is hashed, the hash may include other Objects. You can use ActiveSupport's #as_json
method to recursively convert the object into a hash.
Let's convert a thread to a JSON-compatible object with ActiveSupport's #as_json
:
hashed_thread = thread.as_json
Now, to convert it back to a thread, just call WitBot::MessageThread#from_hash
:
WitBot::MessageThread.from_hash hashed_thread
Pro-Tip: The #metadata
variable on threads can be used to store a hash or other JSON types alongside a thread. It is not used by wit_bot, and is automatically encoded/decoded along with the thread.
Here's an example using conversations, and another with conversations and bot messages.
Down here?
Cool.
I should add something somewhat interesting for you to read...
Here's how to get started with wit_bot in 3 steps:
- Learn how to use wit.ai
- Learn how to setup the gem
- Learn how to send a message
- Learn how to use message threads
- Learn how to use context
- Learn how to use conversations
- Learn to use the REPL
- Learn how to use conversations
- Learn how to use participants
- Learn how to use bots