File: respond.go

package info (click to toggle)
golang-github-slack-go-slack 0.11.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,720 kB
  • sloc: makefile: 54
file content (50 lines) | stat: -rw-r--r-- 960 bytes parent folder | download
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
package main

import (
	"fmt"
	"strings"

	"github.com/slack-go/slack"
)

func main() {
	api := slack.New(
		"YOUR-TOKEN-HERE",
	)

	rtm := api.NewRTM()
	go rtm.ManageConnection()

	for msg := range rtm.IncomingEvents {
		switch ev := msg.Data.(type) {
		case *slack.MessageEvent:
			msg := ev.Msg

			if msg.SubType != "" {
				break // We're only handling normal messages.
			}

			// Create a response object.
			resp := rtm.NewOutgoingMessage(fmt.Sprintf("echo %s", msg.Text), msg.Channel)

			// Respond in thread if not a direct message.
			if !strings.HasPrefix(msg.Channel, "D") {
				resp.ThreadTimestamp = msg.Timestamp
			}

			// Respond in same thread if message came from a thread.
			if msg.ThreadTimestamp != "" {
				resp.ThreadTimestamp = msg.ThreadTimestamp
			}

			rtm.SendMessage(resp)

		case *slack.ConnectedEvent:
			fmt.Println("Connected to Slack")

		case *slack.InvalidAuthEvent:
			fmt.Println("Invalid token")
			return
		}
	}
}