Add slack webhook integration

This commit is contained in:
Daniel Mason 2021-09-01 23:20:47 +12:00
parent c109f08589
commit 1718cc052d
Signed by: idanoo
GPG key ID: 387387CDBC02F132
9 changed files with 107 additions and 5 deletions

View file

@ -18,6 +18,7 @@ type UpdatedRow struct {
ChangeType string // ADDED, REMOVED, MODIFIED
DiscordData string // Formatted Row data
TwitterData string // Formatted Row data
SlackData string // Formatted Row data
}
// Struct of updated locations
@ -29,6 +30,7 @@ func parseCsvRow(changeType string, data string) {
ChangeType: changeType,
DiscordData: formatCsvDiscordRow(data),
TwitterData: formatCsvTwitterRow(data),
SlackData: formatCsvSlackRow(data),
}
updatedLocations.Locations = append(updatedLocations.Locations, newRow)
@ -46,6 +48,12 @@ func formatCsvTwitterRow(data string) string {
return fmt.Sprintf("New Location: %s\n%s\n%s - %s\n#NZCovidTracker #NZCovid", c[2], c[3], c[0], c[1])
}
// formatCsvSlackRow Format the string to a tidy string for the interwebs
func formatCsvSlackRow(data string) string {
c := parseRawRowData(data)
return fmt.Sprintf("*%s* %s on _%s_ - _%s_", c[2], c[3], c[0], c[1])
}
// Returns []string of parsed
func parseRawRowData(data string) []string {
output := make([]string, 0)
@ -95,5 +103,22 @@ func getPostableDiscordData() string {
}
}
return strings.Join(rows, "\n")
return "\\`\\`\\`\n" + strings.Join(rows, "\n") + "\n\\`\\`\\`"
}
func getPostableSlackData() []string {
rows := make([]string, 0)
if len(updatedLocations.Locations) == 0 {
return rows
}
for _, location := range updatedLocations.Locations {
if location.ChangeType == "REMOVED" {
rows = append(rows, fmt.Sprintf("REMOVED: %s", location.SlackData))
} else {
rows = append(rows, location.SlackData)
}
}
return rows
}

View file

@ -8,10 +8,8 @@ import (
"github.com/DisgoOrg/disgohook/api"
)
var (
// Slice of discord webhooks
DiscordWebhooks []string
)
// Slice of discord webhooks
var DiscordWebhooks []string
func postToDiscord(webhookString string, msg string) {
tokenParts := strings.Split(webhookString, "/")

View file

@ -37,6 +37,9 @@ func postTheUpdates() {
// Twitter
go postToTwitter()
// Slack
go postToSlack()
// Discord
postableDiscordData := getPostableDiscordData()
if postableDiscordData == "" {

View file

@ -0,0 +1,39 @@
package nzcovidbot
import (
"fmt"
"github.com/ashwanthkumar/slack-go-webhook"
)
// Slack webhook URL
var SlackWebhook string
func postToSlack() {
if SlackWebhook == "" {
return
}
postableData := getPostableSlackData()
if len(postableData) == 0 {
return
}
attachment1 := slack.Attachment{}
for _, v := range postableData {
attachment1.AddField(slack.Field{Value: v})
}
payload := slack.Payload{
Text: "New Locations of Interest!",
Username: "NZCovidTracker",
Channel: "#covid-19",
IconUrl: "https://www.skids.co.nz/wp-content/uploads/2020/08/download.png",
Attachments: []slack.Attachment{attachment1},
}
err := slack.Send(SlackWebhook, "", payload)
if len(err) > 0 {
fmt.Printf("error: %s\n", err)
}
}

View file

@ -22,6 +22,10 @@ func postToTwitter() {
return
}
if len(updatedLocations.Locations) == 0 {
return
}
config := oauth1.NewConfig(TwitterCreds.ConsumerKey, TwitterCreds.ConsumerSecret)
token := oauth1.NewToken(TwitterCreds.AccessToken, TwitterCreds.AccessTokenSecret)
httpClient := config.Client(oauth1.NoContext, token)