Skip to content

Latest commit

 

History

History
219 lines (158 loc) · 8.84 KB

integration.md

File metadata and controls

219 lines (158 loc) · 8.84 KB

Ferdi Recipe Documentation / Overview

Recipes are responsible for providing the connection between the service itself (e.g. WhatsApp) and Ferdi, providing information like the number of current notifications or handling dark mode.

A Ferdi recipe is basically nothing else than a node module and is currently initialized on dom-ready. You access all of the electron modules as well.

ℹ️ New to Ferdi recipes and Electron? Then we recommend you read Jake Lee's "Super simple guide to adding a new Ferdi service recipe" which gives you an easy step-by-step guide on how to create and publish your recipe!

ℹ️ If you have any problems packaging your recipes, if you are inexperienced with the tools used or have any other problems, we are here to help! Just open a new issue at https://github.com/getferdi/recipes/issues/new and we can help you develop, test and publish your recipe

If you want to update an existing recipe, please refer to updating.md instead

Table of Contents

Preparing

You should have basic knowledge of JavaScript - don't worry, you'll really only need some basic commands as we've already prepared the complicated stuff for you.

We have also created a nice script that already does 50% of the work for you - yay 🎉. If you want to use this script, please make sure you have NodeJS installed on your system.

Create a recipe

  1. Fork this repository on GitHub. You can do this by clicking the "Fork" button in the top right corner
  2. Clone your forked repository. Normally, you can do this by running git clone https://github.com/<Your GitHub Username>/recipes.git in your terminal. You may also use a Git GUI or the GitHub Website for this.
  3. (Optional, if you want to use our creation script) Install its dependencies via the terminal:
pnpm install
  1. (Optional - contd) You can now run our automatic recipe wizard that creates and opens the new recipe for you:
# Make sure you are still in the repository's folder
pnpm run create "Service Name"

Replace Service Name with the name of your service, e.g. pnpm run create "Google Hangouts". This command will automatically create the development recipe in the correct folder, prepares it for your service and opens the new recipe in your file explorer or Finder. 5. Reload Ferdi (CMD/CTRL + SHIFT + R) in order for it to register the new recipe 6. You can now develop your recipe as described below. Please continue down below with "Publishing" after you are done creating your recipe.

  1. (Mandatory) Please run the following step before raising a PR:
pnpm run package

Fix any issues that are reported.

Recipe structure

Every recipe needs a specific file structure in order to work as a Ferdi recipe

  • icon.svg - Icon for the service in SVG form (must be square)
  • index.js - Backend script, this script is NOT included in the service webview but only in Ferdi itself
  • package.json - Information about the recipe
  • webview.js - Optional frontend script, this script is injected into the service itself but still has access to all NodeJS APIs
  • darkmode.css - CSS File that gets included when dark mode is activated

package.json

The package.json is structured like any other node module and allows to completely configure the service.

{
  "id": "tweetdeck",
  "name": "Tweetdeck",
  "version": "1.0.1",
  "license": "MIT",
  "repository": "https://github.com/meetfranz/recipe-tweetdeck",
  "config": {
    "serviceURL": "https://tweetdeck.twitter.com/"
  }
}

To get more information about all the provided configuration flags, check the config docs.

Please note that the fields id, name, version and config are mandatory.

index.js

This is your "backend" code. Right now the options are very limited and most of the services don't need a custom handling here. If your service is relatively straight forward and has a static URL eg. messenger.com, [TEAMID].slack.com or web.skype.com all you need to do to return the Ferdi Class:

module.exports = Ferdi => Ferdi;

If your service can be hosted on custom servers, you can validate the given URL to detect if it's your server and not e.g. google.com. To enable validation you can override the function validateServer

// RocketChat integration
module.exports = Ferdi =>
  class RocketChat extends Ferdi {
    async validateUrl(url) {
      try {
        const resp = await window.fetch(`${url}/api/info`, {
          method: 'GET',
          headers: {
            'Content-Type': 'application/json',
          },
        });
        const data = await resp.json();

        return Object.hasOwnProperty.call(data, 'version');
      } catch (err) {
        console.error(err);
      }

      return false;
    }
  };

validateServer needs to return a Promise, otherwise validation will fail.

By default, Ferdi's user agent looks like this:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.141 Safari/537.36 Ferdi/5.4.4-beta.3 (Electron 8.1.1)

Some services may not be compatible with Ferdi adding it's signature to the user agent.

If you encounter such a service, please remove this signature with the following snippet of code

overrideUserAgent() {
  return window.navigator.userAgent.replace(
    /(Ferdi|Electron)\/\S+ \([^)]+\)/g,
    ""
  );
}

If you want to change the user agent to a different one, you can simply return the String for the new user agent:

overrideUserAgent() {
  return "Mozilla/2.02Gold (Win95; I)";
}

webview.js

The optional webview.js is the actual script that will be loaded into the webview. Here you can do whatever you want to do in order perfectly integrate the service into Ferdi. For convenience, we have provided a very simple set of functions to set unread message badges (Ferdi.setBadge()), set active dialog title (Ferdi.setDialogTitle()) and inject CSS files (Ferdi.injectCSS()).

// telegram integration
module.exports = Ferdi => {
  const getMessages = () => {
    let direct = 0;
    let indirect = 0;
    const elements = document.querySelectorAll('.rp');
    for (const element of elements) {
      const subtitleBadge = element.querySelector('.dialog-subtitle-badge');
      if (subtitleBadge) {
        const parsedValue = Ferdi.safeParseInt(subtitleBadge.textContent);
        if (element.dataset.peerId > 0) {
          direct += parsedValue;
        } else {
          indirect += parsedValue;
        }
      }
    }

    Ferdi.setBadge(direct, indirect);
  };

  const getActiveDialogTitle = () => {
    const element = document.querySelector('.top .peer-title');

    Ferdi.setDialogTitle(element ? element.textContent : '');
  };

  const loopFunc = () => {
    getMessages();
    getActiveDialogTitle();
  };

  Ferdi.loop(loopFunc);

  Ferdi.injectCSS(_path.default.join(__dirname, 'service.css'));
};

To get more information about the provided functions, check the API docs.

Icons

In order to show every service icon crystal clear within the Ferdi UI, we require the icon in .svg (square, 1024x1024px) format.

Dark Mode

You can provide a custom Dark Mode Theme for your recipes just by putting the darkmode.css into your recipe folder. Once the darkmode.css exists, you can enable the Dark Mode in your service settings.

Recipe Dark Mode is only supported by Ferdi 5.0.0-beta.19+. Even then, certain services do not allow clients like Ferdi to override these styles (an example of this is google calendar).

Debugging

In order to debug your service integration, open Ferdi and use the shortcut Cmd/Ctrl+Alt+Shift+I to open the recipes developer tools.

Publishing

Ferdi uses its recipe repository at https://github.com/getferdi/recipes to publish recipes to all clients.

Publishing your recipes to Ferdi is super easy! When you used our recipe creation script, we have created a folder for your recipe inside Ferdi's internal folders (the one that got automatically opened after you ran our script).

Simply copy that whole folder into the repositories "recipes" folder. You'll now need to push your changes to Git and create a Pull Request from your fork repository to our repository using the GitHub website.