Templates / Webhooks

GitHub webhook to Discord: pushes and pull requests in a channel

Verify GitHub’s X-Hub-Signature-256 and post pushes, pull requests and releases to a Discord channel. Copy the code or get it hosted in a minute.

Keep a Discord channel up to date with what happens in a repository. GitHub sends an event for every push or pull request; this function verifies it came from GitHub and posts a short message.

Build it with Wisp

Type this into Wisp, or press Build this — it writes the code below, deploys it and gives you a URL:

A GitHub webhook receiver that verifies X-Hub-Signature-256 with GITHUB_WEBHOOK_SECRET and posts pushes, opened pull requests and published releases to Discord via DISCORD_WEBHOOK_URL.

The code

POST /github-webhook · API

import { createHmac, timingSafeEqual } from 'node:crypto';
import { config } from 'wisp';

export async function handler(req) {
  const raw = Buffer.from(req.rawBody ?? '', req.isBase64Encoded ? 'base64' : 'utf8');
  const expected = 'sha256=' + createHmac('sha256', config.require('GITHUB_WEBHOOK_SECRET')).update(raw).digest('hex');
  const given = req.headers['x-hub-signature-256'] ?? '';
  if (given.length !== expected.length || !timingSafeEqual(Buffer.from(given), Buffer.from(expected))) {
    return { status: 401, body: { error: 'Invalid signature' } };
  }

  const event = req.headers['x-github-event'];
  const p = JSON.parse(raw.toString('utf8'));
  let text = null;
  if (event === 'push' && p.commits?.length) {
    const branch = p.ref.replace('refs/heads/', '');
    text = `**${p.pusher.name}** pushed ${p.commits.length} commit(s) to \`${branch}\` in ${p.repository.full_name}\n` +
      p.commits.slice(0, 5).map((c) => `• ${c.message.split('\n')[0]}`).join('\n');
  } else if (event === 'pull_request' && p.action === 'opened') {
    text = `New PR #${p.number}: **${p.pull_request.title}** by ${p.sender.login}\n${p.pull_request.html_url}`;
  } else if (event === 'release' && p.action === 'published') {
    text = `Released **${p.release.tag_name}** in ${p.repository.full_name}\n${p.release.html_url}`;
  }
  if (text) {
    await fetch(config.require('DISCORD_WEBHOOK_URL'), {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify({ content: text.slice(0, 2000) }), // Discord's message limit
    });
  }
  return { status: 200, body: { ok: true } };
}

Set it up

Add these in the project’s Variables & secrets (secrets are encrypted and never shown in logs):

Try it

Repository → Settings → Webhooks → Add webhook: your function’s URL, content type application/json, the secret, and the events you want. GitHub sends a ping right away; Recent deliveries shows each request and lets you redeliver it.

How it works

Variations

Questions

Why does GitHub show a 401 for the ping? The secret in GitHub and the one in your function differ, or the content type is application/x-www-form-urlencoded — choose application/json.

Build it with Wisp — free trial

Related