forked from gempir/go-twitch-irc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingpong.go
66 lines (54 loc) · 1.23 KB
/
pingpong.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
package main
import (
"log"
"net/http"
"strings"
"sync"
"time"
twitch "github.com/gempir/go-twitch-irc"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
clientUsername = "justinfan123123"
clientAuthenticationToken = "oauth:123123123"
)
var pingMetric = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "pings_received",
Help: "Pings received in the last 15 seconds",
})
func init() {
prometheus.MustRegister(pingMetric)
}
func main() {
client := twitch.NewClient(clientUsername, clientAuthenticationToken)
m := sync.Mutex{}
pingsReceived := 0
client.OnNewMessage(func(channel string, user twitch.User, message twitch.Message) {
if strings.Contains(strings.ToLower(message.Text), "ping") {
log.Println(user.Username, "PONG", message.Text)
m.Lock()
pingsReceived++
m.Unlock()
}
})
go func() {
for {
<-time.After(15 * time.Second)
m.Lock()
pingMetric.Set(float64(pingsReceived))
pingsReceived = 0
m.Unlock()
}
}()
client.Join("testaccount_420")
go func() {
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":9101", nil))
}()
err := client.Connect()
if err != nil {
panic(err)
}
}