-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
139 lines (115 loc) · 2.8 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
// Chaos Monkey will talk to you randomly 🙊
//
// Envvars:
//
// - `CM_RTM_TOKEN`: BearyChat RTM token
// - `CM_VICTIMS`: user ids who will be talk with,
// separates with comma: `=bw52O,=bw52P`
package main
import (
"fmt"
"log"
"math/rand"
"os"
"strings"
"time"
"github.com/bearyinnovative/bearychat-go"
)
type Config struct {
rtmToken string
victims []string
}
func (c Config) isVictimUID(uid string) bool {
for _, victimUID := range c.victims {
if victimUID == uid {
return true
}
}
return false
}
func (c Config) randomVictim() string {
return c.victims[rand.Intn(len(c.victims))]
}
func (c Config) insultMessage(user *bearychat.User) bearychat.RTMMessage {
messages := []string{
fmt.Sprintf("hi %s", user.Name),
fmt.Sprintf("hey %s, are you chill?", user.Name),
fmt.Sprintf("苟利国家生死以,岂因祸福避趋之,%s 识得唔识得啊?", user.Name),
}
return bearychat.RTMMessage{
"type": bearychat.RTMMessageTypeP2PMessage,
"vchannel_id": user.VChannelId,
"to_uid": user.Id,
"text": messages[rand.Intn(len(messages))],
"call_id": rand.Int(), // FIXME `call_id` sequence generator
"refer_key": nil,
}
}
func main() {
config := mustLoadConfigFromEnv()
rtmClient, err := bearychat.NewRTMClient(config.rtmToken)
checkErr(err)
user, wsHost, err := rtmClient.Start()
checkErr(err)
rtmLoop, err := bearychat.NewRTMLoop(wsHost)
checkErr(err)
checkErr(rtmLoop.Start())
defer rtmLoop.Stop()
go rtmLoop.Keepalive(time.NewTicker(2 * time.Second))
errC := rtmLoop.ErrC()
messageC, err := rtmLoop.ReadC()
checkErr(err)
tickTock := time.NewTicker(15 * time.Second)
defer tickTock.Stop()
for {
select {
case err := <-errC:
checkErr(err)
return
case message := <-messageC:
if !message.IsChatMessage() {
continue
}
if !message.IsP2P() {
continue
}
if message.IsFromUser(*user) {
continue
}
uid := message["uid"].(string)
if !config.isVictimUID(uid) {
continue
}
log.Printf("user %s said: %s", uid, message["text"])
checkErr(rtmLoop.Send(message.Refer("🙊")))
case <-tickTock.C:
user, err := rtmClient.User.Info(config.randomVictim())
checkErr(err)
log.Printf("insulting user %s", user.Name)
checkErr(rtmLoop.Send(config.insultMessage(user)))
}
}
}
func mustLoadConfigFromEnv() (config Config) {
config.rtmToken = os.Getenv("CM_RTM_TOKEN")
if config.rtmToken == "" {
log.Fatalf("`CM_RTM_TOKEN` is required!")
return
}
svictims := os.Getenv("CM_VICTIMS")
if svictims == "" {
log.Fatalf("`CM_VICTIMS` is required!")
return
}
config.victims = strings.Split(svictims, ",")
if len(config.victims) == 0 {
log.Fatalf("`CM_VICTIMS` is required!")
return
}
return
}
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}