Skip to content

Accessing and Writing memory.json

Innocent Bystander edited this page Aug 8, 2015 · 1 revision

High-level

bot.memory.exists(["<keyname>", "<keyname>", ...])

  • checks whether memory["<keyname>"]["<keyname>"][...] exists
  • returns None if the key path cannot be found

Helpers for per-user or per-conversation storage

The bot supports per-user and per-conversation memory, meaning that you can store keys and their values for specific users or conversations.

bot.user_memory_set(event.user.id_.chat_id, <keyname>, <keyvalue>)

  • automatically initialises the user memory in memory.json
  • stores <keyvalue> in <keyname>

bot.user_memory_get(event.user.id_.chat_id, '<keyname>')

  • automatically initialises the user memory in memory.json
  • returns value stored in <keyname> or None if unset or null

bot.conversation_memory_set(event.conv_id, '<keyname>', <keyvalue>)

  • automatically initialises the conversation memory in memory.json
  • stores <keyvalue> in <keyname>

bot.conversation_memory_get(event.conv_id, '<keyname>')

  • automatically initialises the conversation memory in memory.json
  • returns value stored in <keyname> or None if unset or null

Memory Examples

Low-Level

Low-level functionality require exception handling. The type of exceptions raised will be listed as part of the documentation here.

bot.memory.set_by_path(["<keyname>", "<keyname>", ...], <somevalue>)

  • raises TypeError if it cannot create the path
  • can only create a new key if all the preceding keys are available
  • use bot.memory.exists(["<keyname>", "<keyname>", ...]) to check availability of the key path before executing this command

bot.memory.get_by_path(["<keyname>", "<keyname>", ...])

  • raises KeyError or TypeError if keys don't exist

bot.memory.pop_by_path(["<keyname>", "<keyname>", ...])

  • deletes/removes the last key specified from the preceding path
  • raises KeyError or TypeError if keys don't exist
Clone this wiki locally