Update the pokemonSync flow | Pandium Docs
Pokémon of the Day Sync
You have access to the newly created slack_user and pokemon_type configs. Let's update pokemonSync to make use of them.
The Pokémon Trainer Academy needs the new pokemonSync to do the following:
- The Pokémon of the day must always be of the type selected in the tenant's connection settings.
- The Slack message should only be sent to the Slack user specified in the tenant's connection settings.
To do this we will need to:
- Fetch Pokémon Type selected in the config.
- Select the Pokémon of the Day from the Pokémon within that type.
- Fetch the Pokémon of the Day.
- Send a Slack message about that Pokémon to the Slack user selected in the config.
Configuration
- In the
.envupdate the run mode and enter selections for the two dynamic configs:
PAN_CTX_RUN_MODE= normal
PAN_CFG_POKEMON_TYPE = rock
PAN_CFG_SLACK_USER= <YOUR-SLACK-MEMBER-ID>
In the config object of your tenant's most recent normal sync logs you should have seen your Slack user ID. Replace the Slack user ID above with your own.
Fetching Selected Pokémon Type
- Fetch the selected Pokémon type by doing the following:
- In
src/index.tspass theconfigtopokemonSync. - In
src/processLogic/pokemonSync.tsaddconfigas an argument topokemonSyncand importConfigto define the type of this new argument. - Pass
config.pokemon_typeto thepokeClient.getTypeByNamemethod to fetch the selected type. - Log out the results of that fetch.
- In
The pokemonSync.ts file should look something like this:
import Pokedex from 'pokedex-promise-v2'
import { WebClient } from '@slack/web-api'
import { pokemonToSlackMessage } from '../transformations.js'
import { Context, Config } from '../lib.js'
export const pokemonSync = async (
pokeClient: Pokedex,
slackClient: WebClient,
context: Context,
config: Config
) => {
console.error(
'------------------------POKEMON SYNC------------------------'
)
const pokemonType = await pokeClient.getTypeByName(config.pokemon_type)
console.error(pokemonType)
let lastPokemonId = 0
if (context.last_successful_run_std_out) {
const lastStdOut = JSON.parse(context.last_successful_run_std_out)
lastPokemonId = Number(lastStdOut.last_pokemon_id) || 0
}
const nextPokemonId = lastPokemonId + 1
const pokemonOfTheDay = await pokeClient.getPokemonByName(nextPokemonId)
const slackMemberIds = ['<YOUR-SLACK-MEMBER-ID>']
for (const slackID of slackMemberIds) {
const slackMessage = pokemonToSlackMessage(pokemonOfTheDay, slackID)
await slackClient.chat.postMessage(slackMessage)
}
return { last_pokemon_id: nextPokemonId }
}
- Run
npm run build && npm run start.
You should get a Pokémon Slack message, but we're more interested in examining the logs; they should look something like this:
> pokemon-of-the-day@1.0.0 start
> node build/src/
This run is in mode: normal
------------------------CONFIG------------------------
Config { pokemon_type: 'rock', slack_user: '<YOUR-SLACK-MEMBER-ID>' }
------------------------CONTEXT------------------------
Context {
run_mode: 'normal',
last_successful_run_std_out: '{"last_pokemon_id":247}'
}
------------------------POKEMON SYNC------------------------
{
damage_relations: {... various damage relations properties of the rock Pokémon type}
... more properties of the rock Pokémon type
}
{"last_pokemon_id":248}
You can remove the console.error(pokemonType).
Selecting Pokémon of the Day
You can learn about the shape of a Pokémon type by examining the one you just logged out or reviewing the PokéAPI docs for the type endpoint.
Member Shape
Each member of the pokemon array has the following shape:
{
"slot": 1,
"pokemon": {
"name": "sandshrew",
"url": "https://pokeapi.co/api/v2/pokemon/27/"
}
}
- Change how
nextPokemonIdis defined; it should be the ID of the first member of the fetched type'spokemonarray whose ID is greater thanlastPokemonId.
Here is one way you could do this:
const pokemonType = await pokeClient.getTypeByName(config.pokemon_type) const pokemonOptions = pokemonType.pokemon
let nextPokemonId: string | undefined for (const pokemon of pokemonOptions) { const pokemonId = Number(pokemon.pokemon.url.split('/').slice(-2, -1)[0]) if (pokemonId <= lastPokemonId) continue nextPokemonId = String(pokemonId) break } if(!nextPokemonId) return {last_pokemon_id: lastPokemonId }
const pokemonOfTheDay = await pokeClient.getPokemonByName(nextPokemonId)
const slackMemberIds = ['
for (const slackID of slackMemberIds) { const slackMessage = pokemonToSlackMessage(pokemonOfTheDay, slackID) await slackClient.chat.postMessage(slackMessage) }
return { last_pokemon_id: nextPokemonId } }
1. Run `npm run build && npm run start`.
Unless you changed the standard out in your `.env`, this run's results may not look too different from the last run's (except the logs won't include the Pokémon type). This is because the list of Pokémon within a type often has many Pokémon of consecutive IDs. Regardless, you have shown that the new method of defining `nextPokemonId` does not interfere with the rest of the flow's functionality.
## Final Adjustments
1. Now adjust `pokemonSync` to use the `slack_user` config:
- Remove the array of hard coded `slackMemberIds`.
- Instead of sending a Slack message for every member in that array, just pass `config.slack_user` to `pokemonToSlackMessage`, and only call `slackClient.chat.postMessage` for that one `slackMessage.`
Your `pokemonSync.ts` could look like this now:
const pokemonType = await pokeClient.getTypeByName(config.pokemon_type)
const pokemonOptions = pokemonType.pokemon
const pokemonOfTheDay = await pokeClient.getPokemonByName(nextPokemonId)
const slackMessage = pokemonToSlackMessage(pokemonOfTheDay, config.slack_user)
await slackClient.chat.postMessage(slackMessage)
return { last_pokemon_id: nextPokemonId }
}
- Run
npm run build && npm run start.
Once again, this run's results may not look too different from the last run's. This shows that accessing the Slack user's ID from config, instead of a hardcoded list, does not interfere with the rest of the flow's functionality.
Conclusion
Your pokemonSync.ts should match this one. In fact, your integration files should match all the ones in this repository.
The final step is to see what this all looks like when it is run on Pandium!
Last updated 7 months ago