Skip to content

Commit

Permalink
Notification change to Power Automate Webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
jggoebel committed Sep 10, 2024
1 parent 9bfcd1c commit 16c8b9b
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions v3/services/scoresvc/internal/scoreservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"os"
"sort"
Expand Down Expand Up @@ -256,13 +257,16 @@ func (s *ScoreServer) GetTimeout() time.Duration {
}

func (s *ScoreServer) SendNotification(code string) {
glog.Infof("Trying to send notification for ", code)
teamsWebhookUrl, found := os.LookupEnv("WEBHOOK_URL")
if !found {
glog.Infof("WEBHOOK_URL not found")
return
}

baseUrl, found := os.LookupEnv("BASE_URL")
if !found {
glog.Infof("BASE_URL not found")
return
}

Expand All @@ -276,15 +280,22 @@ func (s *ScoreServer) SendNotification(code string) {

// Creating the JSON payload for the Teams message
message := map[string]interface{}{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"summary": "New user scanned",
"sections": []map[string]interface{}{
"type": "message",
"attachments": []map[string]interface{}{
{
"activityTitle": "Scanned: `" + string(decodedCode) + "`",
"images": []map[string]interface{}{
{
"image": imageUrl,
"contentType": "application/vnd.microsoft.card.adaptive",
"content": map[string]interface{}{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.2",
"body": []map[string]string{
{
"type": "TextBlock",
"text": "Scanned: " + string(decodedCode),
}, {
"type": "Image",
"url": imageUrl,
},
},
},
},
Expand All @@ -294,14 +305,14 @@ func (s *ScoreServer) SendNotification(code string) {
// Marshal the map into a JSON string.
jsonData, err := json.Marshal(message)
if err != nil {
glog.Infof("Error creating JSON payload:", err)
glog.Errorf("Error creating JSON payload:", err)
return
}

// Create a new HTTP request with the JSON payload
req, err := http.NewRequest("POST", teamsWebhookUrl, bytes.NewBuffer(jsonData))
if err != nil {
glog.Infof("Error creating request:", err)
glog.Errorf("Error creating request:", err)
return
}

Expand All @@ -312,14 +323,17 @@ func (s *ScoreServer) SendNotification(code string) {
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
glog.Infof("Error sending request to Teams:", err)
glog.Errorf("Error sending request to Teams:", err)
return
}
defer resp.Body.Close()

// Check the HTTP response status
if resp.StatusCode != http.StatusOK {
glog.Infof("Failed to send message. Status: %d\n", resp.StatusCode)
// Check the HTTP response status, 202 = Accepted
if resp.StatusCode != 202 {
glog.Errorf("Failed to send message. Status: %d\n", resp.StatusCode)
bodyBytes, _ := io.ReadAll(resp.Body)
bodyString := string(bodyBytes)
glog.Errorf("message: %d\n", bodyString)
return
}

Expand Down

0 comments on commit 16c8b9b

Please sign in to comment.