Mercurial > public > pacobot
comparison event/events.go @ 8:e9df3bb010f4
fix issues
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Thu, 13 Mar 2025 17:41:42 +0000 |
parents | cmd/events.go@4deabe76bd7f |
children | aaf85ae1f942 |
comparison
equal
deleted
inserted
replaced
7:a8aab75f68c9 | 8:e9df3bb010f4 |
---|---|
1 package event | |
2 | |
3 import ( | |
4 "bytes" | |
5 "encoding/json" | |
6 "log" | |
7 "net/http" | |
8 "net/url" | |
9 | |
10 "github.com/denniscmcom/pacobot/auth" | |
11 ) | |
12 | |
13 func ChannelChatMsgSub(authToken, session_id string) { | |
14 config := auth.ReadConfig() | |
15 | |
16 data := map[string]any{ | |
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 | |
35 log.Printf("event: subscribing to %s", data["type"]) | |
36 eventSub(authToken, jsonData) | |
37 } | |
38 | |
39 func eventSub(authToken string, subData []byte) { | |
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 | |
52 config := auth.ReadConfig() | |
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 | |
65 log.Printf("status code: %d", res.StatusCode) | |
66 | |
67 if res.StatusCode != 202 { | |
68 log.Fatal("event: failed to subscribe to event") | |
69 } | |
70 | |
71 log.Println("event: subscribed") | |
72 | |
73 defer res.Body.Close() | |
74 } |