This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
rules.go
121 lines (109 loc) · 2.9 KB
/
rules.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"cirello.io/gochatbot/bot"
"cirello.io/gochatbot/messages"
"cirello.io/gochatbot/rules/cron"
"cirello.io/gochatbot/rules/regex"
)
var cronRules = map[string]cron.Rule{
"message of the day": {
"0 10 * * *",
func() []messages.Message {
return []messages.Message{
{Message: "Good morning!"},
}
},
},
}
var regexRules = []regex.Rule{
{
`{{ .RobotName }} jump`, `tells the robot to jump`,
func(bot bot.Self, msg string, matches []string) []string {
var ret []string
ret = append(ret, "{{ .User }}, How high?")
lastJumpTS := bot.MemoryRead("jump", "lastJump")
ret = append(ret, fmt.Sprintf("{{ .User }} (last time I jumped: %s)", lastJumpTS))
bot.MemorySave("jump", "lastJump", []byte(time.Now().String()))
return ret
},
},
{
`{{ .RobotName }} qr code (.*)`, `turn a URL into a QR Code`,
func(bot bot.Self, msg string, matches []string) []string {
const qrUrl = "https://chart.googleapis.com/chart?chs=178x178&cht=qr&chl=%s"
return []string{
fmt.Sprintf(qrUrl, url.QueryEscape(matches[1])),
}
},
},
{
`{{ .RobotName }} http status (.*)`, `return the description of the given HTTP status`,
func(bot bot.Self, msg string, matches []string) []string {
httpCode, err := strconv.Atoi(matches[1])
if err != nil {
return []string{fmt.Sprintln("I could not convert", matches[1], "into HTTP code.")}
}
return []string{
fmt.Sprintln(
"{{ .User }},", matches[1], "is",
http.StatusText(httpCode),
),
}
},
},
{
`{{ .RobotName }} explainshell (.*)`, `links to explainshell.com on given command`,
func(bot bot.Self, msg string, matches []string) []string {
const explainShellUrl = "http://explainshell.com/explain?cmd=%s"
return []string{
strings.Replace(
fmt.Sprintf(explainShellUrl, url.QueryEscape(matches[1])),
"%20",
"+",
-1,
),
}
},
},
{
`{{ .RobotName }} godoc (.*)`, `search godoc.org and return the first result`,
func(bot bot.Self, msg string, matches []string) []string {
if len(matches) < 2 {
return []string{}
}
respBody, err := httpGet(fmt.Sprintf("http://api.godoc.org/search?q=%s", url.QueryEscape(matches[1])))
if err != nil {
return []string{err.Error()}
}
defer respBody.Close()
var data struct {
Results []struct {
Path string `json:"path"`
Synopsis string `json:"synopsis"`
} `json:"results"`
}
if err := json.NewDecoder(respBody).Decode(&data); err != nil {
return []string{err.Error()}
}
if len(data.Results) == 0 {
return []string{"package not found"}
}
return []string{fmt.Sprintf("%s %s/%s", data.Results[0].Synopsis, "http://godoc.org", data.Results[0].Path)}
},
},
}
func httpGet(u string) (io.ReadCloser, error) {
resp, err := http.Get(u)
if err != nil {
return nil, err
}
return resp.Body, nil
}