diff --git a/examples/files/files.go b/examples/files/files.go index 990d008..bb7b4e5 100644 --- a/examples/files/files.go +++ b/examples/files/files.go @@ -19,7 +19,7 @@ fmt.Printf("%s\n", err) return } - fmt.Printf("Name: %s, Url: %s\n", file.Name, file.URL) + fmt.Printf("Name: %s, URL: %s\n", file.Name, file.URL) err = api.DeleteFile(file.ID) if err != nil { diff --git a/examples/reactions/reactions.go b/examples/reactions/reactions.go index 4bda31d..753f0d2 100644 --- a/examples/reactions/reactions.go +++ b/examples/reactions/reactions.go @@ -39,11 +39,11 @@ // Post as the authenticated user. postAsUserName = authTest.User - postAsUserID = authTest.UserId + postAsUserID = authTest.UserID // Posting to DM with self causes a conversation with slackbot. postToUserName = authTest.User - postToUserID = authTest.UserId + postToUserID = authTest.UserID // Find the channel. _, _, chanID, err := api.OpenIMChannel(postToUserID) diff --git a/examples/websocket/websocket.go b/examples/websocket/websocket.go index 2686022..8ee16eb 100644 --- a/examples/websocket/websocket.go +++ b/examples/websocket/websocket.go @@ -19,7 +19,7 @@ } go wsAPI.HandleIncomingEvents(chReceiver) go wsAPI.Keepalive(20 * time.Second) - go func(wsAPI *slack.SlackWS, chSender chan slack.OutgoingMessage) { + go func(wsAPI *slack.WS, chSender chan slack.OutgoingMessage) { for { select { case msg := <-chSender: @@ -43,8 +43,8 @@ case slack.LatencyReport: a := msg.Data.(slack.LatencyReport) fmt.Printf("Current latency: %v\n", a.Value) - case *slack.SlackWSError: - error := msg.Data.(*slack.SlackWSError) + case *slack.WSError: + error := msg.Data.(*slack.WSError) fmt.Printf("Error: %d - %s\n", error.Code, error.Msg) default: fmt.Printf("Unexpected: %v\n", msg.Data) diff --git a/info.go b/info.go index 225100a..ed25cd6 100644 --- a/info.go +++ b/info.go @@ -5,7 +5,7 @@ "time" ) -// XXX: Need to implement +// UserPrefs needs to be implemented type UserPrefs struct { // "highlight_words":"", // "user_colors":"", @@ -145,7 +145,7 @@ // Info contains various details about Users, Channels, Bots and the authenticated user // It is returned by StartRTM type Info struct { - Url string `json:"url,omitempty"` + URL string `json:"url,omitempty"` User *UserDetails `json:"self,omitempty"` Team *Team `json:"team,omitempty"` Users []User `json:"users,omitempty"` @@ -157,7 +157,7 @@ type infoResponseFull struct { Info - SlackWSResponse + WSResponse } // GetBotByID returns a bot given a bot id @@ -190,10 +190,10 @@ return nil } -// GetGroupById returns a group given a group id -func (info Info) GetGroupById(groupId string) *Group { +// GetGroupByID returns a group given a group id +func (info Info) GetGroupByID(groupID string) *Group { for _, group := range info.Groups { - if group.Id == groupId { + if group.ID == groupID { return &group } } diff --git a/item.go b/item.go index 47a50a6..4100b34 100644 --- a/item.go +++ b/item.go @@ -52,23 +52,23 @@ // CommentId, or the combination of ChannelId and Timestamp must be // specified. type ItemRef struct { - ChannelId string `json:"channel"` + Channel string `json:"channel"` Timestamp string `json:"timestamp"` - FileId string `json:"file"` - CommentId string `json:"file_comment"` + File string `json:"file"` + Comment string `json:"file_comment"` } // NewRefToMessage initializes a reference to to a message. -func NewRefToMessage(channelID, timestamp string) ItemRef { - return ItemRef{ChannelId: channelID, Timestamp: timestamp} +func NewRefToMessage(channel, timestamp string) ItemRef { + return ItemRef{Channel: channel, Timestamp: timestamp} } // NewRefToFile initializes a reference to a file. -func NewRefToFile(fileID string) ItemRef { - return ItemRef{FileId: fileID} +func NewRefToFile(file string) ItemRef { + return ItemRef{File: file} } // NewRefToComment initializes a reference to a file comment. -func NewRefToComment(commentID string) ItemRef { - return ItemRef{CommentId: commentID} +func NewRefToComment(comment string) ItemRef { + return ItemRef{Comment: comment} } diff --git a/item_test.go b/item_test.go index 0f1343c..e1c969c 100644 --- a/item_test.go +++ b/item_test.go @@ -78,48 +78,48 @@ func TestNewRefToMessage(t *testing.T) { ref := NewRefToMessage("chan", "ts") - if got, want := ref.ChannelId, "chan"; got != want { - t.Errorf("ChannelId got %s, want %s", got, want) + if got, want := ref.Channel, "chan"; got != want { + t.Errorf("Channel got %s, want %s", got, want) } if got, want := ref.Timestamp, "ts"; got != want { t.Errorf("Timestamp got %s, want %s", got, want) } - if got, want := ref.FileId, ""; got != want { - t.Errorf("FileId got %s, want %s", got, want) + if got, want := ref.File, ""; got != want { + t.Errorf("File got %s, want %s", got, want) } - if got, want := ref.CommentId, ""; got != want { - t.Errorf("CommentId got %s, want %s", got, want) + if got, want := ref.Comment, ""; got != want { + t.Errorf("Comment got %s, want %s", got, want) } } func TestNewRefToFile(t *testing.T) { ref := NewRefToFile("file") - if got, want := ref.ChannelId, ""; got != want { - t.Errorf("ChannelId got %s, want %s", got, want) + if got, want := ref.Channel, ""; got != want { + t.Errorf("Channel got %s, want %s", got, want) } if got, want := ref.Timestamp, ""; got != want { t.Errorf("Timestamp got %s, want %s", got, want) } - if got, want := ref.FileId, "file"; got != want { - t.Errorf("FileId got %s, want %s", got, want) + if got, want := ref.File, "file"; got != want { + t.Errorf("File got %s, want %s", got, want) } - if got, want := ref.CommentId, ""; got != want { - t.Errorf("CommentId got %s, want %s", got, want) + if got, want := ref.Comment, ""; got != want { + t.Errorf("Comment got %s, want %s", got, want) } } func TestNewRefToComment(t *testing.T) { ref := NewRefToComment("file_comment") - if got, want := ref.ChannelId, ""; got != want { - t.Errorf("ChannelId got %s, want %s", got, want) + if got, want := ref.Channel, ""; got != want { + t.Errorf("Channel got %s, want %s", got, want) } if got, want := ref.Timestamp, ""; got != want { t.Errorf("Timestamp got %s, want %s", got, want) } - if got, want := ref.FileId, ""; got != want { - t.Errorf("FileId got %s, want %s", got, want) + if got, want := ref.File, ""; got != want { + t.Errorf("File got %s, want %s", got, want) } - if got, want := ref.CommentId, "file_comment"; got != want { - t.Errorf("CommentId got %s, want %s", got, want) + if got, want := ref.Comment, "file_comment"; got != want { + t.Errorf("Comment got %s, want %s", got, want) } } diff --git a/messages.go b/messages.go index d86ee37..8dd6f38 100644 --- a/messages.go +++ b/messages.go @@ -102,12 +102,12 @@ } // NewOutgoingMessage prepares an OutgoingMessage that the user can use to send a message -func (api *SlackWS) NewOutgoingMessage(text string, channel string) *OutgoingMessage { +func (api *WS) NewOutgoingMessage(text string, channel string) *OutgoingMessage { api.mutex.Lock() defer api.mutex.Unlock() - api.messageId++ + api.messageID++ return &OutgoingMessage{ - ID: api.messageId, + ID: api.messageID, Type: "message", Channel: channel, Text: text, diff --git a/reactions.go b/reactions.go index c62f15b..6878e50 100644 --- a/reactions.go +++ b/reactions.go @@ -59,28 +59,28 @@ } const ( - DEFAULT_REACTIONS_USERID = "" - DEFAULT_REACTIONS_COUNT = 100 - DEFAULT_REACTIONS_PAGE = 1 - DEFAULT_REACTIONS_FULL = false + DEFAULT_REACTIONS_USER = "" + DEFAULT_REACTIONS_COUNT = 100 + DEFAULT_REACTIONS_PAGE = 1 + DEFAULT_REACTIONS_FULL = false ) // ListReactionsParameters is the inputs to find all reactions by a user. type ListReactionsParameters struct { - UserId string - Count int - Page int - Full bool + User string + Count int + Page int + Full bool } // NewListReactionsParameters initializes the inputs to find all reactions // performed by a user. func NewListReactionsParameters() ListReactionsParameters { return ListReactionsParameters{ - UserId: DEFAULT_REACTIONS_USERID, - Count: DEFAULT_REACTIONS_COUNT, - Page: DEFAULT_REACTIONS_PAGE, - Full: DEFAULT_REACTIONS_FULL, + User: DEFAULT_REACTIONS_USER, + Count: DEFAULT_REACTIONS_COUNT, + Page: DEFAULT_REACTIONS_PAGE, + Full: DEFAULT_REACTIONS_FULL, } } @@ -136,17 +136,17 @@ if name != "" { values.Set("name", name) } - if item.ChannelId != "" { - values.Set("channel", string(item.ChannelId)) + if item.Channel != "" { + values.Set("channel", string(item.Channel)) } if item.Timestamp != "" { values.Set("timestamp", string(item.Timestamp)) } - if item.FileId != "" { - values.Set("file", string(item.FileId)) - } - if item.CommentId != "" { - values.Set("file_comment", string(item.CommentId)) + if item.File != "" { + values.Set("file", string(item.File)) + } + if item.Comment != "" { + values.Set("file_comment", string(item.Comment)) } response := &SlackResponse{} if err := parseResponse("reactions.add", values, response, api.debug); err != nil { @@ -166,17 +166,17 @@ if name != "" { values.Set("name", name) } - if item.ChannelId != "" { - values.Set("channel", string(item.ChannelId)) + if item.Channel != "" { + values.Set("channel", string(item.Channel)) } if item.Timestamp != "" { values.Set("timestamp", string(item.Timestamp)) } - if item.FileId != "" { - values.Set("file", string(item.FileId)) - } - if item.CommentId != "" { - values.Set("file_comment", string(item.CommentId)) + if item.File != "" { + values.Set("file", string(item.File)) + } + if item.Comment != "" { + values.Set("file_comment", string(item.Comment)) } response := &SlackResponse{} if err := parseResponse("reactions.remove", values, response, api.debug); err != nil { @@ -193,17 +193,17 @@ values := url.Values{ "token": {api.config.token}, } - if item.ChannelId != "" { - values.Set("channel", string(item.ChannelId)) + if item.Channel != "" { + values.Set("channel", string(item.Channel)) } if item.Timestamp != "" { values.Set("timestamp", string(item.Timestamp)) } - if item.FileId != "" { - values.Set("file", string(item.FileId)) - } - if item.CommentId != "" { - values.Set("file_comment", string(item.CommentId)) + if item.File != "" { + values.Set("file", string(item.File)) + } + if item.Comment != "" { + values.Set("file_comment", string(item.Comment)) } if params.Full != DEFAULT_REACTIONS_FULL { values.Set("full", strconv.FormatBool(params.Full)) @@ -223,8 +223,8 @@ values := url.Values{ "token": {api.config.token}, } - if params.UserId != DEFAULT_REACTIONS_USERID { - values.Add("user", params.UserId) + if params.User != DEFAULT_REACTIONS_USER { + values.Add("user", params.User) } if params.Count != DEFAULT_REACTIONS_COUNT { values.Add("count", strconv.Itoa(params.Count)) diff --git a/reactions_test.go b/reactions_test.go index 103885f..817ed06 100644 --- a/reactions_test.go +++ b/reactions_test.go @@ -337,13 +337,13 @@ }, } wantParams := map[string]string{ - "user": "UserID", + "user": "User", "count": "200", "page": "2", "full": "true", } params := NewListReactionsParameters() - params.UserId = "UserID" + params.User = "User" params.Count = 200 params.Page = 2 params.Full = true diff --git a/slack.go b/slack.go index a1f4583..8f462e1 100644 --- a/slack.go +++ b/slack.go @@ -17,7 +17,7 @@ } type AuthTestResponse struct { - Url string `json:"url"` + URL string `json:"url"` Team string `json:"team"` User string `json:"user"` TeamID string `json:"team_id"` diff --git a/stars_test.go b/stars_test.go index 353701c..39993ec 100644 --- a/stars_test.go +++ b/stars_test.go @@ -118,18 +118,18 @@ for i, test := range tests { sh = newStarsHandler() sh.response = test.json - response_items, response_paging, err := api.GetStarred(test.params) + responseItems, responsePaging, err := api.GetStarred(test.params) if err != nil { t.Fatalf("%d Unexpected error: %s", i, err) } if !reflect.DeepEqual(sh.gotParams, test.wantParams) { t.Errorf("%d got %v; want %v", i, sh.gotParams, test.wantParams) } - if !reflect.DeepEqual(response_items, test.starredItems) { - t.Errorf("%d got %v; want %v", i, response_items, test.starredItems) + if !reflect.DeepEqual(responseItems, test.starredItems) { + t.Errorf("%d got %v; want %v", i, responseItems, test.starredItems) } - if !reflect.DeepEqual(response_paging, test.paging) { - t.Errorf("%d got %v; want %v", i, response_paging, test.paging) + if !reflect.DeepEqual(responsePaging, test.paging) { + t.Errorf("%d got %v; want %v", i, responsePaging, test.paging) } } } diff --git a/websocket.go b/websocket.go index 4d5fe7b..997d22d 100644 --- a/websocket.go +++ b/websocket.go @@ -16,9 +16,9 @@ type MessageEvent Message -type SlackWS struct { +type WS struct { conn *websocket.Conn - messageId int + messageID int mutex sync.Mutex pings map[int]time.Time Slack @@ -29,15 +29,15 @@ ReplyTo int `json:"reply_to"` Timestamp string `json:"ts"` Text string `json:"text"` - SlackWSResponse -} - -type SlackWSResponse struct { - Ok bool `json:"ok"` - Error *SlackWSError `json:"error"` -} - -type SlackWSError struct { + WSResponse +} + +type WSResponse struct { + Ok bool `json:"ok"` + Error *WSError `json:"error"` +} + +type WSError struct { Code int Msg string } @@ -64,13 +64,13 @@ return fmt.Sprintf("\"%s\"", tm.Format("Mon Jan _2")) } -func (s SlackWSError) Error() string { +func (s WSError) Error() string { return s.Msg } var portMapping = map[string]string{"ws": "80", "wss": "443"} -func fixUrlPort(orig string) (string, error) { +func fixURLPort(orig string) (string, error) { urlObj, err := url.ParseRequestURI(orig) if err != nil { return "", err @@ -82,7 +82,7 @@ return orig, nil } -func (api *Slack) StartRTM(protocol, origin string) (*SlackWS, error) { +func (api *Slack) StartRTM(protocol, origin string) (*WS, error) { response := &infoResponseFull{} err := parseResponse("rtm.start", url.Values{"token": {api.config.token}}, response, api.debug) if err != nil { @@ -95,34 +95,34 @@ // websocket.Dial does not accept url without the port (yet) // Fixed by: https://github.com/golang/net/commit/5058c78c3627b31e484a81463acd51c7cecc06f3 // but slack returns the address with no port, so we have to fix it - api.info.Url, err = fixUrlPort(api.info.Url) + api.info.URL, err = fixURLPort(api.info.URL) if err != nil { return nil, err } api.config.protocol, api.config.origin = protocol, origin - wsApi := &SlackWS{Slack: *api} - wsApi.conn, err = websocket.Dial(api.info.Url, api.config.protocol, api.config.origin) + wsAPI := &WS{Slack: *api} + wsAPI.conn, err = websocket.Dial(api.info.URL, api.config.protocol, api.config.origin) if err != nil { return nil, err } - wsApi.pings = make(map[int]time.Time) - return wsApi, nil -} - -func (api *SlackWS) Ping() error { + wsAPI.pings = make(map[int]time.Time) + return wsAPI, nil +} + +func (api *WS) Ping() error { api.mutex.Lock() defer api.mutex.Unlock() - api.messageId++ - msg := &Ping{ID: api.messageId, Type: "ping"} + api.messageID++ + msg := &Ping{ID: api.messageID, Type: "ping"} if err := websocket.JSON.Send(api.conn, msg); err != nil { return err } // TODO: What happens if we already have this id? - api.pings[api.messageId] = time.Now() + api.pings[api.messageID] = time.Now() return nil } -func (api *SlackWS) Keepalive(interval time.Duration) { +func (api *WS) Keepalive(interval time.Duration) { ticker := time.NewTicker(interval) defer ticker.Stop() @@ -136,7 +136,7 @@ } } -func (api *SlackWS) SendMessage(msg *OutgoingMessage) error { +func (api *WS) SendMessage(msg *OutgoingMessage) error { if msg == nil { return fmt.Errorf("Can't send a nil message") } @@ -147,7 +147,7 @@ return nil } -func (api *SlackWS) HandleIncomingEvents(ch chan SlackEvent) { +func (api *WS) HandleIncomingEvents(ch chan SlackEvent) { for { event := json.RawMessage{} if err := websocket.JSON.Receive(api.conn, &event); err == io.EOF { @@ -157,7 +157,7 @@ //} // should we reconnect here? if !api.conn.IsClientConn() { - api.conn, err = websocket.Dial(api.info.Url, api.config.protocol, api.config.origin) + api.conn, err = websocket.Dial(api.info.URL, api.config.protocol, api.config.origin) if err != nil { log.Panic(err) } @@ -180,7 +180,7 @@ } } -func (api *SlackWS) handleEvent(ch chan SlackEvent, event json.RawMessage) { +func (api *WS) handleEvent(ch chan SlackEvent, event json.RawMessage) { em := Event{} err := json.Unmarshal(event, &em) if err != nil { diff --git a/websocket_teams.go b/websocket_teams.go index 0652115..123158a 100644 --- a/websocket_teams.go +++ b/websocket_teams.go @@ -19,7 +19,7 @@ type TeamDomainChangeEvent struct { Type string `json:"type"` - Url string `json:"url"` + URL string `json:"url"` Domain string `json:"domain"` }