-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
146 lines (126 loc) · 2.94 KB
/
adapter.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
package main
import (
"github.com/labstack/echo/v4"
"github.com/lxbot/lxlib"
"log"
"net/http"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"time"
)
type M = map[string]interface{}
var ch *chan M
var token string
var secret string
var startTime time.Time
func Boot(c *chan M) {
ch = c
token = os.Getenv("LXBOT_KOKOROIO_ACCESSTOKEN")
secret = os.Getenv("LXBOT_KOKOROIO_CALLBACKSECRET")
startTime = time.Now()
go listen()
}
func Send(msg M) {
m, err := lxlib.NewLXMessage(msg)
if err != nil {
log.Println(err)
return
}
channelId := m.Room.ID
message := m.Message.Text
_ = send(channelId, message)
}
func Reply(msg M) {
m, err := lxlib.NewLXMessage(msg)
if err != nil {
log.Println(err)
return
}
channelId := m.Room.ID
user := m.User.ID
message := "@" + user + " " + m.Message.Text
_ = send(channelId, message)
}
func send(channelId string, msg string) error {
u := "https://kokoro.io/api/v1/bot/channels/" + channelId + "/messages"
v := url.Values{}
v.Set("message", msg)
req, err := http.NewRequest("POST", u, strings.NewReader(v.Encode()))
if err != nil {
return err
}
req.Header.Set("X-Access-Token", token)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return err
}
func listen() {
e := echo.New()
e.GET("/", get)
e.POST("/", post, authorize)
_ = e.Start("0.0.0.0:1323")
}
func get(c echo.Context) error {
ms := new(runtime.MemStats)
runtime.ReadMemStats(ms)
return c.HTML(http.StatusOK, `
<!doctype html>
<html>
<head>
<title>lxbot - adapter-kokoro.io</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.min.css">
</head>
<body>
<h1><a href="https://lxbot.io">lxbot</a> - <a href="https://github.com/lxbot/adapter-kokoro.io">adapter-kokoro.io</a></h1>
<ul>
<li>heap: ` + strconv.FormatUint(ms.HeapAlloc / 1024, 10) + ` KB</li>
<li>sys: ` + strconv.FormatUint(ms.Sys / 1024, 10) + ` KB</li>
<li>goroutine: ` + strconv.Itoa(runtime.NumGoroutine()) + `</li>
<li>uptime: ` + time.Since(startTime).String() + `</li>
</ul>
</body>
</html>
`)
}
func post(c echo.Context) error {
m := M{}
if err := c.Bind(&m); err != nil {
return c.NoContent(406)
}
resErr := c.NoContent(202)
*ch <- M{
"user": M{
"id": m["profile"].(M)["screen_name"],
"name": m["display_name"],
},
"room": M{
"id": m["channel"].(M)["id"],
"name": m["channel"].(M)["channel_name"],
"description": m["channel"].(M)["description"],
},
"message": M{
"id": strconv.Itoa(int(m["id"].(float64))),
"text": m["plaintext_content"],
"attachments": nil,
},
"raw": m,
}
return resErr
}
func authorize(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Request().Header.Get("Authorization") != secret {
log.Println("invalid webhook secret")
return c.NoContent(401)
}
return next(c)
}
}