diff --git a/TODO.txt b/TODO.txt index 037508d..8607960 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,5 +1,3 @@ - Add more tests!!! -- Add timeouts -- Fix the Websocket mess - Add support to have markdown hints - See section Message Formatting at https://api.slack.com/docs/formatting diff --git a/examples/websocket/websocket.go b/examples/websocket/websocket.go index e7e70a8..fc6513d 100644 --- a/examples/websocket/websocket.go +++ b/examples/websocket/websocket.go @@ -36,7 +36,7 @@ case *slack.LatencyReport: fmt.Printf("Current latency: %v\n", ev.Value) - case *slack.SlackWSError: + case *slack.RTMError: fmt.Printf("Error: %s\n", ev.Error()) default: diff --git a/info.go b/info.go index 17178bb..962531a 100644 --- a/info.go +++ b/info.go @@ -157,7 +157,7 @@ type infoResponseFull struct { Info - SlackWebResponse + WebResponse } // GetBotByID returns a bot given a bot id diff --git a/misc.go b/misc.go index 1438e46..03ef9dc 100644 --- a/misc.go +++ b/misc.go @@ -15,6 +15,17 @@ "path/filepath" "time" ) + +type WebResponse struct { + Ok bool `json:"ok"` + Error *WebError `json:"error"` +} + +type WebError string + +func (s WebError) Error() string { + return string(s) +} func fileUploadReq(path, fpath string, values url.Values) (*http.Request, error) { fullpath, err := filepath.Abs(fpath) diff --git a/websocket.go b/websocket.go index da5c91d..79314e9 100644 --- a/websocket.go +++ b/websocket.go @@ -25,7 +25,7 @@ // Connection life-cycle conn *websocket.Conn - IncomingEvents chan SlackEvent + IncomingEvents chan RTMEvent outgoingMessages chan OutgoingMessage killChannel chan bool forcePing chan bool @@ -46,7 +46,7 @@ func newRTM(api *Client) *RTM { return &RTM{ Client: *api, - IncomingEvents: make(chan SlackEvent, 50), + IncomingEvents: make(chan RTMEvent, 50), outgoingMessages: make(chan OutgoingMessage, 20), pings: make(map[int]time.Time), isConnected: false, diff --git a/websocket_managed_conn.go b/websocket_managed_conn.go index 6f9bfc0..f597446 100644 --- a/websocket_managed_conn.go +++ b/websocket_managed_conn.go @@ -36,7 +36,7 @@ return } rtm.info = info - rtm.IncomingEvents <- SlackEvent{"connected", &ConnectedEvent{ + rtm.IncomingEvents <- RTMEvent{"connected", &ConnectedEvent{ ConnectionCount: connectionCount, Info: info, }} @@ -76,7 +76,7 @@ for { // send connecting event - rtm.IncomingEvents <- SlackEvent{"connecting", &ConnectingEvent{ + rtm.IncomingEvents <- RTMEvent{"connecting", &ConnectingEvent{ Attempt: boff.attempts + 1, ConnectionCount: connectionCount, }} @@ -86,13 +86,13 @@ return info, conn, nil } // check for fatal errors - currently only invalid_auth - if sErr, ok := err.(*SlackWebError); ok && sErr.Error() == "invalid_auth" { - rtm.IncomingEvents <- SlackEvent{"invalid_auth", &InvalidAuthEvent{}} + if sErr, ok := err.(*WebError); ok && sErr.Error() == "invalid_auth" { + rtm.IncomingEvents <- RTMEvent{"invalid_auth", &InvalidAuthEvent{}} return nil, nil, sErr } // any other errors are treated as recoverable and we try again after // sending the event along the IncomingEvents channel - rtm.IncomingEvents <- SlackEvent{"connection_error", &ConnectionErrorEvent{ + rtm.IncomingEvents <- RTMEvent{"connection_error", &ConnectionErrorEvent{ Attempt: boff.attempts, ErrorObj: err, }} @@ -132,7 +132,7 @@ rtm.isConnected = false rtm.wasIntentional = intentional err := rtm.conn.Close() - rtm.IncomingEvents <- SlackEvent{"disconnected", &DisconnectedEvent{intentional}} + rtm.IncomingEvents <- RTMEvent{"disconnected", &DisconnectedEvent{intentional}} return err } @@ -198,7 +198,7 @@ func (rtm *RTM) sendOutgoingMessage(msg OutgoingMessage) { rtm.Debugln("Sending message:", msg) if len(msg.Text) > MaxMessageTextLength { - rtm.IncomingEvents <- SlackEvent{"outgoing_error", &MessageTooLongEvent{ + rtm.IncomingEvents <- RTMEvent{"outgoing_error", &MessageTooLongEvent{ Message: msg, MaxLength: MaxMessageTextLength, }} @@ -206,7 +206,7 @@ } err := websocket.JSON.Send(rtm.conn, msg) if err != nil { - rtm.IncomingEvents <- SlackEvent{"outgoing_error", &OutgoingErrorEvent{ + rtm.IncomingEvents <- RTMEvent{"outgoing_error", &OutgoingErrorEvent{ Message: msg, ErrorObj: err, }} @@ -249,7 +249,7 @@ rtm.forcePing <- true return } else if err != nil { - rtm.IncomingEvents <- SlackEvent{"incoming_error", &IncomingEventError{ + rtm.IncomingEvents <- RTMEvent{"incoming_error", &IncomingEventError{ ErrorObj: err, }} // force a ping here too? @@ -268,14 +268,14 @@ event := &Event{} err := json.Unmarshal(rawEvent, event) if err != nil { - rtm.IncomingEvents <- SlackEvent{"unmarshalling_error", &UnmarshallingErrorEvent{err}} + rtm.IncomingEvents <- RTMEvent{"unmarshalling_error", &UnmarshallingErrorEvent{err}} return } switch event.Type { case "": rtm.handleAck(rawEvent) case "hello": - rtm.IncomingEvents <- SlackEvent{"hello", &HelloEvent{}} + rtm.IncomingEvents <- RTMEvent{"hello", &HelloEvent{}} case "pong": rtm.handlePong(rawEvent) default: @@ -292,9 +292,9 @@ return } if ack.Ok { - rtm.IncomingEvents <- SlackEvent{"ack", ack} + rtm.IncomingEvents <- RTMEvent{"ack", ack} } else { - rtm.IncomingEvents <- SlackEvent{"ack_error", &AckErrorEvent{ack.Error}} + rtm.IncomingEvents <- RTMEvent{"ack_error", &AckErrorEvent{ack.Error}} } } @@ -310,7 +310,7 @@ } if pingTime, exists := rtm.pings[pong.ReplyTo]; exists { latency := time.Since(pingTime) - rtm.IncomingEvents <- SlackEvent{"latency_report", &LatencyReport{Value: latency}} + rtm.IncomingEvents <- RTMEvent{"latency_report", &LatencyReport{Value: latency}} delete(rtm.pings, pong.ReplyTo) } else { rtm.Debugln("RTM Error - unmatched 'pong' event:", string(event)) @@ -328,7 +328,7 @@ if !exists { rtm.Debugf("RTM Error, received unmapped event %q: %s\n", typeStr, string(event)) err := fmt.Errorf("RTM Error: Received unmapped event %q: %s\n", typeStr, string(event)) - rtm.IncomingEvents <- SlackEvent{"unmarshalling_error", &UnmarshallingErrorEvent{err}} + rtm.IncomingEvents <- RTMEvent{"unmarshalling_error", &UnmarshallingErrorEvent{err}} return } t := reflect.TypeOf(v) @@ -337,10 +337,10 @@ if err != nil { rtm.Debugf("RTM Error, could not unmarshall event %q: %s\n", typeStr, string(event)) err := fmt.Errorf("RTM Error: Could not unmarshall event %q: %s\n", typeStr, string(event)) - rtm.IncomingEvents <- SlackEvent{"unmarshalling_error", &UnmarshallingErrorEvent{err}} - return - } - rtm.IncomingEvents <- SlackEvent{typeStr, recvEvent} + rtm.IncomingEvents <- RTMEvent{"unmarshalling_error", &UnmarshallingErrorEvent{err}} + return + } + rtm.IncomingEvents <- RTMEvent{typeStr, recvEvent} } // eventMapping holds a mapping of event names to their corresponding struct diff --git a/websocket_misc.go b/websocket_misc.go index 93476b9..eca98b5 100644 --- a/websocket_misc.go +++ b/websocket_misc.go @@ -10,38 +10,27 @@ ReplyTo int `json:"reply_to"` Timestamp string `json:"ts"` Text string `json:"text"` - SlackWSResponse + RTMResponse } -type SlackWebResponse struct { - Ok bool `json:"ok"` - Error *SlackWebError `json:"error"` +type RTMResponse struct { + Ok bool `json:"ok"` + Error *RTMError `json:"error"` } -type SlackWebError string - -func (s SlackWebError) Error() string { - return string(s) -} - -type SlackWSResponse struct { - Ok bool `json:"ok"` - Error *SlackWSError `json:"error"` -} - -type SlackWSError struct { +type RTMError struct { Code int Msg string } -func (s SlackWSError) Error() string { +func (s RTMError) Error() string { return fmt.Sprintf("Code %d - %s", s.Code, s.Msg) } type MessageEvent Message -// SlackEvent is the main wrapper. You will find all the other messages attached -type SlackEvent struct { +// RTMEvent is the main wrapper. You will find all the other messages attached +type RTMEvent struct { Type string Data interface{} }