Webhook

Top.gg Webhook

Kind: global class

new Webhook(authorization)

Create a new webhook client instance

Param Description
authorization Webhook authorization to verify requests

Example

const express = require('express')
const { Webhook } = require(`@top-gg/sdk`)

const app = express()
const wh = new Webhook('webhookauth123')

app.post('/dblwebhook', wh.listener(vote => {
  // vote is your vote object e.g
  console.log(vote.user) // => 321714991050784770
}))

app.listen(80)

// In this situation, your TopGG Webhook dashboard should look like
// URL = http://your.server.ip:80/dblwebhook
// Authorization: webhookauth123

webhook.listener(fn) ⇒

Listening function for handling webhook requests

Kind: instance method of WebhookReturns: An express request handler

Param Description
fn Vote handling function, this function can also throw an error to allow for the webhook to resend from Top.gg

Example

app.post('/webhook', wh.listener((vote) => {
  console.log(vote.user) // => 395526710101278721
}))

Example

// Throwing an error to resend the webhook
app.post('/webhook/', wh.listener((vote) => {
  // for example, if your bot is offline, you should probably not handle votes and try again
  if (bot.offline) throw new Error('Bot offline')
}))

~~webhook.middleware()~~

Deprecated

(Use the new .listener() function) Middleware function to pass to express, sets req.vote to the payload

Kind: instance method of WebhookExample

app.post('/dblwebhook', wh.middleware(), (req, res) => {
  // req.vote is your payload e.g
  console.log(req.vote.user) // => 395526710101278721
})