本文整理匯總了Python中rivescript.RiveScript.get_uservars方法的典型用法代碼示例。如果您正苦於以下問題:Python RiveScript.get_uservars方法的具體用法?Python RiveScript.get_uservars怎麽用?Python RiveScript.get_uservars使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rivescript.RiveScript
的用法示例。
在下文中一共展示了RiveScript.get_uservars方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: AdmiralBot
# 需要導入模塊: from rivescript import RiveScript [as 別名]
# 或者: from rivescript.RiveScript import get_uservars [as 別名]
#.........這裏部分代碼省略.........
bot.inst.do_one_loop()
###
# Event handlers.
###
def on_message(self, bot, username, remote_username, message):
"""Handle a message from an end user.
Parameters:
* bot: A reference to the interface that received the message.
* username: A unique name for the end user, typically with the
interface name prefixed, like `SLACK-kirsle`
* remote_username: The 'real' username, as the interface knows it.
* message: The text of the user's message.
"""
# Load this user's variables.
self.load_uservars(username)
# Get a reply for the user.
reply = self.rs.reply(username, message)
# Log the transaction.
self.log_chat(username, message, reply)
# Store the user's variables.
self.save_uservars(username)
# Send the response back.
# TODO: handle queueing and delayed responses.
bot.send_message(remote_username, reply)
def log_chat(self, username, message, reply):
"""Log the chat transaction to disk."""
# Each user gets their own log directory.
sanitized = re.sub(r'[^A-Za-z0-9_\[email protected]]+', '', username)
outdir = os.path.join("logs", "chats", sanitized)
outfile = os.path.join(outdir, "{}.log".format(sanitized))
if not os.path.isdir(outdir):
os.makedirs(outdir, mode=0o755)
self.log.info("[{}] {}".format(username, message))
self.log.info("[{}] {}".format(self.c.personality.name, reply))
with open(outfile, "a", encoding="utf-8") as fh:
today = datetime.datetime.today()
ts = today.strftime(self.c.logging.date_format)
fh.write("{today}\n"
"[{username}] {message}\n"
"[{bot}] {reply}\n\n".format(
today=ts,
username=username,
message=message,
bot=self.c.personality.name,
reply=reply,
))
def load_uservars(self, username):
"""Load a user's variables from disk."""
if not os.path.isdir("users"):
os.mkdir("users")
sanitized = re.sub(r'[^A-Za-z0-9_\[email protected]]+', '', username)
filename = "users/{}.json".format(sanitized)
if not os.path.isfile(filename):
return
with open(filename, "r", encoding="utf-8") as fh:
data = fh.read()
try:
params = json.loads(data)
print(params)
for key, value in params.items():
if type(value) is str:
self.rs.set_uservar(username, key, value)
print("SET VAR:", username, key, value)
except:
pass
def save_uservars(self, username):
"""Save a user's variables to disk."""
if not os.path.isdir("users"):
os.mkdir("users")
sanitized = re.sub(r'[^A-Za-z0-9_\[email protected]]+', '', username)
filename = "users/{}.json".format(sanitized)
with open(filename, "w", encoding="utf-8") as fh:
fh.write(json.dumps(self.rs.get_uservars(username)))
###
# Utility/Misc functions.
###
def panic(self, error):
"""Exit with a fatal error message."""
self.log.error(error)
raise RuntimeError(error)