botbye-node-fastify
v0.1.1
Published
BotBye! integration for Fastify
Downloads
6
Readme
NodeJS Fastify
Install
npm i botbye-node-fastifyor
yarn add botbye-node-fastifyUsage
- Import
botbyefrombotbye-node-fastifymodule:
import * as botbye from "botbye-node-fastify";- Call
initwith your projectserver-key:
botbye.init({
// Use your server-key
serverKey: '00000000-0000-0000-0000-000000000000',
});- Use
validateRequeston handlers where you need bot protection:
fastify.get('/', async(request, reply) => {
// Get token from any place you store it in request (header for example)
const botbyeToken = getBotByeTokenFromRequest(request)
const options = {
token: botbyeToken,
request,
}
const botbyeResponse = await botbye.validateRequest(options);
const isAllowed = botbyeResponse.result?.isAllowed ?? true;
if (!isAllowed) {
reply.code(403).send()
return;
}
reply.send({ hello: "world" })
})Examples of BotBye API responses:
Bot detected:
{
"reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
"result": {
"isAllowed": false
},
"error": null
}Bot not detected:
{
"reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
"result": {
"isAllowed": true
},
"error": null
}Request banned by custom rule:
{
"reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
"result": {
"isAllowed": false
},
"error": {
"message": "Banned by rule: MY_CUSTOM_RULE"
}
}Invalid serverKey:
{
"reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
"result": null,
"error": {
"message": "[BotBye] Bad Request: Invalid Server Key"
}
}Full Code Example
import Fastify from "fastify";
import * as botbye from "botbye-node-fastify";
const fastify = Fastify({
logger: true,
})
botbye.init({
// Use your server-key
serverKey: '00000000-0000-0000-0000-000000000000',
});
fastify.get('/', async(request, reply) => {
// Get token from any place you store it in request (header for example)
const botbyeToken = getBotByeTokenFromRequest(request)
const options = {
token: botbyeToken,
request,
}
const botbyeResponse = await botbye.validateRequest(options);
const isAllowed = botbyeResponse.result?.isAllowed ?? true;
if (!isAllowed) {
reply.code(403).send()
return;
}
reply.send({ hello: "world" })
})
fastify.listen({ port: 3000 })