diff --git a/daily.go b/daily.go index 8b79efb..90a7535 100644 --- a/daily.go +++ b/daily.go @@ -3,7 +3,7 @@ package main import ( - "fmt" + "log" "os" "sort" "strconv" @@ -38,13 +38,16 @@ func main() { message := createMessage(yesterDay, channels, count) botClient := slack.New(os.Getenv("SLACK_BOT_TOKEN")) - botClient.PostMessage(os.Getenv("SLACK_CHANNEL_ID"), slack.MsgOptionText(message, false)) + _, _, err := botClient.PostMessage(os.Getenv("SLACK_CHANNEL_ID"), slack.MsgOptionText(message, false)) + if err != nil { + log.Printf("can not post: %v\n", err) + } } func (client SlackClient) getConversationsForUser() []slack.Channel { conversations, _, err := client.GetConversationsForUser(&slack.GetConversationsForUserParameters{}) if err != nil { - fmt.Println(err) + log.Printf("can not get channels: %v\n", err) } return conversations } @@ -56,7 +59,11 @@ func createChannels(conversations []slack.Channel, now time.Time, yesterDay time var count int for _, conversation := range conversations { params := slack.GetConversationHistoryParameters{ChannelID: conversation.ID, Limit: 1000, Latest: latest, Oldest: oldest} - conversationHistory, _ := userClient.GetConversationHistory(¶ms) + conversationHistory, err := userClient.GetConversationHistory(¶ms) + if err != nil { + log.Printf("can not get history channelID: %s, %v\n", conversation.ID, err) + continue + } channel := Channel{name: conversation.Name, id: conversation.ID} for _, message := range conversationHistory.Messages { count++ diff --git a/daily_test.go b/daily_test.go index 6dcd3d0..6539743 100644 --- a/daily_test.go +++ b/daily_test.go @@ -5,6 +5,7 @@ package main import ( "bytes" "embed" + "log" "net/http" "os" "strings" @@ -41,7 +42,7 @@ func TestGetConversationsForUser(t *testing.T) { { name: "usersConversationsError", apiRes: "testdata/usersConversations/error.json", - want: want{channels: []slack.Channel{}, print: "invalid_auth"}, + want: want{channels: []slack.Channel{}, print: "can not get channels: invalid_auth"}, }, } for _, tt := range tests { @@ -56,21 +57,21 @@ func TestGetConversationsForUser(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Helper() - orgStdout := os.Stdout + var buf bytes.Buffer + log.SetOutput(&buf) + defaultFlags := log.Flags() + log.SetFlags(0) defer func() { - os.Stdout = orgStdout + log.SetOutput(os.Stderr) + log.SetFlags(defaultFlags) + buf.Reset() }() - r, w, _ := os.Pipe() - os.Stdout = w + got := SlackClient{client}.getConversationsForUser() + if len(got) != len(tt.want.channels) { t.Errorf("add() = %v, want %v", got, tt.want.channels) } - w.Close() - var buf bytes.Buffer - if _, err := buf.ReadFrom(r); err != nil { - t.Fatalf("failed to read buf: %v", err) - } gotPrint := strings.TrimRight(buf.String(), "\n") if gotPrint != tt.want.print { t.Errorf("add() = %v, want %v", gotPrint, tt.want.print)