Skip to content

Commit

Permalink
removed logs, added example webhook payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
lucapasquale committed May 17, 2024
1 parent 336e928 commit 9b3a0b2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,64 @@ Grab your bot's token from [Discord Applications](https://discord.com/developers

Create an account on [Google Cloud Platform](https://console.cloud.google.com), and create a new API Key for your bot. You can limit the key to only have access to `YouTube Data API v3`

<details>
<summary>Example webhook payload</summary>

```xml
<?xml version='1.0' encoding='UTF-8'?>
<feed
xmlns:yt="http://www.youtube.com/xml/schemas/2015"
xmlns="http://www.w3.org/2005/Atom">
<link rel="hub" href="https://pubsubhubbub.appspot.com"/>
<link rel="self" href="https://www.youtube.com/xml/feeds/videos.xml?channel_id=UCHDxYLv8iovIbhrfl16CNyg"/>
<title>YouTube video feed</title>
<updated>2024-05-17T01:37:49.636216015+00:00</updated>
<entry>
<id>yt:video:TBXAlFqn93E</id>
<yt:videoId>TBXAlFqn93E</yt:videoId>
<yt:channelId>UCHDxYLv8iovIbhrfl16CNyg</yt:channelId>
<title>I wish this wasn’t an Ubisoft game</title>
<link rel="alternate" href="https://www.youtube.com/watch?v=TBXAlFqn93E"/>
<author>
<name>GameLinked</name>
<uri>https://www.youtube.com/channel/UCHDxYLv8iovIbhrfl16CNyg</uri>
</author>
<published>2024-05-17T01:37:38+00:00</published>
<updated>2024-05-17T01:37:49.636216015+00:00</updated>
</entry>
</feed>
```
</details>

### Twitch

To be able to search and receive webhooks from streams, you need to register an application on the [Twitch Applications](https://dev.twitch.tv/console/apps). After that, you'll be able to get the `client_id` and `client_secret` tokens.

<details>
<summary>Example webhook payload</summary>

```json
{
"challenge":"6gepqopldUA-K7p_CJDK3gmndD6mxyxjWSS7wCTFxZ8",
"subscription":{
"id":"3ce498e8-44d4-446f-886c-66bc256af5d9",
"status":"webhook_callback_verification_pending",
"type":"stream.online",
"version":"1",
"condition":{
"broadcaster_user_id":"35958947"
},
"transport":{
"method":"webhook",
"callback":"https://botchini.lucapasquale.dev/api/twitch/webhooks/callback"
},
"created_at":"2024-05-17T01:46:52.47828845Z",
"cost":1
}
}
```
</details>

## Running locally

After all necessary values are on `config/dev.secret.exs`, run the bot with: `mix phx.server`. It will reload automatically when you update a file.
Expand Down
3 changes: 1 addition & 2 deletions lib/botchini_web/controllers/twtich_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ defmodule BotchiniWeb.TwitchController do
message_id = get_header(conn, "twitch-eventsub-message-id")
message_timestamp = get_header(conn, "twitch-eventsub-message-timestamp")
[body] = Map.get(conn.assigns, :raw_body)
Logger.info(body)
payload = message_id <> message_timestamp <> body

webhook_secret = Application.fetch_env!(:botchini, :twitch_webhook_secret)
payload = message_id <> message_timestamp <> body

hmac =
:crypto.mac(:hmac, :sha256, webhook_secret, payload)
Expand Down
5 changes: 2 additions & 3 deletions lib/botchini_web/controllers/youtube_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ defmodule BotchiniWeb.YoutubeController do
defp request_is_valid?(_conn, :dev), do: true

defp request_is_valid?(conn, _) do
[raw_body] = Map.get(conn.assigns, :raw_body)
Logger.info(raw_body)
webhook_secret = Application.fetch_env!(:botchini, :youtube_webhook_secret)
[body] = Map.get(conn.assigns, :raw_body)

hmac =
:crypto.mac(:hmac, :sha, webhook_secret, raw_body)
:crypto.mac(:hmac, :sha, webhook_secret, body)
|> Base.encode16(case: :lower)

header_signature =
Expand Down

0 comments on commit 9b3a0b2

Please sign in to comment.