Skip to content

Commit

Permalink
Merge pull request #71 from natron-io/notifications
Browse files Browse the repository at this point in the history
update message return values
  • Loading branch information
janlauber authored Feb 8, 2022
2 parents f69a5af + ffd1389 commit 5497e1b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
39 changes: 33 additions & 6 deletions controllers/notificationController.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func GetNotifications(c *fiber.Ctx) error {
}

// make slack api call to get all notifications of the BroadCast channel
resp, err := util.SlackClient.GetConversationHistory(&historyParams)
responseConversationHistory, err := util.SlackClient.GetConversationHistory(&historyParams)

if err != nil {
util.ErrorLogger.Printf("%s", err)
Expand All @@ -40,11 +40,38 @@ func GetNotifications(c *fiber.Ctx) error {
})
}

// parse messages to string slice
var notifications []string
for _, message := range resp.Messages {
if message.Blocks.BlockSet != nil {
notifications = append(notifications, message.Text)
// get the slack url of the channel

// create map of notifications with client_msg_id as key and message as value and username

type Notification struct {
ClientMsgID string `json:"client_msg_id"`
Message string `json:"message"`
UserRealName string `json:"user_real_name"`
UserAvatarURL string `json:"user_avatar_url"`
UnixTimestamp string `json:"unix_timestamp"`
LinkToMessage string `json:"link_to_message"`
}

notifications := []Notification{}
for _, message := range responseConversationHistory.Messages {
responseUserName, err := util.SlackClient.GetUserInfo(message.User)

if err != nil {
util.ErrorLogger.Printf("%s", err)
continue
}

if message.ClientMsgID != "" {
// json map of notifications
notifications = append(notifications, Notification{
ClientMsgID: message.ClientMsgID,
Message: message.Text,
UserRealName: responseUserName.Profile.RealName,
UserAvatarURL: responseUserName.Profile.Image192,
UnixTimestamp: message.Timestamp,
LinkToMessage: util.SlackURL + "/archives/" + util.BroadCastChannelID + "/p" + message.Timestamp,
})
}
}

Expand Down
19 changes: 16 additions & 3 deletions util/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,24 @@ func LoadEnv() error {
if SLACK_TOKEN = os.Getenv("SLACK_TOKEN"); SLACK_TOKEN == "" {
WarningLogger.Println("SLACK_TOKEN is not set")
SLACK_TOKEN = ""
} else {
InfoLogger.Printf("SLACK_TOKEN set using env: %s", SLACK_TOKEN)
}

if BroadCastChannelID = os.Getenv("SLACK_BROADCAST_CHANNEL_ID"); BroadCastChannelID == "" {
WarningLogger.Println("SLACK_BROADCAST_CHANNEL_ID is not set")
BroadCastChannelID = ""
if BroadCastChannelID = os.Getenv("SLACK_BROADCAST_CHANNEL_ID"); BroadCastChannelID == "" && SLACK_TOKEN != "" {
ErrorLogger.Println("SLACK_BROADCAST_CHANNEL_ID is not set")
Status = "Error: SLACK_BROADCAST_CHANNEL_ID is not set"
os.Exit(1)
} else {
InfoLogger.Printf("SLACK_BROADCAST_CHANNEL_ID set using env: %s", BroadCastChannelID)
}

if SlackURL = os.Getenv("SLACK_URL"); SlackURL == "" && SLACK_TOKEN != "" {
ErrorLogger.Println("SLACK_URL is not set")
Status = "Error: SLACK_URL is not set"
os.Exit(1)
} else {
InfoLogger.Printf("SLACK_URL set using env: %s", SlackURL)
}

// get every env variable starting with STORAGE_COST_ and parse it to STORAGE_COST with the storage class name after STORAGE_COST_ as key
Expand Down
1 change: 1 addition & 0 deletions util/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ var (
SLACK_TOKEN = os.Getenv("SLACK_TOKEN")
SlackClient *slack.Client
BroadCastChannelID string
SlackURL string
)

0 comments on commit 5497e1b

Please sign in to comment.