def handle_message(self, message, bot_service):
"""
Process incoming message generating a response to the sender.
:param message: Generic message received from provider
:param bot_service: Service Integration
:type bot_service: IntegrationBot :class:`IntegrationBot <permabots.models.bot.IntegrationBot>`
.. note:: Message content will be extracted by IntegrationBot
"""
urlpatterns = []
state_context = {}
chat_state = bot_service.get_chat_state(message)
for handler in caching.get_or_set_related(self, 'handlers', 'response', 'request', 'target_state'):
if handler.enabled:
source_states = caching.get_or_set_related(handler, 'source_states')
if chat_state:
state_context = chat_state.ctx
if not source_states or (chat_state and chat_state.state in source_states):
urlpatterns.append(handler.urlpattern())
resolver = RegexURLResolver(r'^', urlpatterns)
try:
resolver_match = resolver.resolve(bot_service.message_text(message))
except Resolver404:
logger.warning("Handler not found for %s" % message)
else:
callback, callback_args, callback_kwargs = resolver_match
logger.debug("Calling callback:%s for message %s with %s" %
(callback, message, callback_kwargs))
text, keyboard, target_state, context = callback(self, message=message, service=bot_service.identity,
state_context=state_context, **callback_kwargs)
if target_state:
self.update_chat_state(bot_service, message, chat_state, target_state, context)
keyboard = bot_service.build_keyboard(keyboard)
bot_service.send_message(bot_service.get_chat_id(message), text, keyboard, message)
评论列表
文章目录