Package list golang-github-nlopes-slack / 065c48f
Removed Slack prefix from some structs Norberto Lopes 6 years ago
7 changed file(s) with 42 addition(s) and 44 deletion(s). Raw diff Collapse all Expand all
00 - Add more tests!!!
1 - Add timeouts
2 - Fix the Websocket mess
31 - Add support to have markdown hints
42 - See section Message Formatting at https://api.slack.com/docs/formatting
3535 case *slack.LatencyReport:
3636 fmt.Printf("Current latency: %v\n", ev.Value)
3737
38 case *slack.SlackWSError:
38 case *slack.RTMError:
3939 fmt.Printf("Error: %s\n", ev.Error())
4040
4141 default:
156156
157157 type infoResponseFull struct {
158158 Info
159 SlackWebResponse
159 WebResponse
160160 }
161161
162162 // GetBotByID returns a bot given a bot id
1414 "path/filepath"
1515 "time"
1616 )
17
18 type WebResponse struct {
19 Ok bool `json:"ok"`
20 Error *WebError `json:"error"`
21 }
22
23 type WebError string
24
25 func (s WebError) Error() string {
26 return string(s)
27 }
1728
1829 func fileUploadReq(path, fpath string, values url.Values) (*http.Request, error) {
1930 fullpath, err := filepath.Abs(fpath)
2424
2525 // Connection life-cycle
2626 conn *websocket.Conn
27 IncomingEvents chan SlackEvent
27 IncomingEvents chan RTMEvent
2828 outgoingMessages chan OutgoingMessage
2929 killChannel chan bool
3030 forcePing chan bool
4545 func newRTM(api *Client) *RTM {
4646 return &RTM{
4747 Client: *api,
48 IncomingEvents: make(chan SlackEvent, 50),
48 IncomingEvents: make(chan RTMEvent, 50),
4949 outgoingMessages: make(chan OutgoingMessage, 20),
5050 pings: make(map[int]time.Time),
5151 isConnected: false,
3535 return
3636 }
3737 rtm.info = info
38 rtm.IncomingEvents <- SlackEvent{"connected", &ConnectedEvent{
38 rtm.IncomingEvents <- RTMEvent{"connected", &ConnectedEvent{
3939 ConnectionCount: connectionCount,
4040 Info: info,
4141 }}
7575
7676 for {
7777 // send connecting event
78 rtm.IncomingEvents <- SlackEvent{"connecting", &ConnectingEvent{
78 rtm.IncomingEvents <- RTMEvent{"connecting", &ConnectingEvent{
7979 Attempt: boff.attempts + 1,
8080 ConnectionCount: connectionCount,
8181 }}
8585 return info, conn, nil
8686 }
8787 // check for fatal errors - currently only invalid_auth
88 if sErr, ok := err.(*SlackWebError); ok && sErr.Error() == "invalid_auth" {
89 rtm.IncomingEvents <- SlackEvent{"invalid_auth", &InvalidAuthEvent{}}
88 if sErr, ok := err.(*WebError); ok && sErr.Error() == "invalid_auth" {
89 rtm.IncomingEvents <- RTMEvent{"invalid_auth", &InvalidAuthEvent{}}
9090 return nil, nil, sErr
9191 }
9292 // any other errors are treated as recoverable and we try again after
9393 // sending the event along the IncomingEvents channel
94 rtm.IncomingEvents <- SlackEvent{"connection_error", &ConnectionErrorEvent{
94 rtm.IncomingEvents <- RTMEvent{"connection_error", &ConnectionErrorEvent{
9595 Attempt: boff.attempts,
9696 ErrorObj: err,
9797 }}
131131 rtm.isConnected = false
132132 rtm.wasIntentional = intentional
133133 err := rtm.conn.Close()
134 rtm.IncomingEvents <- SlackEvent{"disconnected", &DisconnectedEvent{intentional}}
134 rtm.IncomingEvents <- RTMEvent{"disconnected", &DisconnectedEvent{intentional}}
135135 return err
136136 }
137137
197197 func (rtm *RTM) sendOutgoingMessage(msg OutgoingMessage) {
198198 rtm.Debugln("Sending message:", msg)
199199 if len(msg.Text) > MaxMessageTextLength {
200 rtm.IncomingEvents <- SlackEvent{"outgoing_error", &MessageTooLongEvent{
200 rtm.IncomingEvents <- RTMEvent{"outgoing_error", &MessageTooLongEvent{
201201 Message: msg,
202202 MaxLength: MaxMessageTextLength,
203203 }}
205205 }
206206 err := websocket.JSON.Send(rtm.conn, msg)
207207 if err != nil {
208 rtm.IncomingEvents <- SlackEvent{"outgoing_error", &OutgoingErrorEvent{
208 rtm.IncomingEvents <- RTMEvent{"outgoing_error", &OutgoingErrorEvent{
209209 Message: msg,
210210 ErrorObj: err,
211211 }}
248248 rtm.forcePing <- true
249249 return
250250 } else if err != nil {
251 rtm.IncomingEvents <- SlackEvent{"incoming_error", &IncomingEventError{
251 rtm.IncomingEvents <- RTMEvent{"incoming_error", &IncomingEventError{
252252 ErrorObj: err,
253253 }}
254254 // force a ping here too?
267267 event := &Event{}
268268 err := json.Unmarshal(rawEvent, event)
269269 if err != nil {
270 rtm.IncomingEvents <- SlackEvent{"unmarshalling_error", &UnmarshallingErrorEvent{err}}
270 rtm.IncomingEvents <- RTMEvent{"unmarshalling_error", &UnmarshallingErrorEvent{err}}
271271 return
272272 }
273273 switch event.Type {
274274 case "":
275275 rtm.handleAck(rawEvent)
276276 case "hello":
277 rtm.IncomingEvents <- SlackEvent{"hello", &HelloEvent{}}
277 rtm.IncomingEvents <- RTMEvent{"hello", &HelloEvent{}}
278278 case "pong":
279279 rtm.handlePong(rawEvent)
280280 default:
291291 return
292292 }
293293 if ack.Ok {
294 rtm.IncomingEvents <- SlackEvent{"ack", ack}
294 rtm.IncomingEvents <- RTMEvent{"ack", ack}
295295 } else {
296 rtm.IncomingEvents <- SlackEvent{"ack_error", &AckErrorEvent{ack.Error}}
296 rtm.IncomingEvents <- RTMEvent{"ack_error", &AckErrorEvent{ack.Error}}
297297 }
298298 }
299299
309309 }
310310 if pingTime, exists := rtm.pings[pong.ReplyTo]; exists {
311311 latency := time.Since(pingTime)
312 rtm.IncomingEvents <- SlackEvent{"latency_report", &LatencyReport{Value: latency}}
312 rtm.IncomingEvents <- RTMEvent{"latency_report", &LatencyReport{Value: latency}}
313313 delete(rtm.pings, pong.ReplyTo)
314314 } else {
315315 rtm.Debugln("RTM Error - unmatched 'pong' event:", string(event))
327327 if !exists {
328328 rtm.Debugf("RTM Error, received unmapped event %q: %s\n", typeStr, string(event))
329329 err := fmt.Errorf("RTM Error: Received unmapped event %q: %s\n", typeStr, string(event))
330 rtm.IncomingEvents <- SlackEvent{"unmarshalling_error", &UnmarshallingErrorEvent{err}}
330 rtm.IncomingEvents <- RTMEvent{"unmarshalling_error", &UnmarshallingErrorEvent{err}}
331331 return
332332 }
333333 t := reflect.TypeOf(v)
336336 if err != nil {
337337 rtm.Debugf("RTM Error, could not unmarshall event %q: %s\n", typeStr, string(event))
338338 err := fmt.Errorf("RTM Error: Could not unmarshall event %q: %s\n", typeStr, string(event))
339 rtm.IncomingEvents <- SlackEvent{"unmarshalling_error", &UnmarshallingErrorEvent{err}}
340 return
341 }
342 rtm.IncomingEvents <- SlackEvent{typeStr, recvEvent}
339 rtm.IncomingEvents <- RTMEvent{"unmarshalling_error", &UnmarshallingErrorEvent{err}}
340 return
341 }
342 rtm.IncomingEvents <- RTMEvent{typeStr, recvEvent}
343343 }
344344
345345 // eventMapping holds a mapping of event names to their corresponding struct
99 ReplyTo int `json:"reply_to"`
1010 Timestamp string `json:"ts"`
1111 Text string `json:"text"`
12 SlackWSResponse
12 RTMResponse
1313 }
1414
15 type SlackWebResponse struct {
16 Ok bool `json:"ok"`
17 Error *SlackWebError `json:"error"`
15 type RTMResponse struct {
16 Ok bool `json:"ok"`
17 Error *RTMError `json:"error"`
1818 }
1919
20 type SlackWebError string
21
22 func (s SlackWebError) Error() string {
23 return string(s)
24 }
25
26 type SlackWSResponse struct {
27 Ok bool `json:"ok"`
28 Error *SlackWSError `json:"error"`
29 }
30
31 type SlackWSError struct {
20 type RTMError struct {
3221 Code int
3322 Msg string
3423 }
3524
36 func (s SlackWSError) Error() string {
25 func (s RTMError) Error() string {
3726 return fmt.Sprintf("Code %d - %s", s.Code, s.Msg)
3827 }
3928
4029 type MessageEvent Message
4130
42 // SlackEvent is the main wrapper. You will find all the other messages attached
43 type SlackEvent struct {
31 // RTMEvent is the main wrapper. You will find all the other messages attached
32 type RTMEvent struct {
4433 Type string
4534 Data interface{}
4635 }