-
Notifications
You must be signed in to change notification settings - Fork 12
/
connectorMethods.go
118 lines (106 loc) · 3.03 KB
/
connectorMethods.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package test
import (
"strings"
"github.com/lnxjedi/gopherbot/robot"
)
// BotMessage is for receiving messages from the robot
type BotMessage struct {
User, Channel, Message string
Format robot.MessageFormat
}
func (tc *TestConnector) getUserInfo(u string) (*testUser, bool) {
var i int
var exists bool
if id, ok := tc.ExtractID(u); ok {
i, exists = userIDMap[id]
} else {
i, exists = userMap[u]
}
if exists {
return &tc.users[i], true
}
return nil, false
}
func (tc *TestConnector) getChannel(c string) string {
if ch, ok := tc.ExtractID(c); ok {
return strings.TrimPrefix(ch, "#")
}
return c
}
// MessageHeard indicates to the user a message was heard;
// for test/terminal it's a noop.
func (tc *TestConnector) MessageHeard(u, c string) {
return
}
// SetUserMap lets Gopherbot provide a mapping of usernames to user IDs
func (tc *TestConnector) SetUserMap(map[string]string) {
return
}
// GetProtocolUserAttribute returns a string attribute or nil if slack doesn't
// have that information
func (tc *TestConnector) GetProtocolUserAttribute(u, attr string) (value string, ret robot.RetVal) {
var user *testUser
var exists bool
if user, exists = tc.getUserInfo(u); !exists {
return "", robot.UserNotFound
}
switch attr {
case "email":
return user.Email, robot.Ok
case "internalid":
return user.InternalID, robot.Ok
case "realname", "fullname", "real name", "full name":
return user.FullName, robot.Ok
case "firstname", "first name":
return user.FirstName, robot.Ok
case "lastname", "last name":
return user.LastName, robot.Ok
case "phone":
return user.Phone, robot.Ok
// that's all the attributes we can currently get from slack
default:
return "", robot.AttributeNotFound
}
}
// SendProtocolChannelMessage sends a message to a channel
func (tc *TestConnector) SendProtocolChannelThreadMessage(ch, thr, mesg string, f robot.MessageFormat) (ret robot.RetVal) {
channel := tc.getChannel(ch)
msg := &BotMessage{
User: "",
Channel: channel,
Message: mesg,
Format: f,
}
return tc.sendMessage(msg)
}
// SendProtocolUserChannelMessage sends a message to a user in a channel
func (tc *TestConnector) SendProtocolUserChannelThreadMessage(uid, uname, ch, thr, mesg string, f robot.MessageFormat) (ret robot.RetVal) {
channel := tc.getChannel(ch)
msg := &BotMessage{
User: uname,
Channel: channel,
Message: mesg,
Format: f,
}
return tc.sendMessage(msg)
}
// SendProtocolUserMessage sends a direct message to a user
func (tc *TestConnector) SendProtocolUserMessage(u string, mesg string, f robot.MessageFormat) (ret robot.RetVal) {
var user *testUser
var exists bool
if user, exists = tc.getUserInfo(u); !exists {
return robot.UserNotFound
}
msg := &BotMessage{
User: user.Name,
Channel: "",
Message: mesg,
Format: f,
}
return tc.sendMessage(msg)
}
// JoinChannel joins a channel given it's human-readable name, e.g. "general"
// Only useful for connectors that require it, a noop otherwise
func (tc *TestConnector) JoinChannel(c string) (ret robot.RetVal) {
return robot.Ok
}