Add the Slack Client

Now you will import the Slack client from the library @slack/web-api and confirm you can use it to fetch information from Slack.

A Slack client will let us post our daily messages, so let's get our code talking to Slack.

  1. Run npm install @slack/web-api.

These docs for this library show how to create an instance of a Slack client.

  1. In src/index.ts do the following:

These Slack web API docs describe the users.list endpoint. That will be helpful because you'll need some information about the users in the Pokémon Trainer Academy's Slack workspace in order to send them messages.

  1. Use your instance of the Slack client to fetch a list of users in the workspace that your Slack token has access to.

At this point your src/index.ts should look like this:

import * as dotenv from 'dotenv'
dotenv.config()
import { WebClient } from '@slack/web-api'
import Pokedex from 'pokedex-promise-v2'
import { Config, Secret, Context } from './lib.js'

const run = async () => {
    const context = new Context()
    const secrets = new Secret()
    const config = new Config()

    console.error(`This run is in mode: ${context['run_mode']}`)
    console.error('------------------------CONFIG------------------------')
    console.error(config)

    console.error('------------------------CONTEXT------------------------')
    console.error(context)

    const pokeClient = new Pokedex()
    const slackClient = new WebClient(secrets.slack_oauth_access_token)

    const { members } = await slackClient.users.list()
    console.error(members)
}

run().then(
    () => {},
    () => {
        process.exitCode = 1
    }
)
  1. Run npm run build && npm run start.

You should see something like this logged - except that the printed array should be filled with users from your Slack workspace.

> pokemon-of-the-day@1.0.0 start
> node build/src/

This run is in mode: normal
------------------------CONFIG------------------------
Config {}
------------------------CONTEXT------------------------
Context { run_mode: 'normal' }
[
  {
    id: 'USLACKBOT',
    team_id: 'TCF1SSXMJ',
    name: 'slackbot',
    deleted: false,
    color: '757575',
    real_name: 'Slackbot',
    tz: 'America/Los_Angeles',
    tz_label: 'Pacific Standard Time',
    tz_offset: -28800,
    profile: {
      title: '',
      phone: '',
      skype: '',
      real_name: 'Slackbot',
      real_name_normalized: 'Slackbot',
      display_name: 'Slackbot',
      display_name_normalized: 'Slackbot',
      fields: {},
      status_text: '',
      status_emoji: '',
      status_emoji_display_info: [],
      status_expiration: 0,
      avatar_hash: 'sv41d8cd98f0',
      always_active: true,
      first_name: 'slackbot',
      last_name: '',
      image_24: 'https://a.slack-edge.com/80588/img/slackbot_24.png',
      image_32: 'https://a.slack-edge.com/80588/img/slackbot_32.png',
      image_48: 'https://a.slack-edge.com/80588/img/slackbot_48.png',
      image_72: 'https://a.slack-edge.com/80588/img/slackbot_72.png',
      image_192: 'https://a.slack-edge.com/80588/marketing/img/avatars/slackbot/avatar-slackbot.png',
      image_512: 'https://a.slack-edge.com/80588/img/slackbot_512.png',
      status_text_canonical: '',
      team: 'TCF1SSXMJ'
    },
    is_admin: false,
    is_owner: false,
    is_primary_owner: false,
    is_restricted: false,
    is_ultra_restricted: false,
    is_bot: false,
    is_app_user: false,
    updated: 0,
    is_email_confirmed: false,
    who_can_share_contact_card: 'EVERYONE'
  },
  ...
  ]

Review documentation about the Slack Web API to develop a strategy for sending the Pokémon of the Day Slack messages. For example, you'll need to use the postMessage endpoint, and the docs say "If you want your app's bot user to start a 1:1 conversation with another user in a workspace, provide the user's user ID as the channel value"

That means you'll need the Slack member IDs of the Pokémon trainers who the Academy said should receive the daily messages. Consult the list of Slack members which was logged in the last run, and take note of those ID's (if you don't happen to see any Pokémon trainers you should at least be able to find yourself and note your own ID 😉)

Now that you know the Slack client works, so you can remove the logic for fetching and logging out the Slack users from src/index.ts.

Last updated