-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
217 lines (175 loc) · 5.29 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/mrshankly/go-twitch/twitch"
"log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
)
type Response struct {
Text string `json:"text"`
Attachments []*Attachment `json:"attachments"`
}
type Field struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short"`
}
type Attachment struct {
Fallback string `json:"fallback"`
Title string `json:"title"`
TitleLink string `json:"title_link"`
Text string `json:"text"`
ImageUrl string `json:"image_url"`
Fields []*Field `json:"fields"`
Color string `json:"color"`
}
type Attachments struct {
Attachs []*Attachment `json:"attachments"`
}
// on behalf of whg i need to apologize for doing this so quick and dirty
// enjoy ^_^
// bot keeps running if errors, except if listenandserve fails
func Run(port int) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
text := r.PostFormValue("text")
userId := r.PostFormValue("user_id")
userName := r.PostFormValue("user_name")
log.Printf("Handling request: %s from: %s (ID: %s)", text, userName, userId)
// make sure we dont trigger from ourselves, not sure if whgbot is needed here though, seems like slack uses USLACKBOT anyway
if userId == "USLACKBOT" || userName == "whgbot" {
return
}
// need to check to see if the url we are fed is a normal url
// or if this has been altered to slack format
slackRe := regexp.MustCompile(`([\S]+)\|(www\.[\S]+)$`)
isSlack := slackRe.MatchString(text)
// if it's the weird slack format, we need to get the proper url
if isSlack {
text = slackRe.FindStringSubmatch(text)[2]
}
// thank you based slack devs for making me do this
text = strings.Replace(text, ">", "", -1)
text = strings.Replace(text, "<", "", -1)
// matches twitch.tv and justin.tv urls
// (although justin.tv is gone now?)
streamRe := regexp.MustCompile(`(twitch\.tv\/([\w]+)|justin\.tv\/([\w]+))\/?$`)
videoRe := regexp.MustCompile(`(twitch\.tv\/([\w]+)\/([a-z]\/[0-9]+)\/?|justin\.tv\/([\w]+)\/([a-z]\/[0-9]+)\/?)`)
isStream := streamRe.MatchString(text)
isVideo := videoRe.MatchString(text)
client := twitch.NewClient(&http.Client{})
if isStream {
username := streamRe.FindStringSubmatch(text)[2]
data, err := client.Streams.Channel(username)
if err != nil {
log.Println(err)
return
}
if data.Stream.Id == 0 {
log.Println("STREAM OFFLINE")
response := &Response{Attachments: nil, Text: username + " is not streaming."}
payload, err := json.Marshal(response)
if err != nil {
return
}
log.Printf("Sending response: %s", payload)
w.Write(payload)
return
}
viewers := &Field{
Title: "Viewers",
Value: strconv.Itoa(data.Stream.Viewers),
Short: true,
}
// an attachment may contain several fields, but only one here
fields := []*Field{viewers}
attachment := &Attachment{
Fallback: username + " playing " + data.Stream.Game + " with " + strconv.Itoa(data.Stream.Viewers) + " viewers.",
Title: data.Stream.Channel.Status,
TitleLink: data.Stream.Channel.Url,
Text: "Playing " + data.Stream.Game,
ImageUrl: data.Stream.Preview,
Fields: fields,
Color: "#6441A5",
}
// a response may contain several attachments
attachs := []*Attachment{attachment}
response := &Response{Attachments: attachs, Text: data.Stream.Channel.Name + " is streaming right now!"}
payload, err := json.Marshal(response)
if err != nil {
return
}
log.Printf("Sending response: %s", payload)
w.Write(payload)
} else if isVideo {
videoId := videoRe.FindStringSubmatch(text)[3]
videoId = strings.Replace(videoId, "/", "", -1)
data, err := client.Videos.Id(videoId)
if err != nil {
return
}
if data == nil {
return
}
views := &Field{
Title: "Views",
Value: strconv.Itoa(data.Views),
Short: true,
}
duration := time.Duration(data.Length) * time.Second
length := &Field{
Title: "Duration",
Value: duration.String(),
Short: true,
}
// an attachment may contain several fields, but only one here
fields := []*Field{views, length}
attachment := &Attachment{
Fallback: data.Title + " [ " + strconv.Itoa(data.Views) + " views]",
Title: data.Title,
TitleLink: data.Url,
Text: data.Description,
ImageUrl: data.Preview,
Fields: fields,
Color: "#6441A5",
}
// a response may contain several attachments
attachs := []*Attachment{attachment}
response := &Response{Attachments: attachs, Text: "Video by " + data.Channel.Name}
payload, err := json.Marshal(response)
if err != nil {
return
}
log.Printf("Sending response: %s", payload)
w.Write(payload)
} else {
log.Println("IS NOTHING")
return
}
})
log.Printf("Starting http server on port %d", port)
err := http.ListenAndServe(":"+strconv.Itoa(port), nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func main() {
var httpPort int
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: whgbot-slack.exe -port=3000\n")
flag.PrintDefaults()
}
flag.IntVar(&httpPort, "port", 27015, "The HTTP port on which to listen")
flag.Parse()
if httpPort == 0 {
flag.Usage()
os.Exit(2)
}
Run(httpPort)
}