Exemple #1
0
 def test_set(self):
     context = Context(entities={}, history=[], counter=0)
     context.myent = "foo"
     context.foo = EntityValue("foo",
                               counter=context.counter,
                               state_set=context.get_state_name(),
                               raw={"value": "foo"})
     assert context.myent.get_value(this_msg=True) == "foo"
     assert context.foo.get_value(this_msg=True) == "foo"
Exemple #2
0
 def test_context_get_set(self):
     dialog = MockDialog()
     context = Context(dialog=dialog, entities={}, history=[], counter=0)
     context.intent = "greeting"
     context.intent = "goodbye"
     intent = context.intent.get_value(this_msg=True)
     assert intent == "goodbye"
     cnt = context.intent.count()
     assert cnt == 2
     assert cnt == len(context.intent)
    def __init__(self, chat_manager, message, interceptors=None):
        from botshot.core.logging.logging_service import AsyncLoggingService
        flows = get_flows()
        if flows is None:
            raise ValueError("Flows have not been initialized.")
        if chat_manager is None:
            raise ValueError("ChatManager is None")
        if message is None:
            raise ValueError("Message is None")
        if message.conversation is None:
            raise ValueError("Conversation is None")

        self.message = message
        self.chat_manager = chat_manager
        self.send_exceptions = config.get("SEND_EXCEPTIONS",
                                          default=settings.DEBUG)
        self.flows = flows
        self.current_state_name = self.message.conversation.state or 'default.root'
        self.context = Context.from_dict(dialog=self,
                                         data=message.conversation.context_dict
                                         or {})
        loggers = [
            import_string(path)()
            for path in config.get('MESSAGE_LOGGERS', default=[])
        ]
        self.logging_service = AsyncLoggingService(loggers)
        self.dialog = Dialog(message=self.message,
                             context=self.context,
                             chat_manager=self.chat_manager,
                             logging_service=self.logging_service)
        self.interceptors = interceptors or []
 def get_ids(self):
     matching = []
     for conversation in ChatConversation.objects.all():
         context = Context.load(data=conversation.context_dict)
         if self._filter_context(context):
             matching.append(conversation.id)
     return matching
Exemple #5
0
 def test_context_message_age_filter(self):
     context = Context(dialog=MockDialog(), entities={}, history=[], counter=0)
     context.myentity = 1
     context.counter += 1
     context.myentity = 2
     context.counter += 1
     context.myentity = 3
     assert 'myentity' in context
     assert 'myentityy' not in context
     assert 'myent' not in context
     assert context.counter == 2
     assert context.myentity.get_value(this_msg=True) == 3
     assert context.myentity.get_value() == 3
     assert context.myentity.count() == 3
     assert context.myentity.exactly(messages=1).get_value() == 2
     assert context.myentity.newer_than(messages=1).count() == 1
     assert context.myentity.older_than(messages=1).count() == 1
     assert context.myentity.older_than(messages=1).get_value() == 1
     assert context.myentity.older_than(messages=0).count() == 2
     assert context.myentity.newer_than(messages=3).count() == 3
Exemple #6
0
    def __init__(self):

        self.message = ChatMessage()
        self.sender = ChatUser()
        self.conversation = ChatConversation()
        self.conversation.conversation_id = 1
        self.conversation.save()
        self.chat_manager = ChatManager()
        self.context = Context.load({})
        self.logging_service = Mock()

        self.sent = []
        self.schedules = []
        self.inactive_callbacks = []
Exemple #7
0
 def test_history(self):
     states = ["first", "second", "third", "fourth", "fifth"]
     context = Context(entities={}, history=[], counter=0, history_limit=10)
     for i, state in enumerate(states):
         context.add_state(state)
         assert context.get_state_name() == state
     for i, state in enumerate(reversed(states)):
         assert context.get_history_state(i)['name'] == state
     assert context.counter == 0