| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | |
| 7 | "github.com/slack-go/slack" |
| 8 | ) |
| 9 | |
| 10 | func main() { |
| 11 | api := slack.New( |
| 12 | "YOUR-TOKEN-HERE", |
| 13 | ) |
| 14 | |
| 15 | rtm := api.NewRTM() |
| 16 | go rtm.ManageConnection() |
| 17 | |
| 18 | for msg := range rtm.IncomingEvents { |
| 19 | switch ev := msg.Data.(type) { |
| 20 | case *slack.MessageEvent: |
| 21 | msg := ev.Msg |
| 22 | |
| 23 | if msg.SubType != "" { |
| 24 | break // We're only handling normal messages. |
| 25 | } |
| 26 | |
| 27 | // Create a response object. |
| 28 | resp := rtm.NewOutgoingMessage(fmt.Sprintf("echo %s", msg.Text), msg.Channel) |
| 29 | |
| 30 | // Respond in thread if not a direct message. |
| 31 | if !strings.HasPrefix(msg.Channel, "D") { |
| 32 | resp.ThreadTimestamp = msg.Timestamp |
| 33 | } |
| 34 | |
| 35 | // Respond in same thread if message came from a thread. |
| 36 | if msg.ThreadTimestamp != "" { |
| 37 | resp.ThreadTimestamp = msg.ThreadTimestamp |
| 38 | } |
| 39 | |
| 40 | rtm.SendMessage(resp) |
| 41 | |
| 42 | case *slack.ConnectedEvent: |
| 43 | fmt.Println("Connected to Slack") |
| 44 | |
| 45 | case *slack.InvalidAuthEvent: |
| 46 | fmt.Println("Invalid token") |
| 47 | return |
| 48 | } |
| 49 | } |
| 50 | } |