当前位置: 首页>>代码示例>>Python>>正文


Python RiveScript.set_topic方法代码示例

本文整理汇总了Python中rivescript.RiveScript.set_topic方法的典型用法代码示例。如果您正苦于以下问题:Python RiveScript.set_topic方法的具体用法?Python RiveScript.set_topic怎么用?Python RiveScript.set_topic使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rivescript.RiveScript的用法示例。


在下文中一共展示了RiveScript.set_topic方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: RiveBot

# 需要导入模块: from rivescript import RiveScript [as 别名]
# 或者: from rivescript.RiveScript import set_topic [as 别名]
class RiveBot(object):
	"""An example RiveScript bot using callbacks to manage user session data.

	This example assumes single user per RiveScript instance and as
	such it's suitable for use in stateless services (e.g. in web apps
	receiving webhooks). Just init, get reply and teardown. Of course,
	it will also work in RTM implementations with custom longer-lived 
	bot threads.

	Session state is persisted to a single JSON file. This wouldn't be 
	thread-safe in a concurrent environment (e.g. web server). In such
	case it would be recommended to subclass SessionStore and implement
	database persistence (preferably via one of great Python ORMs such
	as SQLAlchemy or peewee).
	"""
	def __init__(self, script_dir, user, ss, debug=False):
		self._user     = user
		self._redirect = None

		# init RiveScript
		self._rs = RiveScript(debug=debug)
		self._rs.load_directory(script_dir)
		self._rs.sort_replies()

		# restore session
		if isinstance(ss, SessionStore):
			self._ss = ss
		else:
			raise RuntimeError("RiveBot init error: provided session store object is not a SessionStore instance.")

		self._restore_session()

		# register event callbacks
		self._rs.on('topic',    self._topic_cb)
		self._rs.on('uservar',  self._uservar_cb)

	def _topic_cb(self, user, topic, redirect=None):
		"""Topic callback.

		This is a single-user-per-rive (stateless instance) scenario; in a multi-user
		scenario within a single thread, callback functions should delegate the 
		execution to proper user session objects.
		"""
		log.debug("Topic callback: user={}, topic={}, redirect={}".format(user, topic, redirect))
		self._session.set_topic(topic, redirect)

	def _uservar_cb(self, user, name, value):
		"""Topic callback. See comment for `_topic_cb()`"""
		log.debug("User variable callback: user={}, name={}, value={}".format(user, name, value))
		self._session.set_variable(name, value)		

	def _restore_session(self):
		self._session = self._ss.load(self._user)

		# set saved user variables
		for name, value in self._session.variables():
			self._rs.set_uservar(self._user, name, value)

		# set saved topic
		topic = self._session.get_topic()
		if topic:
			if '/' in topic:
				topic, self._redirect = topic.split('/')
			self._rs.set_topic(self._user, topic)

	def _save_session(self):
		self._ss.save(self._session)

	def run(self):
		log.info("RiveBot starting...")
		if self._redirect:
			# Repeat saved redirect so that the user gets the context
			# after session restart.
			redir_reply = self._rs.redirect(self._user, self._redirect)
			print("bot> Welcome back!")
			print("bot>", redir_reply)

		while True:
		    msg = raw_input("{}> ".format(self._user))
		    if msg == '/quit':
		        self.stop()
		        break
		    reply = self._rs.reply(self._user, msg)
		    print("bot>", reply)

	def stop(self):
		log.info("RiveBot shutting down...")
		print("\nbot> Bye.")
		self._save_session()
开发者ID:flogiston,项目名称:rivescript-python,代码行数:91,代码来源:example.py


注:本文中的rivescript.RiveScript.set_topic方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。