Created
October 25, 2021 15:08
-
-
Save afro-coder/b53a8b21f46396101487e980b689c63c to your computer and use it in GitHub Desktop.
Gist to read data from Salesforce and push to Microsoft Teams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"net/url" | |
"os" | |
"reflect" | |
"time" | |
) | |
func main() { | |
f, err := os.OpenFile("chat_notifier.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) | |
if err != nil { | |
log.Fatalf("error opening file: %v", err) | |
} | |
defer f.Close() | |
log.SetOutput(f) | |
// Get waiting chats | |
query := "Salesforce Query" | |
var waiting_chats = "https://salesforceURL/services/data/v47.0/query/?q=" + url.QueryEscape(query) | |
client := &http.Client{} | |
req, err := http.NewRequest("GET", waiting_chats, nil) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
req.Header.Add("Authorization", "Bearer Token") | |
res, err := client.Do(req) | |
if res.StatusCode != 200 { | |
log.Fatalln(res.Status) | |
} | |
if err != nil { | |
log.Fatalln(err) | |
} | |
defer res.Body.Close() | |
body, err := ioutil.ReadAll(res.Body) | |
//Convert the body to type string | |
if err != nil { | |
log.Fatalln(err) | |
} | |
var data map[string]interface{} | |
err = json.Unmarshal(body, &data) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
//log.Println(data["totalSize"]) | |
log.Println(data) | |
// Convert to Float then int | |
dat := int(data["totalSize"].(float64)) | |
log.Println(reflect.TypeOf(dat)) | |
if dat > 0 { | |
var payload = []byte(`{ | |
"type":"message", | |
"attachments":[ | |
{ | |
"contentType":"application/vnd.microsoft.card.adaptive", | |
"contentUrl":null, | |
"content":{ | |
"$schema":"http://adaptivecards.io/schemas/adaptive-card.json", | |
"type":"AdaptiveCard", | |
"version":"1.2", | |
"body":[ | |
{ | |
"type": "TextBlock", | |
"text": "Enter your Text", | |
"size": "Large", | |
"weight": "Bolder", | |
"wrap": true | |
}, | |
] | |
} | |
} | |
] | |
}`) | |
log.Printf("Sending data to the webhook") | |
request, err := http.NewRequest("POST", "MICROSOFT_WEBHOOK_URL", bytes.NewBuffer(payload)) | |
log.Println(string(body)) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
request.Header.Set("Content-Type", "application/json") | |
response, error := client.Do(request) | |
body, _ := ioutil.ReadAll(response.Body) | |
log.Println(string(body)) | |
if error != nil { | |
log.Fatalln(error) | |
} | |
defer response.Body.Close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment