title: yowsup - Integrating sending and receiving
body: <p><strong>Background:</strong></p>
  
  <p>I would like to integrate yowsup to my home automation project. I have seen a simple sample on how to receive messages and after some minor changes it is working fine.</p>
  
  <p><strong>Issue:</strong></p>
  
  <p>My problem starts when it comes to integrate the send message feature. Those are the two files I am using:</p>
  
  <p><strong>run.py</strong></p>
  
  <pre><code>from layer import EchoLayer
  
  from yowsup.layers.auth import YowAuthenticationProtocolLayer
  from yowsup.layers.protocol_messages import YowMessagesProtocolLayer
  from yowsup.layers.protocol_receipts import YowReceiptProtocolLayer
  from yowsup.layers.protocol_acks import YowAckProtocolLayer
  from yowsup.layers.protocol_presence import YowPresenceProtocolLayer
  from yowsup.layers.network import YowNetworkLayer
  from yowsup.layers.coder import YowCoderLayer
  from yowsup.common import YowConstants
  from yowsup.layers import YowLayerEvent
  from yowsup.stacks import YowStack, YOWSUP_CORE_LAYERS
  from yowsup import env
  
  
  CREDENTIALS = ("phone", "pwd")
  
  if __name__ == "__main__":
  
      layers = (
          EchoLayer,
          (YowAuthenticationProtocolLayer, YowMessagesProtocolLayer, YowReceiptProtocolLayer, YowAckProtocolLayer, YowPresenceProtocolLayer)
      ) + YOWSUP_CORE_LAYERS
  
      stack = YowStack(layers)
      # Setting credentials
      stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, CREDENTIALS)
  
      # WhatsApp server address
      stack.setProp(YowNetworkLayer.PROP_ENDPOINT, YowConstants.ENDPOINTS[0])
      stack.setProp(YowCoderLayer.PROP_DOMAIN, YowConstants.DOMAIN)              
      stack.setProp(YowCoderLayer.PROP_RESOURCE, env.CURRENT_ENV.getResource())
  
      # Sending connecting signal
      stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT))
  
      # Program main loop
      stack.loop()
  </code></pre>
  
  <p><strong>layer.py</strong></p>
  
  <pre><code>from yowsup.layers.interface import YowInterfaceLayer, ProtocolEntityCallback
  from yowsup.layers.protocol_messages.protocolentities import TextMessageProtocolEntity
  from yowsup.layers.protocol_receipts.protocolentities import OutgoingReceiptProtocolEntity
  from yowsup.layers.protocol_acks.protocolentities import OutgoingAckProtocolEntity
  from yowsup.layers.protocol_presence.protocolentities import PresenceProtocolEntity
  import threading
  import logging
  
  logger = logging.getLogger(__name__)
  
  class EchoLayer(YowInterfaceLayer):
  
      @ProtocolEntityCallback("message")
      def onMessage(self, messageProtocolEntity):
          #send receipt otherwise we keep receiving the same message over and over
          print str(messageProtocolEntity.getFrom()) + ' - ' + str(messageProtocolEntity.getBody())
          receipt = OutgoingReceiptProtocolEntity(messageProtocolEntity.getId(), messageProtocolEntity.getFrom())
          self.toLower(receipt)
  
      @ProtocolEntityCallback("send_message")
      def sendMessage(self, destination, message, messageProtocolEntity):
          outgoingMessageProtocolEntity = TextMessageProtocolEntity(
          message,
          to = destination + "@s.whatsapp.net")
          self.toLower(outgoingMessageProtocolEntity)
  
  
      @ProtocolEntityCallback("receipt")
      def onReceipt(self, entity):
          ack = OutgoingAckProtocolEntity(entity.getId(), "receipt", "delivery")
          self.toLower(ack)
  
      # List of (jid, message) tuples
      PROP_MESSAGES = "org.openwhatsapp.yowsup.prop.sendclient.queue"
  
      def __init__(self):
          super(EchoLayer, self).__init__()
          self.ackQueue = []
          self.lock = threading.Condition()
  
      @ProtocolEntityCallback("success")
      def onSuccess(self, successProtocolEntity):
          self.lock.acquire()
          for target in self.getProp(self.__class__.PROP_MESSAGES, []):
              phone, message = target
              if '@' in phone:
                  messageEntity = TextMessageProtocolEntity(message, to = phone)
              elif '-' in phone:
                  messageEntity = TextMessageProtocolEntity(message, to = "%s@g.us" % phone)
              else:
                  messageEntity = TextMessageProtocolEntity(message, to = "%s@s.whatsapp.net" % phone)
              self.ackQueue.append(messageEntity.getId())
              self.toLower(messageEntity)
          self.lock.release()
  
      @ProtocolEntityCallback("ack")
      def onAck(self, entity):
          self.lock.acquire()
  
          if entity.getId() in self.ackQueue:
              self.ackQueue.pop(self.ackQueue.index(entity.getId()))
  
          if not len(self.ackQueue):
              logger.info("Message sent")
              #raise KeyboardInterrupt()
  
          self.lock.release()
  </code></pre>
  
  <p><strong>Questions:</strong></p>
  
  <ul>
  <li>Where am I supposed to call the send_message method, so I can send messages wherever I need it?</li>
  <li>Is there a regular event (triggering every second or something) which I could use to send my messages?</li>
  </ul>