Skip to content

Instantly share code, notes, and snippets.

@robinglen
Last active September 2, 2022 12:31
Show Gist options
  • Save robinglen/473f073a5612b516bde1c073836b05c7 to your computer and use it in GitHub Desktop.
Save robinglen/473f073a5612b516bde1c073836b05c7 to your computer and use it in GitHub Desktop.
Fastify server for Bookworms slack integration
import Fastify from "fastify";
import fastifyForm from "fastify-formbody";
import { init } from "./bookworms.js";
import { sendBookMarks, sendBookmarkCommands} from "./slack.js";
const fastify = Fastify();
// fastifyForm is needed to get the body from a POST request
fastify.register(fastifyForm);
// creating a hook for Slack slash commands
fastify.post("/webhooks/slack/bookmarks", (request, reply) => {
const { body } = request;
// handle requesting requests when user doesn't specify a folder
if (body?.text === "" || body?.text.toLowerCase() === "all") {
reply.send(sendBookmarkCommands());
} else {
reply.send(sendBookMarks(body.text));
}
});
fastify.listen(3000, async (err) => {
// loading from file system the bookmarks
await init();
if (err) {
fastify.log.error(err);
process.exit(1);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment