Skip to content

Commit

Permalink
chore: initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiMA10 committed May 15, 2020
1 parent 3803bb1 commit 9a80b7d
Show file tree
Hide file tree
Showing 7 changed files with 1,285 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
TWITCH_CHANNEL_ID=""
TWITCH_TOKEN=""
HUE_CLIENT_ID=""
HUE_CLIENT_SECRET=""
HUE_TOKEN=""
HUE_REFRESH_TOKEN=""
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/**
.env
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# Twitch Hue
# 💡💡 lights

> Let your streaming control your Philips Hue lights 🙈
## How to setup

Copy the .env.example file and complete the required values.

```
cp .env.example .env
```

| Env var | Description | Required |
| ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | -------- |
| TWITCH_CHANNEL_ID | Your [Twitch Channel Id](https://dev.twitch.tv/docs/v5/reference/channels/#get-channel) | **true** |
| TWITCH_TOKEN | Your [Twitch Access Token](https://dev.twitch.tv/docs/authentication#getting-tokens) with **channel:read:redemptions** scope | **true** |
| HUE_CLIENT_ID | The client id from a [Philips Hue Remote](https://developers.meethue.com/develop/hue-api/remote-api-quick-start-guide/) | **true** |
| HUE_CLIENT_SECRET | The client secret from a [Philips Hue Remote](https://developers.meethue.com/develop/hue-api/remote-api-quick-start-guide/) | **true** |
| HUE_TOKEN | The [hue access token](https://developers.meethue.com/develop/hue-api/remote-authentication/) generated with the same Philips Hue Remote | **true** |
| HUE_REFRESH_TOKEN | The [hue refresh token](https://developers.meethue.com/develop/hue-api/remote-authentication/) generated with the same Philips Hue Remote | **true** |
16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,19 @@
"main": "index.js",
"repository": "https://github.com/SantiMA10/twitch-hue.git",
"author": "Santi",
"license": "MIT"
"license": "MIT",
"scripts": {
"dev:twitch": "nodemon -r dotenv/config src/twitch.js",
"dev:hue": "nodemon -r dotenv/config src/hue.js"
},
"devDependencies": {
"nodemon": "^2.0.3"
},
"dependencies": {
"dotenv": "^8.2.0",
"node-hue-api": "^4.0.6",
"socket.io": "^2.3.0",
"websocket": "^1.0.31",
"ws": "^7.3.0"
}
}
21 changes: 21 additions & 0 deletions src/hue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const v3 = require("node-hue-api").v3;

const updateLightStatus = async ({ on } = { on: false }) => {
const remote = v3.api.createRemote(
process.env.HUE_CLIENT_ID,
process.env.HUE_CLIENT_SECRET
);

const api = await remote.connectWithTokens(
process.env.HUE_TOKEN,
process.env.HUE_REFRESH_TOKEN
);

await Promise.all([
api.lights.setLightState(5, { on }),
api.lights.setLightState(6, { on }),
api.lights.setLightState(9, { on }),
]);
};

module.exports = updateLightStatus;
45 changes: 45 additions & 0 deletions src/twitch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const WebSocket = require("ws");
const hue = require("./hue");

const ws = new WebSocket("wss://pubsub-edge.twitch.tv");

ws.on("open", function open() {
console.log("connected");
ws.send(
JSON.stringify({
type: "LISTEN",
nonce: "44h1k13746815ab1r2",
data: {
topics: [`channel-points-channel-v1.${process.env.TWITCH_CHANNEL_ID}`],
auth_token: process.env.TWITCH_TOKEN,
},
})
);
});

ws.on("close", function close() {
console.log("disconnected");
});

ws.on("message", function incoming(data) {
const json = JSON.parse(data);
if (!json.data) {
return;
}

const message = JSON.parse(json.data.message);

if (!message.data.redemption.reward) {
return;
}

const reward = message.data.redemption.reward;

if (reward.id === "3cc420c0-2198-4cd8-9a07-c68f489c50be") {
hue({ on: false });

setTimeout(() => {
hue({ on: true });
}, 10 * 1000);
}
});
Loading

0 comments on commit 9a80b7d

Please sign in to comment.