Init du dépot sur une base de scorbot

This commit is contained in:
hadware
2018-05-24 03:24:06 +02:00
commit 0b2ed0e002
13 changed files with 566 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
from .messages import *
from .connections import *
from .commons import MessageDispatcher
+36
View File
@@ -0,0 +1,36 @@
import logging
from typing import List
from tools.commons import UserList
class MessageProcessor:
"""Parent class for all processors. A processor is basically a bot response triggered by a condition.
The match method returns a boolean telling if the trigger condition is verified, and the process
method returns a string of the output message. All bot processors are to be grouped in a list then
given to a Dispatcher"""
def match(self, text : str, sender_id : str, users_list : UserList) -> bool:
"""Returns true if this is the kind of text a processor should respond to"""
pass
def process(self, text : str, sender_id : str, users_list : UserList) -> str:
"""Processes a message and returns an answer"""
pass
class MessageDispatcher:
"""Dispatches the current input message to the first botprocessor that matches the context given
to the dispatch method."""
def __init__(self, processor_list : List[MessageProcessor]):
self.processor_list = processor_list
def dispatch(self,text : str, sender_id : str, users_list : UserList) -> str:
"""Tells its first botprocessor to match the message to process this message and returns its answer"""
for processor in self.processor_list:
if processor.match(text, sender_id, users_list):
logging.info("Matched %s" % processor.__class__.__name__)
return processor.process(text, sender_id, users_list)
return None
+29
View File
@@ -0,0 +1,29 @@
import logging
from typing import List
from tools.commons import UserList
class ConnectProcessor:
def match(self, sender_id: str, users_list: UserList) -> bool:
"""Returns true if this is the kind of user connection a processor should respond to"""
pass
def process(self, sender_id: str, users_list: UserList) -> str:
"""Processes a message and returns an answer"""
pass
class ConnectionDispatcher:
def __init__(self, processor_list: List[ConnectProcessor]):
self.processor_list = processor_list
def dispatch(self, sender_id: str, users_list: UserList) -> str:
"""Tells its first botprocessor to match the message to process this message and returns its answer"""
for processor in self.processor_list:
if processor.match(sender_id, users_list):
logging.info("Matched %s" % processor.__class__.__name__)
return processor.process(sender_id, users_list)
return None
+33
View File
@@ -0,0 +1,33 @@
from tools.commons import Message
from .commons import *
class DispatcherBotProcessor(MessageProcessor):
"""A processor that matches a context, then forwards the message to a list of sub-processors.
This enables the botprocessor-matching mechanism to behave kinda like a decision tree"""
def __init__(self, processors_list : List[MessageProcessor]):
self.dispatcher = MessageDispatcher(processors_list)
def process(self, text : str, sender_id : str, users_list : UserList):
return self.dispatcher.dispatch(text, sender_id, users_list)
class CommandsDispatcherProcessor(DispatcherBotProcessor):
"""Reacts to commands of the form '/botname command' or 'botname, command' """
def __init__(self, processors_list: List[MessageProcessor], trigger_word: str = None, default_response :str = None):
super().__init__(processors_list)
self.trigger = trigger_word
self.default_response = default_response if default_response is not None else "Commande non reconnue, pd"
def match(self, text : str, sender_id : str, users_list : UserList):
trigger = self.trigger.upper() if self.trigger is not None else users_list.my_name.upper()
return text.upper().startswith(trigger + ",") \
or text.upper().startswith("/" + trigger)
def process(self, text : str, sender_id : str, users_list : UserList):
without_cmd = text[len(users_list.my_name)+1:]
response = super().process(without_cmd, sender_id, users_list)
return Message(self.default_response) if response is None else response