-
Notifications
You must be signed in to change notification settings - Fork 0
/
siritori-lingrbot.go
154 lines (136 loc) · 2.81 KB
/
siritori-lingrbot.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
package main
import (
"bufio"
"flag"
"github.com/hoisie/web"
"github.com/mattn/go-lingr"
"math/rand"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
var re = regexp.MustCompile(`^(\S+)\s+#(しりとり|siritori)\s*$`)
var re2 = regexp.MustCompile(`^\s*#(しりとり|siritori)!\s*$`)
var cwd string
func defaultAddr() string {
port := os.Getenv("PORT")
if port == "" {
return ":80"
}
return ":" + port
}
var addr = flag.String("addr", defaultAddr(), "server address")
var upper = strings.NewReplacer(
"ぁ", "あ",
"ぃ", "い",
"ぅ", "う",
"ぇ", "え",
"ぉ", "お",
"ゃ", "や",
"ゅ", "ゆ",
"ょ", "よ",
)
func kana2hira(s string) string {
return strings.Map(func(r rune) rune {
if 0x30A1 <= r && r <= 0x30F6 {
return r - 0x0060
}
return r
}, s)
}
func hira2kana(s string) string {
return strings.Map(func(r rune) rune {
if 0x3041 <= r && r <= 0x3096 {
return r + 0x0060
}
return r
}, s)
}
func search(text string) string {
rs := []rune(text)
r := rs[len(rs)-1]
f, err := os.Open(filepath.Join(cwd, "dict.txt"))
if err != nil {
return ""
}
defer f.Close()
buf := bufio.NewReader(f)
words := []string{}
for {
b, _, err := buf.ReadLine()
if err != nil {
break
}
line := string(b)
if ([]rune(line))[0] == r {
words = append(words, line)
}
}
if len(words) == 0 {
return ""
}
return words[rand.Int()%len(words)]
}
func shiritori(text string) string {
text = strings.Replace(text, "ー", "", -1)
if rand.Int()%2 == 0 {
text = hira2kana(text)
} else {
text = kana2hira(text)
}
return search(text)
}
func handleText(text string) string {
rs := []rune(strings.TrimSpace(text))
if len(rs) == 0 {
return "しばくぞ"
}
if rs[len(rs)-1] == 'ん' || rs[len(rs)-1] == 'ン' {
return "勝った(笑)"
}
s := shiritori(text)
if s == "" {
return "わかりません"
}
rs = []rune(s)
if rs[len(rs)-1] == 'ん' || rs[len(rs)-1] == 'ン' {
s += "\nあっ..."
}
return s
}
func main() {
flag.Parse()
cwd = filepath.Dir(os.Args[0])
rand.Seed(time.Now().UnixNano())
siritoriMode := map[string]bool{}
web.Post("/", func(ctx *web.Context) string {
status, err := lingr.DecodeStatus(ctx.Request.Body)
if err != nil {
ctx.Abort(500, err.Error())
return err.Error()
}
for _, event := range status.Events {
if message := event.Message; message != nil {
text := message.Text
current, _ := siritoriMode[message.Room]
if re2.MatchString(text) {
current = !current
siritoriMode[message.Room] = current
if current {
return "しりとりモード オン"
}
return "しりとりモード オフ"
} else if current {
return handleText(text)
} else if re.MatchString(text) {
text = re.FindStringSubmatch(text)[1]
return handleText(text)
}
}
}
return ""
})
web.Run(*addr)
}