forked from shomali11/slacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
96 lines (77 loc) · 2.38 KB
/
context.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
package slacker
import (
"context"
"github.com/slack-go/slack"
"github.com/slack-go/slack/socketmode"
)
// A BotContext interface is used to respond to an event
type BotContext interface {
Context() context.Context
Event() *MessageEvent
SocketMode() *socketmode.Client
Client() *slack.Client
}
// NewBotContext creates a new bot context
func NewBotContext(ctx context.Context, client *slack.Client, socketmode *socketmode.Client, evt *MessageEvent) BotContext {
return &botContext{ctx: ctx, event: evt, client: client, socketmode: socketmode}
}
type botContext struct {
ctx context.Context
event *MessageEvent
client *slack.Client
socketmode *socketmode.Client
}
// Context returns the context
func (r *botContext) Context() context.Context {
return r.ctx
}
// Event returns the slack message event
func (r *botContext) Event() *MessageEvent {
return r.event
}
// SocketMode returns the SocketMode client
func (r *botContext) SocketMode() *socketmode.Client {
return r.socketmode
}
// Client returns the slack client
func (r *botContext) Client() *slack.Client {
return r.client
}
// MessageEvent contains details common to message based events, including the
// raw event as returned from Slack along with the corresponding event type.
// The struct should be kept minimal and only include data that is commonly
// used to prevent freqeuent type assertions when evaluating the event.
type MessageEvent struct {
// Channel ID where the message was sent
Channel string
// User ID of the sender
User string
// Text is the unalterted text of the message, as returned by Slack
Text string
// TimeStamp is the message timestamp
TimeStamp string
// ThreadTimeStamp is the message thread timestamp.
ThreadTimeStamp string
// Data is the raw event data returned from slack. Using Type, you can assert
// this into a slackevents *Event struct.
Data interface{}
// Type is the type of the event, as returned by Slack. For instance,
// `app_mention` or `message`
Type string
// BotID holds the Slack User ID for our bot
BotID string
}
// IsThread indicates if a message event took place in a thread.
func (e *MessageEvent) IsThread() bool {
if e.ThreadTimeStamp == "" || e.ThreadTimeStamp == e.TimeStamp {
return false
}
return true
}
// IsBot indicates if the message was sent by a bot
func (e *MessageEvent) IsBot() bool {
if e.BotID == "" {
return false
}
return true
}