For the complete documentation index, see llms.txt. This page is also available as Markdown.

Add the pokemonSync flow

Send unique daily Pokémon updates from your Pandium integration by adding a pokemonSync flow that fetches Pokémon, posts Slack messages, and advances IDs using tenant metadata.

You have clients that talk to both APIs, so put them together to start sending educational Pokémon messages via Slack.

A Pandium integration can be run in normal mode or init mode. The pokemonSync flow is for normal mode.

The goal in this flow is to send a Slack message about a new Pokémon each day. To do this we will need to:

  1. Add the file for this new flow:

    Your file structure should now look like this:

  ├── build 
  ├── node_modules 
  ├── src
  │  ├── processLogic
  │  │  └── pokemonSync.ts
  │  ├── index.ts
  │  └── lib.ts
  ├── package.json
  ├── PANDIUM.yaml
  └── tsconfig.json
  1. Within src/processLogic/pokemonSync.ts get a logger for the file and add the shell of an asynchronous pokemonSync function.

  1. Within src/index.ts import pokemonSync and invoke it within the main function when the run mode is normal.

The src/index.ts should now look something like this:

  1. Run pandium local build && pandium local run <tenant-id>, and you should see the following logged:

  1. Fetch a Pokémon by doing the following:

The pokemonSync.ts file should look something like this:

  1. Run pandium local build && pandium local run <tenant-id>.

You should see the same information logged as before - except that now a large Pokémon object has also been printed. This confirms the Pokémon client within pokemonSync is working, so you can remove the logger.info(pokemonOfTheDay).

  1. Add a function to transform the pokemonOfTheDay into a Slack message.

Here is one way transformations.ts could look:

  1. Within pokemonSync use pokemonToSlackMessage and slackClient.chat.postMessage to send a message to each of the Academy's Pokémon trainers.

The pokemonSync.ts file should look something like this:

  1. Run pandium local build && pandium local run <tenant-id>. You should get a Slack message about the Pokémon of the Day!

We're not quite done though. If you run it again you will get another Slack message about the same Pokémon. One of the Academy's requests is that we won't repeat Pokémon.

To accomplish this goal, we will use tenant metadata to remember which Pokémon was sent last.

At the end of every run Pandium reads the last line the integration printed to the standard out, validates it against the metadata_schema you added to the PANDIUM.yaml, and merges it into the tenant's metadata. On the next run that metadata is handed back to the integration, where pandium.metadata() reads it.

  1. Alter the integration so that it updates the tenant metadata during a normal sync.

  2. Run pandium local build && pandium local run <tenant-id>.

You should get another slack message about that same Pokémon. However the logs now end with the metadata update, which should look something like this:

That last line is the standard out. When this code runs on Pandium it will be saved as the tenant's metadata.

  1. Use that metadata in pokemonSync to ensure the Pokémon of the day is not repeated.

Your pokemonSync.ts should look like this:

And your src/index.ts should look like this:

  1. Run pandium local build && pandium local run <tenant-id>. You should get a Slack message about Pokémon #1, Bulbasaur!

The logs should look like this:

Notice the following about the logs:

  • Your tenant doesn't have any metadata yet, so lastPokemonId fell back to 0 and the Pokémon of the day was #1.

  • The ID printed to the standard out is greater than the last Pokémon's ID.

If you run this locally again you will get Bulbasaur again. That's expected: a local run reads the tenant's metadata from Pandium, but it doesn't write the standard out back to Pandium. Only runs on Pandium update the tenant's metadata, which is what you will see next.

Your pokemonSync.ts should loook like the one here. In fact, all your integration files should match all the ones in this repository at this commit.

The final step is to see what this all looks like when it is run on Pandium!

Last updated

Was this helpful?