Last active
September 2, 2022 12:31
-
-
Save robinglen/473f073a5612b516bde1c073836b05c7 to your computer and use it in GitHub Desktop.
Fastify server for Bookworms slack integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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