Skip to content

Instantly share code, notes, and snippets.

@endofline
Created July 2, 2015 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endofline/c509bd834d5354b10fcc to your computer and use it in GitHub Desktop.
Save endofline/c509bd834d5354b10fcc to your computer and use it in GitHub Desktop.
Conversation Memory (Alpha)
import datetime
import plugins
from hangups.ui.utils import get_conv_name
convmem = {}
def _initialise(bot):
_memory_load(bot)
_update_conversation_list(bot)
plugins.register_admin_command(["dumpconv"])
plugins.register_handler(_watch_message, type="allmessages")
plugins.register_handler(_watch_rename, type="rename")
plugins.register_handler(_watch_member, type="membership")
plugins.register_shared('convmem.removeconv', _conv_remove)
def _update_conversation_list(bot):
for conv in bot._conv_list.get_all():
_conv_update(bot, conv, source="init")
def _watch_rename(bot, event, command):
_conv_update(bot, event.conv, source="renm")
def _watch_message(bot, event, command):
_conv_update(bot, event.conv, source="chat")
def _watch_member(bot, event, command):
_conv_update(bot, event.conv, source="mmbr")
def _conv_update(bot, conv, source="unknown"):
conv_title = get_conv_name(conv)
if conv.id_ not in convmem:
convmem[conv.id_] = {}
convmem[conv.id_] = {
"title": conv_title,
"source": source,
"updated": datetime.datetime.now().strftime("%Y%m%d%H%M%S")}
_memory_save(bot)
def _conv_remove(bot, convid):
if convid in convmem:
del(convmem[convid])
print("convmem: removing {}".format(convid))
_memory_save(bot)
def _memory_save(bot):
bot.memory.set_by_path(['convmem'], convmem)
bot.memory.save()
def _memory_load(bot):
if bot.memory.exists(['convmem']):
convs = bot.memory.get_by_path(['convmem'])
for convid in convs:
convmem[convid] = convs[convid]
def dumpconv(bot, event):
for convid, convdata in convmem.items():
print("{} {} {}".format(convid, convdata["source"], convdata["title"]))
print("total: {}".format(len(convmem)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment