Mercurial > public > pacobot
annotate event/events.go @ 12:aaf85ae1f942
add very simple html template
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Thu, 20 Mar 2025 11:12:21 +0000 |
parents | e9df3bb010f4 |
children |
rev | line source |
---|---|
8 | 1 package event |
6 | 2 |
3 import ( | |
4 "bytes" | |
5 "encoding/json" | |
6 "log" | |
7 "net/http" | |
8 "net/url" | |
8 | 9 |
10 "github.com/denniscmcom/pacobot/auth" | |
6 | 11 ) |
12 | |
12
aaf85ae1f942
add very simple html template
Dennis C. M. <dennis@denniscm.com>
parents:
8
diff
changeset
|
13 func SubChannelChatMsg(authToken, session_id string) { |
8 | 14 config := auth.ReadConfig() |
6 | 15 |
8 | 16 data := map[string]any{ |
6 | 17 "type": "channel.chat.message", |
18 "version": "1", | |
19 "condition": map[string]string{ | |
20 "broadcaster_user_id": config.BroadcasterUserId, | |
21 "user_id": config.BroadcasterUserId, | |
22 }, | |
23 "transport": map[string]string{ | |
24 "method": "websocket", | |
25 "session_id": session_id, | |
26 }, | |
27 } | |
28 | |
29 jsonData, err := json.Marshal(data) | |
30 | |
31 if err != nil { | |
32 log.Fatal(err) | |
33 } | |
34 | |
8 | 35 log.Printf("event: subscribing to %s", data["type"]) |
12
aaf85ae1f942
add very simple html template
Dennis C. M. <dennis@denniscm.com>
parents:
8
diff
changeset
|
36 subEvent(authToken, jsonData) |
6 | 37 } |
38 | |
12
aaf85ae1f942
add very simple html template
Dennis C. M. <dennis@denniscm.com>
parents:
8
diff
changeset
|
39 func subEvent(authToken string, subData []byte) { |
6 | 40 baseUrl := &url.URL{ |
41 Scheme: "https", | |
42 Host: "api.twitch.tv", | |
43 Path: "helix/eventsub/subscriptions", | |
44 } | |
45 | |
46 req, err := http.NewRequest("POST", baseUrl.String(), bytes.NewBuffer(subData)) | |
47 | |
48 if err != nil { | |
49 log.Fatal(err) | |
50 } | |
51 | |
8 | 52 config := auth.ReadConfig() |
6 | 53 |
54 req.Header.Set("Authorization", "Bearer "+authToken) | |
55 req.Header.Set("Client-Id", config.ClientId) | |
56 req.Header.Set("Content-Type", "application/json") | |
57 | |
58 client := &http.Client{} | |
59 res, err := client.Do(req) | |
60 | |
61 if err != nil { | |
62 log.Fatal(err) | |
63 } | |
64 | |
8 | 65 log.Printf("status code: %d", res.StatusCode) |
6 | 66 |
8 | 67 if res.StatusCode != 202 { |
68 log.Fatal("event: failed to subscribe to event") | |
6 | 69 } |
70 | |
8 | 71 log.Println("event: subscribed") |
72 | |
73 defer res.Body.Close() | |
6 | 74 } |