Skip to content

Instantly share code, notes, and snippets.

@afro-coder
Created Oct 25, 2021
Embed
What would you like to do?
Gist to read data from Salesforce and push to Microsoft Teams
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