2 Writing your own backends
Fabio Manganiello edited this page 2021-09-17 00:29:26 +02:00

You can also write your own backends, where a backend is nothing but a thread that listens for messages on a certain channel and pokes the main app whenever it receives one.

  1. Create your backend directory under platypush/backend (e.g. voicemail)

  2. In the case above, platypush.backend.voicemail will be your package name.

  3. Create an __init__.py under platypush/backend/voicemail.

  4. If your module is voicemail, then its main class should be named VoicemailBackend.

  5. The configuration for your module will be read from a section named backend.voicemail from your config.yaml. Its values will be passed over the backend constructor arguments.

  6. Implement the run method. Since a backend is a thread that polls for new messages on a channel, this will be the thread main method. send_message should call self.on_message at the end to post a new message to the application.

  7. Implement the send_message method. This method will be called whenever the application needs to send a new message through send_request and send_response. You should never call send_message directly.

The __init__.py will look like this:

from .. import Backend

class VoicemailBackend(Backend)
    def __init__(self, phone)
        super().__init__()
        self.phone = phone
        self.voicemail = Voicemail(...)

    def send_message(self, msg):
        self.voicemail.save_msg(msg)

    def run(self):
        while True:
            msg = self.voicemail.poll()
            self.on_message(msg)
  1. Add a manifest.yaml file to the same folder. Example template:
manifest:
  type: backend
  package: platypush.backend.voicemail

  # Events launched by the extension
  events:
    platypush.message.event.voicemail.OnVoicemailMessage: When a message is received

  # Installation requirements
  install:
    # List of system-wide requirements on Debian/Ubuntu and derived
    apt:
      - sudo
    # List of system-wide requirements on Arch Linux and derived
    pacman:
      - sudo
    # List of pip dependencies
    pip:
      - requests
    # Extra commands to run after the dependencies are installed
    exec:
      - sudo systemctl enable my-service