Skip to content

Instantly share code, notes, and snippets.

@afro-coder
Created Oct 25, 2021
Embed
What would you like to do?
Pull Data from Salesforce and push to MS teams
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
//"reflect"
"time"
//"fmt"
"strings"
)
type Respdata struct {
TotalSize int `json:"totalSize"`
Done bool `json:"done"`
Records []struct {
Attributes struct {
Type string `json:"type,omitempty"`
Url string `json:"url,omitempty"`
} `json:"attributes,omitempty"`
CaseNumber string `json:"CaseNumber,omitempty"`
Origin string `json:"Origin,omitempty"`
CreatedDate string `json:"CreatedDate,omitempty"`
} `json:"records,omitempty"`
}
func main() {
f, err := os.OpenFile("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)
query := "Salesforce Query"
var waiting_chats = "https://Salesforce.com/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{}
var data Respdata
err = json.Unmarshal(body, &data)
if err != nil {
log.Fatalln(err)
}
log.Println(data)
// Create a slice
var s []string
// Range over the map and push to the string array or make it into bytes with new lines
for _, elem := range data.Records {
s = append(s, strings.TrimSpace(elem.CaseNumber))
}
// fmt.Printf("%v\n",s)
caseString := strings.Join(s[:], ", ")
if data.TotalSize > 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",
"Summary":"Cases",
"body":[
{
"type": "TextBlock",
"text": "Managed Server Case",
"size": "Large",
"weight": "Bolder",
"wrap": true
},
{
"type": "TextBlock",
"text": "Case: ` + caseString + ` ",
"size": "medium"
}
]
}
}
]
}`)
log.Printf("Sent Notification")
// MS Test Bot Room
request, err := http.NewRequest("POST", "Microsoft WEBHOOK URL", bytes.NewBuffer(payload))
// Test room
if err != nil {
log.Fatalln(err)
log.Fatalln("Error1")
}
request.Header.Set("Content-Type", "application/json")
response, error := client.Do(request)
if error != nil {
log.Fatalln(error)
}
body, _ := ioutil.ReadAll(response.Body)
log.Println(string(body))
defer response.Body.Close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment