hugo-micropub/microblogpub.go

32 lines
707 B
Go
Raw Normal View History

2019-12-12 21:36:05 +00:00
package main
import (
"bytes"
"encoding/json"
"net/http"
)
func PostToMicroblog(location string, text string) {
if len(MicroblogUrl) == 0 || len(MicroblogToken) == 0 {
// Microblog.pub deactivated
return
}
if len(text) == 0 {
text = location
}
note := &struct {
Content string `json:"content"`
}{
Content: "[" + text + "](" + location + ")",
}
bytesRepresentation, err := json.Marshal(note)
if err != nil {
return
}
client := &http.Client{}
req, _ := http.NewRequest("POST", MicroblogUrl+"api/new_note", bytes.NewBuffer(bytesRepresentation))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+MicroblogToken)
_, _ = client.Do(req)
}