Giter Site home page Giter Site logo

aydrian / courier-netlify-twitch Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 1.0 18 KB

Netlify function to accept Twitch EventSub webhooks and send notifications using Courier

JavaScript 93.27% HTML 6.73%
courier twitch eventsub webhooks netlify netlify-functions notifications

courier-netlify-twitch's Introduction

Courier Netlify Twitch

Netlify Status

This is a simple Netlify Function project that will receive EventSub webhooks from Twitch and send notifications using Courier.

Installation Options

First, sign up for a Courier Developer Account, it’s free to sign up and includes 10,000 notifications per month. Next, create a Twitch application that will be used to access the Twitch API. Then, choose an option below.

This starter focuses on the stream.online EventSub subscription that will trigger when a user starts streaming on Twitch. You can extend this to handle any of the other subscription types.

Option one: One-click deploy

Deploy to Netlify

Clicking this button will create a new repo for you that looks like this one, and sets that repo up immediately for deployment on Netlify. You will be prompted for a repo name and to provide the values for the following environment variables to use with Courier and Twitch.

  • Courier Auth Token (COURIER_AUTH_TOKEN), required - You can find this value in your Courier API Keys Settings.
  • Twitch Signing Secret (TWITCH_SIGNING_SECRET), required - A string used for verifying requests from Twitch.
  • Twitch Client ID (TWITCH_CLIENT_ID), required - Client ID needed to access the Twitch API. Get it from your Twitch Dev Console.
  • Twitch Client Secret (TWITCH_CLIENT_SECRET), required - Client Secret needed to access the Twitch API. Get it from your Twitch Dev Console

Option two: Manual clone

You will need to install the Netlify CLI and connect it to your Netlify site to run locally.

  1. Clone this repo: git clone https://github.com/aydrian/courier-netlify-twitch.git
  2. Connect to Netlify using ntl init
  3. Add the above environment variables using ntl env:set [env var] [value]
  4. Run the app locally using ntl dev
  5. Make any changes and push to your repo to deploy.

Subscribe to EventSub Events

To begin receiving EventSub events, you'll need to first create a subscription. The easiest way to do this is by using the Twitch CLI. Once you have installed it, use the twitch configure command to supply it with your Twitch Client ID and Secret. Once you have finished, you can run twitch token to fetch an an access token.

Get User ID for Twitch Broadcaster

To create our subscription, we'll need to retrieve the user ID of broadcaster for whom we want to receive online alerts. We can do this using the Twitch CLI with the broadcaster's login ID:

twitch api get users -q login=trycourier

We'll use the id value from the Twitch user object returned in the data array.

Subscribe to stream.online event

Now we can create our subscription using the Twitch CLI. You'll need the name of your Netlify site, the user id of the broadcaster, and your Twitch signing secret. Run the following command with the needed substitutions:

twitch api post eventsub/subscriptions -b '{
    "type": "stream.online",
    "version": "1",
    "condition": {
        "broadcaster_user_id": "BROADCASTER_ID"
    },
    "transport": {
        "method": "webhook",
        "callback": "https://NETLIFY_SITE_NAME.netlify.app/webhooks/twitch",
        "secret": "TWITCH_SIGNING_SECRET"
    }
}'

Twitch will not send a POST the the Netlify function each time the broadcaster starts streaming.

Create Courier Notification

When the broadcaster goes online, Courier will send a notification mapped to the TWITCH_STREAM_ONLINE event to a list with the ID {broadcaster_id}.stream.online, e.g. trycourier.stream.online. You will need to create a notification and map it to the event and add recipients to the list.

Notification Data

The following data object with details about the stream will be sent along with the Courier send call:

{
  "stream": {
    "id": "40078987165",
    "type": "live",
    "startDate": "2021-01-05T19:54:35Z",
    "title": "Courier Live: Twitch EventSub and Courier",
    "thumbnailUrl": "https://static-cdn.jtvnw.net/previews-ttv/live_user_trycourier.jpg",
    "viewers": 0
  },
  "game": {
    "id": "417752",
    "name": "Talk Shows & Podcasts",
    "boxArtUrl": "https://static-cdn.jtvnw.net/ttv-boxart/Talk%20Shows%20&%20Podcasts.jpg"
  },
  "broadcaster": {
    "id": "493127514",
    "type": "affiliate",
    "userType": "",
    "name": "trycourier",
    "displayName": "trycourier",
    "description": "The smartest way to design \u0026 deliver notifications. Design once, deliver to any channel.",
    "profilePictureUrl": "https://static-cdn.jtvnw.net/jtv_user_pictures/454577ae-2bb1-4d2f-aeb9-64dfc7d00244-profile_image-300x300.png",
    "views": 60
  }
}

You can use any of these values in the creation of your notification using variables.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.