Update slack to push linkable attachments

This commit is contained in:
Daniel Mason 2021-09-07 13:37:11 +12:00
parent 45a6b9e9df
commit 7b654c0bf0
Signed by: idanoo
GPG key ID: 387387CDBC02F132
3 changed files with 51 additions and 24 deletions

View file

@ -2,7 +2,7 @@ SOURCE_REPO=https://github.com/minhealthnz/nz-covid-data.git // Source repo
DISCORD_WEBHOOKS= // Comma separated DISCORD_WEBHOOKS= // Comma separated
SLACK_WEBHOOKS= // Comma separated SLACK_WEBHOOKS= // webhook1!channel1,webhook2!channel2 Comma separated with ! for channel hash
TWITTER_CONSUMER_KEY= TWITTER_CONSUMER_KEY=
TWITTER_CONSUMER_SECRET= TWITTER_CONSUMER_SECRET=

View file

@ -6,10 +6,14 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"net/url"
"os" "os"
"regexp"
"sort" "sort"
"strings" "strings"
"time" "time"
"github.com/ashwanthkumar/slack-go-webhook"
) )
// Slice of updated located // Slice of updated located
@ -24,9 +28,9 @@ type UpdatedRow struct {
LocationName string `json:"LocationName"` // Location Name LocationName string `json:"LocationName"` // Location Name
LocationAddress string `json:"LocationAddress"` // Location Address LocationAddress string `json:"LocationAddress"` // Location Address
DiscordData string `json:"-"` // Formatted Row data DiscordData string `json:"-"` // Formatted Row data
TwitterData string `json:"-"` // Formatted Row data TwitterData string `json:"-"` // Formatted Row data
SlackData string `json:"-"` // Formatted Row data SlackData slack.Attachment `json:"-"` // Formatted Row data
} }
// Struct of updated locations // Struct of updated locations
@ -198,8 +202,34 @@ func formatCsvTwitterRow(c []string) string {
} }
// formatCsvSlackRow Format the string to a tidy string for the interwebs // formatCsvSlackRow Format the string to a tidy string for the interwebs
func formatCsvSlackRow(c []string) string { func formatCsvSlackRow(c []string) slack.Attachment {
return fmt.Sprintf("*%s* %s on _%s_ - _%s_", c[2], c[3], c[0], c[1]) url := getMapsLinkFromAddress(c[2], c[3])
name := stripDateFromName(c[2])
dateRange := fmt.Sprintf("%s - %s", c[0], c[1])
attachment := slack.Attachment{
Title: &name,
TitleLink: &url,
Text: &dateRange,
}
return attachment
}
// getMapsLinkFromAddress hyperlink gmaps
func getMapsLinkFromAddress(name string, address string) string {
return fmt.Sprintf("https://www.google.com/maps/search/?api=1&query=%s", url.QueryEscape(name+", "+address))
}
// stripDateFromName if theres a date at the end - remove it!
func stripDateFromName(name string) string {
re := regexp.MustCompile(`\d{1,2}/\d{1,2}/\d{2,4}`)
submatchall := re.FindAllString(name, -1)
for _, element := range submatchall {
name = strings.Replace(name, element, "", 1)
break
}
return strings.TrimSpace(name)
} }
// Returns []string of parsed data.. starttime, endtime, name, address, ID // Returns []string of parsed data.. starttime, endtime, name, address, ID
@ -267,8 +297,8 @@ func getPostableDiscordData() []string {
return append(groups, strings.Join(rows, "\n")) return append(groups, strings.Join(rows, "\n"))
} }
func getPostableSlackData() []string { func getPostableSlackData() []slack.Attachment {
rows := make([]string, 0) rows := make([]slack.Attachment, 0)
if len(updatedLocations.Locations) == 0 { if len(updatedLocations.Locations) == 0 {
return rows return rows
} }

View file

@ -7,7 +7,7 @@ import (
"github.com/ashwanthkumar/slack-go-webhook" "github.com/ashwanthkumar/slack-go-webhook"
) )
// Slack webhook URL // Slack webhook#channel
var SlackWebhooks []string var SlackWebhooks []string
func postToSlack() { func postToSlack() {
@ -15,29 +15,26 @@ func postToSlack() {
return return
} }
postableData := getPostableSlackData() attachmentData := getPostableSlackData()
if len(postableData) == 0 { if len(attachmentData) == 0 {
return return
} }
rawText := strings.Join(postableData, "\n")
attachment1 := slack.Attachment{}
attachment1.Text = &rawText
payload := slack.Payload{
Text: "New Locations of Interest!",
Username: "NZCovidTracker",
Channel: "#covid-19-locations",
IconUrl: "https://www.skids.co.nz/wp-content/uploads/2020/08/download.png",
Attachments: []slack.Attachment{attachment1},
}
for _, webhook := range SlackWebhooks { for _, webhook := range SlackWebhooks {
if webhook == "" { if webhook == "" {
continue continue
} }
err := slack.Send(webhook, "", payload) parts := strings.Split(webhook, "!")
payload := slack.Payload{
Text: "New Locations of Interest!",
Username: "NZCovidTracker",
Channel: "#" + parts[1],
IconUrl: "https://www.skids.co.nz/wp-content/uploads/2020/08/download.png",
Attachments: attachmentData,
}
err := slack.Send(parts[0], "", payload)
if len(err) > 0 { if len(err) > 0 {
fmt.Print(err) fmt.Print(err)
} }