Example #1
0
 def __init__(self, caller, fail_silently=False, **kwargs):
     super(YowsupMessageServiceBackend, self).__init__(fail_silently, **kwargs)
     try:
         self.stack = YowsupStack((conf.LISTENCLOSELY_YOWSUP_NUMBER, conf.LISTENCLOSELY_YOWSUP_PASS), 
                                  conf.LISTENCLOSELY_YOWSUP_ENCRYPTION, self._get_top_layers())
     except yowsup_exceptions.ConfigurationError as e:
         logger.error("Configuration error") 
         raise listenclosely_exceptions.ConfigurationError("{0}".format(e))
     self.stack.caller = caller
Example #2
0
 def setUp(self):
     self.stack = YowsupStack(("341111111", "password"))
     stackClassesArr = (CeleryLayer, CoreLayerMock)
     self.stack._YowStack__stack = stackClassesArr[::-1]
     self.stack._YowStack__stackInstances = []
     self.stack._YowStack__props = {}
     self.stack._construct()
     self.stack.facade = self.stack.getLayerInterface(CeleryLayer)
     self.mock_layer = self.stack._YowStack__stackInstances[0]
     self.celery_layer = self.stack._YowStack__stackInstances[1]
     self.number = "341234567"
     self.content = "message test"
     self.message = (self.number, self.content)
Example #3
0
 def __init__(self, worker, login, config, unmoxie, **kwargs):
     """
     :param worker: celery worker
     :param login: optional login:password parameter
     :param config: optional path to configuration file
     :param unmoxie: boolean to disable encryption
     """
     credentials = self._get_credentials(login, config)
     if not credentials:
         raise ConfigurationError("Error: You must specify a configuration method")
     worker.app.stack = YowsupStack(credentials, not unmoxie, self._get_top_layers(worker))
     logger.info("Yowsup for %s intialized" % credentials[0])
Example #4
0
 def setUp(self):
     self.stack = YowsupStack(("341111111", "password"))
     stackClassesArr = (CeleryLayer, CoreLayerMock)   
     self.stack._YowStack__stack = stackClassesArr[::-1]
     self.stack._YowStack__stackInstances = []
     self.stack._YowStack__props = {}
     self.stack._construct()
     self.stack.facade = self.stack.getLayerInterface(CeleryLayer)
     self.mock_layer = self.stack._YowStack__stackInstances[0]
     self.celery_layer = self.stack._YowStack__stackInstances[1]
     self.number = "341234567"
     self.content = "message test"
     self.message = (self.number, self.content)
Example #5
0
class StackTest(unittest.TestCase):
    
    def setUp(self):
        self.stack = YowsupStack(("341111111", "password"))
        stackClassesArr = (CeleryLayer, CoreLayerMock)   
        self.stack._YowStack__stack = stackClassesArr[::-1]
        self.stack._YowStack__stackInstances = []
        self.stack._YowStack__props = {}
        self.stack._construct()
        self.stack.facade = self.stack.getLayerInterface(CeleryLayer)
        self.mock_layer = self.stack._YowStack__stackInstances[0]
        self.celery_layer = self.stack._YowStack__stackInstances[1]
        self.number = "341234567"
        self.content = "message test"
        self.message = (self.number, self.content)
        
    def _queue_thread(self, fn, *args, **kwargs):
        while not self.stack.listening:
            pass
        if fn:
            fn(*args, **kwargs)
        
    def _asynloop(self, asyn_action, timeout=0.1, auto_connect=False):
        self.input_thread = threading.Thread(target=self._queue_thread, args=(asyn_action,))
        self.input_thread.daemon = True
        self.input_thread.start()
        self.stack.asynloop(timeout=timeout, auto_connect=auto_connect)
        self.assertFalse(self.stack.listening, False)
        self.assertTrue(self.stack.detached_queue.empty())
        
    def test_asynloop_timeout(self):
        self._asynloop(None, 0.1)     
        
    def test_asynloop_autoconnect(self):
        def check_connected():
            self.assertTrue(self.stack.facade.connected())
        self._asynloop(check_connected, auto_connect=True) 
        
    def _test_asynloop(self, action):
        def queue_to_loop():
            self.assertTrue(self.stack.listening)
            self.stack.facade.connect()
            self.assertTrue(self.stack.facade.connected())
            if action:
                action()
            self.stack.facade.disconnect()
            self.assertFalse(self.stack.facade.connected())
        
        with mock.patch.object(YowsupStack, 'getLayerInterface', return_value=self.mock_layer) as mock_interface:
            mock_interface.return_value = self.mock_layer
            self._asynloop(queue_to_loop)
            
    def test_asynloop_disconnect(self):
        self._test_asynloop(None)
        
    def test_asynloop_receive_message(self):
        def receive_message():
            msg = self.mock_layer.receive_message()
            ack = self.mock_layer.lowerSink.pop()
            self.assertEqual(msg.getId(), ack.getId())
        self._test_asynloop(receive_message)
        
    def test_asynloop_receive_ack(self):
        def receive_ack():
            self.mock_layer.receive_ack()
            self.assertEqual([], self.mock_layer.lowerSink)         
        self._test_asynloop(receive_ack)
        
    def test_asynloop_receive_receipt(self):
        def receive_receipt():
            receipt = self.mock_layer.receive_receipt()
            ack = self.mock_layer.lowerSink.pop()
            self.assertEqual(receipt.getId(), ack.getId())
        self._test_asynloop(receive_receipt)
        
    def test_asynloop_send_message(self):
        def send_message():
            msg_to_send = self.stack.facade.send_message("341234567", "content")
            msg_sent = self.mock_layer.lowerSink.pop()
            self.assertEqual(msg_to_send.getId(), msg_sent.getId())
            self.assertEqual(msg_to_send.getBody(), msg_sent.getBody())
        self._test_asynloop(send_message)
Example #6
0
class YowsupMessageServiceBackend(BaseMessageServiceBackend):
    """
    Message Service Backend implementation to send instant messages with yowsup
    """
    
    def _get_top_layers(self):
        top_layers = []
        if conf.LISTENCLOSELY_YOWSUP_TOP_LAYERS:
            for top_layer_string in conf.LISTENCLOSELY_YOWSUP_TOP_LAYERS:
                top_layer = import_string(top_layer_string)
                top_layers.append(top_layer)
        top_layers.append(ListenCloselyLayer)
        return tuple(top_layers)
    
    def __init__(self, caller, fail_silently=False, **kwargs):
        super(YowsupMessageServiceBackend, self).__init__(fail_silently, **kwargs)
        try:
            self.stack = YowsupStack((conf.LISTENCLOSELY_YOWSUP_NUMBER, conf.LISTENCLOSELY_YOWSUP_PASS), 
                                     conf.LISTENCLOSELY_YOWSUP_ENCRYPTION, self._get_top_layers())
        except yowsup_exceptions.ConfigurationError as e:
            logger.error("Configuration error") 
            raise listenclosely_exceptions.ConfigurationError("{0}".format(e))
        self.stack.caller = caller
        
    def listen(self):
        """
        Loop to receive messages from whatsapp server. Set autoconnect option to True
        """
        if not self.stack.listening:
            try:
                self.stack.asynloop(auto_connect=True)
            except yowsup_exceptions.AuthenticationError as e:
                raise listenclosely_exceptions.AuthenticationError(str(e))
            except yowsup_exceptions.UnexpectedError as e:
                logger.error("Unexpected error %s" % str(e))
                raise listenclosely_exceptions.UnexpectedError(str(e))
        else:
            logger.warning("Already listening")
            
    def send_message(self, id_service, content):
        """
        Send message to a 
        """
        try:
            message = self.stack.facade.send_message(id_service, content)
            return message.getId()
        except yowsup_exceptions.ConnectionError as e:
            logger.error("Connection error: Message to %s not sent. Please listen before sending messages" % id_service)
            raise listenclosely_exceptions.ConnectionError(str(e))
        except yowsup_exceptions.UnexpectedError as e:
            logger.error("Unexpected error %s" % str(e))
            raise listenclosely_exceptions.UnexpectedError(str(e))
        
    def disconnect(self):
        """
        Disconnect from connection
        """
        try:
            self.stack.facade.disconnect()
        except yowsup_exceptions.ConnectionError as e:
            logger.warning("Trying to disconnect not connected service")
        except yowsup_exceptions.UnexpectedError as e:
            logger.error("Unexpected error %s" % str(e))
            raise listenclosely_exceptions.UnexpectedError(str(e))
Example #7
0
class StackTest(unittest.TestCase):
    def setUp(self):
        self.stack = YowsupStack(("341111111", "password"))
        stackClassesArr = (CeleryLayer, CoreLayerMock)
        self.stack._YowStack__stack = stackClassesArr[::-1]
        self.stack._YowStack__stackInstances = []
        self.stack._YowStack__props = {}
        self.stack._construct()
        self.stack.facade = self.stack.getLayerInterface(CeleryLayer)
        self.mock_layer = self.stack._YowStack__stackInstances[0]
        self.celery_layer = self.stack._YowStack__stackInstances[1]
        self.number = "341234567"
        self.content = "message test"
        self.message = (self.number, self.content)

    def _queue_thread(self, fn, *args, **kwargs):
        while not self.stack.listening:
            pass
        if fn:
            fn(*args, **kwargs)

    def _asynloop(self, asyn_action, timeout=0.1, auto_connect=False):
        self.input_thread = threading.Thread(target=self._queue_thread,
                                             args=(asyn_action, ))
        self.input_thread.daemon = True
        self.input_thread.start()
        self.stack.asynloop(timeout=timeout, auto_connect=auto_connect)
        self.assertFalse(self.stack.listening, False)
        self.assertTrue(self.stack.detached_queue.empty())

    def test_asynloop_timeout(self):
        self._asynloop(None, 0.1)

    def test_asynloop_autoconnect(self):
        def check_connected():
            self.assertTrue(self.stack.facade.connected())

        self._asynloop(check_connected, auto_connect=True)

    def _test_asynloop(self, action):
        def queue_to_loop():
            self.assertTrue(self.stack.listening)
            self.stack.facade.connect()
            self.assertTrue(self.stack.facade.connected())
            if action:
                action()
            self.stack.facade.disconnect()
            self.assertFalse(self.stack.facade.connected())

        with mock.patch.object(YowsupStack,
                               'getLayerInterface',
                               return_value=self.mock_layer) as mock_interface:
            mock_interface.return_value = self.mock_layer
            self._asynloop(queue_to_loop)

    def test_asynloop_disconnect(self):
        self._test_asynloop(None)

    def test_asynloop_receive_message(self):
        def receive_message():
            msg = self.mock_layer.receive_message()
            ack = self.mock_layer.lowerSink.pop()
            self.assertEqual(msg.getId(), ack.getId())

        self._test_asynloop(receive_message)

    def test_asynloop_receive_ack(self):
        def receive_ack():
            self.mock_layer.receive_ack()
            self.assertEqual([], self.mock_layer.lowerSink)

        self._test_asynloop(receive_ack)

    def test_asynloop_receive_receipt(self):
        def receive_receipt():
            receipt = self.mock_layer.receive_receipt()
            ack = self.mock_layer.lowerSink.pop()
            self.assertEqual(receipt.getId(), ack.getId())

        self._test_asynloop(receive_receipt)

    def test_asynloop_send_message(self):
        def send_message():
            msg_to_send = self.stack.facade.send_message(
                "341234567", "content")
            msg_sent = self.mock_layer.lowerSink.pop()
            self.assertEqual(msg_to_send.getId(), msg_sent.getId())
            self.assertEqual(msg_to_send.getBody(), msg_sent.getBody())

        self._test_asynloop(send_message)