Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

To and From Hash

Ben edited this page Mar 16, 2016 · 5 revisions

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.

Getting Started

Here's how to get started with wit_bot in 3 steps:

  1. Learn how to use wit.ai
  2. Learn how to setup the gem
  3. Learn how to send a message

Dive Deeper

Create Bots

Integrate it

Clone this wiki locally