forked from BeepBoopHQ/go-slackbot
-
Notifications
You must be signed in to change notification settings - Fork 8
/
context.go
35 lines (29 loc) · 959 Bytes
/
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
package slackbot
import (
"github.com/essentialkaos/slack"
"golang.org/x/net/context"
)
const (
BOT_CONTEXT = "__BOT_CONTEXT__"
MESSAGE_CONTEXT = "__MESSAGE_CONTEXT__"
)
func BotFromContext(ctx context.Context) *Bot {
if result, ok := ctx.Value(BOT_CONTEXT).(*Bot); ok {
return result
}
return nil
}
// AddBotToContext sets the bot reference in context and returns the newly derived context
func AddBotToContext(ctx context.Context, bot *Bot) context.Context {
return context.WithValue(ctx, BOT_CONTEXT, bot)
}
func MessageFromContext(ctx context.Context) *slack.MessageEvent {
if result, ok := ctx.Value(MESSAGE_CONTEXT).(*slack.MessageEvent); ok {
return result
}
return nil
}
// AddMessageToContext sets the Slack message event reference in context and returns the newly derived context
func AddMessageToContext(ctx context.Context, msg *slack.MessageEvent) context.Context {
return context.WithValue(ctx, MESSAGE_CONTEXT, msg)
}