Pulling avatars from slack

Basic bot integration

Published December 19, 2021 #slack, #nextjs, #bot

Create a slack app

First we need to create a slack application.

Create a new app, and add the following scopes

We are going to use a manifest, so we can tweak things as we go.

  _metadata:
    major_version: 1
  display_information:
    name: HappyFunCorp App
  settings:
    org_deploy_enabled: false
    socket_mode_enabled: false
    is_hosted: false
    token_rotation_enabled: false

Create the database and schema

  npm install prisma
  npx prisma init

Setup the schema in prisma/schema.prisma

  generator client {
    provider = "prisma-client-js"
  }

  datasource db {
    provider = "postgresql"
    url      = env("DATABASE_URL")
  }

  model SlackUser {
    id           String       @id
    name         String
    email        String?
    deleted      Boolean
    admin        Boolean?
    restricted   Boolean?
    bot          Boolean
    tz           String?
    title        String?
    skype        String?
    real_name    String?
    display_name String?
    status_text  String?
    status_emoji String?
    custom_image Boolean?
    original_image String?
    }

Start a database if you don't have one

  docker run -d -e POSTGRES_PASSWORD=awesome_password -p 5432:5432 postgres

And create the migrations

  npx prisma migrate dev --name init

Installing @slack/bolt

npm install @slack/bot dotenv

users-list.js

  const { App } = require('@slack/bolt');
  require('dotenv').config()
  const { PrismaClient } = require('@prisma/client')
  const prisma = new PrismaClient()

  const fs = require('fs')

  const app = new App({
    signingSecret: process.env.SLACK_SIGNING_SECRET,
    token: process.env.SLACK_BOT_TOKEN,
  });

  (async () => {
      console.log( "Getting user list from slack" )
      const slackdata = await app.client.users.list()


      if( slackdata["ok"] ) {
          const data = []
          slackdata["members"].map( (e) => {            
              data.push({
                  id: e["id"],
                  name: e["name"],
                  email: e["profile"]["email"],
                  deleted: e["deleted"],
                  admin: e["is_admin"],
                  restricted: e["is_restricted"],
                  bot: e["is_bot"],
                  tz: e["tz"],
                  title: e["profile"]["title"],
                  skype: e["profile"]["skype"],
                  real_name: e["profile"]["real_name"],
                  display_name: e["profile"]["display_name"],
                  status_text: e["profile"]["status_text"],
                  status_email: e["profile"]["status_email"],
                  status_emoji: e["profile"]["status_emoji"],
                  custom_image: e["profile"]["is_custom_image"],
                  original_image: e["profile"]["image_original"],
              })
          })

          console.log( `Found ${data.length} accounts, updating database` );

          while( data.length > 0 ) {
              const e = data.pop();

              // console.log( `Updating/created ${e['name']}` )

              const user = await prisma.slackUser.upsert({
                  where: { id: e['id'] },
                  update: e,
                  create: e
              })

          }

          console.log( "Done" );
      }
  })();

Create a nextjs app

Base

  npm install next react react-dom

Update package.json:

  {
      "scripts": {
          "dev": "next dev",
          "build": "next build",
          "start": "next start",
          "lint": "next lint"
      }
  }

And then create a sample pages/index.js

  const { PrismaClient } = require("@prisma/client");
  const prisma = new PrismaClient();
  const { MessageSender } = require("../components/messageSender");

  function Home({ users }) {
    return (
      <div>
        <div className="d-flex flex-wrap justify-content-around">
          {users.map((u) => (
            <div key={u.id} className="card mt-3" style={{ width: "18rem" }}>
              <div className="card-body">
                {u.custom_image && (
                  <img src={u.original_image} className="card-img-top" />
                )}
                <h5 className="card-title">{u.name}</h5>
                <p className="card-text">
                  {u.real_name}
                  <br />
                  {u.title}
                  <br />
                  <a href={`mailto:${u.email}`}>{u.email}</a>
                </p>
                <MessageSender user={u} />
              </div>
            </div>
          ))}
        </div>
      </div>
    );
  }

  export async function getServerSideProps(context) {
    const users = await prisma.slackUser.findMany({
      where: {
        deleted: false,
        restricted: false,
      },
    });

    return {
      props: { users }, // will be passed to the page component as props
    };
  }
  export default Home;

And also the MessageSender component in components/messageSender.jsx:

  export const MessageSender = ({ user }) => {
    return (
      <form action="/api/sendMessage" method="get">
        <input
          type="text"
          name="message"
          placeholder={`message to ${user.name}`}
        />
        <input type="hidden" name="id" value={user.id} />
        <button type="submit" className="btn-primary">
          Send
        </button>
      </form>
    );
  };

Sending a message

Lets create the api call to actually send the message:

  const { App } = require("@slack/bolt");

  export default async function handler(req, res) {
    const app = new App({
      signingSecret: process.env.SLACK_SIGNING_SECRET,
      token: process.env.SLACK_BOT_TOKEN,
    });

    const { id, message } = req.query;

    console.log(`Sending ${message} to ${id}`);

    if (id && message) {
      try {
        // Call the chat.postMessage method using the WebClient
        const result = await app.client.chat.postMessage({
          channel: id,
          text: message,
        });

        console.log(result);

        res.status(200).json({ id, message, ok: result.ok });
      } catch (error) {
        console.error(error);
        res.status(500).json({ id, message, result: error.toString() });
      }

      res.status(200).json({ id, message, action: "Sent" });
    } else {
      res.status(404).json({ action: "Not Sent", reason: "Missing params" });
    }
  }

Styling

  npm install bootstrap

pages/_app.js:

  // pages/_app.js
  import 'bootstrap/dist/css/bootstrap.css'

  export default function MyApp({ Component, pageProps }) {
      return <div className="container">
          <Component {...pageProps} />
          </div>
  }

References

Read next

See also

Receiving CloudEvents with NextJS

Need to receive cloudevents from your NextJS app? Me too! You need next-runtime. Install npm i next-runtime cloudevents In pages/indes.ts And then: import { handle, json } from 'next-runtime'; import { HTTP } from 'cloudevents'; export const getServerSideProps = handle({ async post({ req }) { console.log( req.body ) console.log( req.headers ) const receivedEvent = HTTP.toEvent({headers: req.headers, body: req.body }) console.log( receivedEvent ) return json({ message: 'Thanks for your submission!

Read more

NextJS with Prisma on Kubernetes

deploy as a knative service

Now that we have our cluster up and running, lets look at how to build and deploy a NextJS app on it, including the database. Create a NextJS app We'll scaffold out a TypeScript app. npx create-next-app@latest --typescript myapp cd myapp npm run dev Fireup a local data docker run -e POSTGRES_PASSWORD=awesome_password -p 5432:5432 postgres Install prisma We'll add the npm packages to our project.

Read more