SO-ChatBot
SO-ChatBot copied to clipboard
Import and export functions: save and restore the bot's memory
Occasionally, in Root Access, we change the way our bot is hosted. When we change, say, from one "browser" to another, normally the bot's HTML5 storage (bot.memory
) would be lost.
(Aside: the Root Access bot is currently being hosted headless using PhantomJS with my driver to perform the authentication step and load SO-ChatBot into the JS context. It has very recently migrated from running a headed Firefox in xrdp.)
To ease migration and preserve our many learned commands, Jimmy Hoffa and I co-authored a pair of commands for importing and exporting the HTML5 storage as base64'ed JSON.
!!export
will cause the bot to JSON.stringify
its memory; base64 encode it; and paste it as a multi-line chat message in chat (multi-line messages can be as long as needed, as long as any one line doesn't exceed a certain character limit; this trick is used in Root Access's patched !!listcommands
as well.)
A bot owner can then paste (as raw UTF-8 text) the results of the !!export
onto a pastebin site that lets you grab the raw file. For instance, gist.github.com
with rawgithub
, or pastebin
.
Once you have the paste URL, you can load your new bot with no memory, and run !!import http://url...
and it'll XHR to that URL, get the data as UTF-8, base64 decode it, parse it into JSON, load it into the bot's memory, and save the memory to the HTML5 web storage backing store.
This is not something you do often, but it would definitely ease the transition from one browser to another; for instance, from Firefox to PhantomJS, or from whatever we're using to the eventual (we hope!) NodeJS port of the bot.
I'd prepare a patch, but I'm a little busy, and it's just two files here and here. These are not communal commands, and require owner privs to execute, so that random people can't load arbitrary JSON into our bot's storage (that would be, at least, a functionality problem potentially, if not also a security risk). There's still a non-zero risk considering how many people the bot considers an owner (site mods, room owners, etc), but it's better than nothing.
I really like this idea, and it is one step closer to online memory storage (which I really need to get on! someone kick me in the ass to write that)
Rob, would you like me to split the core functionality of import and export out into a separate file that isn't directly tied to the import and export commands? I'm asking because I'm thinking you might be able to use the core functionality of b64'ing the memory in your "online memory storage" concept. You could set a timer that syncs the b64 memory blob to the online memory storage each time it's mutated or something like that (or throttled to once per 60 seconds if there's more than one mutation within a 60-second sliding window).
Why b64 instead of cleartext? The cleartext will always be smaller.
copy(
'var o=' + JSON.stringify(localStorage) + '; Object.keys(o).forEach(function (k) { if (k.indexOf("bot_") === 0) localStorage[k] = o[k]; });'
)
That creates the ready-to-run dump of the memory. Run that when you want to save, paste the result into gist or whatever, run the result when you want to load.
@Zirak Wouldn't that have to be done from the console of a headed web browser? I run it headless using so-chatbot-driver, so it's not very easy to get to the client JS console...
TODO:
Refresh bot memory without requiring a restart. Maybe make the GitHub requests asynchronous.