-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
46 lines (38 loc) · 972 Bytes
/
function.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
package gcf_slack_sample
import (
"net/http"
"os"
"strings"
"github.com/nlopes/slack"
)
var (
threadTs slack.RTMsgOption
)
type SlackParams struct {
accessToken string
botUserID string
rtm *slack.RTM
}
func PostMessage(w http.ResponseWriter, r *http.Request) { // 引数は飾り。Cloud Functionのhttp trigger で必要
params := SlackParams{
accessToken: os.Getenv("ACCESS_TOKEN"),
botUserID: "",
}
api := slack.New(params.accessToken)
params.rtm = api.NewRTM()
go params.rtm.ManageConnection()
go func() {
for msg := range params.rtm.IncomingEvents {
switch ev := msg.Data.(type) {
case *slack.ConnectedEvent:
params.botUserID = ev.Info.User.ID
case *slack.MessageEvent:
if !strings.Contains(ev.Msg.Text, params.botUserID) {
continue
}
threadTs = slack.RTMsgOptionTS(ev.ThreadTimestamp)
params.rtm.SendMessage(params.rtm.NewOutgoingMessage("Sample TEST", ev.Channel, threadTs))
}
}
}()
}