-
Notifications
You must be signed in to change notification settings - Fork 1
/
facebook.go
61 lines (51 loc) · 1.28 KB
/
facebook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package external
import (
"bytes"
"encoding/json"
"fmt"
"github.com/bitesinbyte/ferret/pkg/config"
"io"
"net/http"
"os"
)
const FacebookCreatePostUrl = "https://graph.facebook.com/v19.0/%s/feed"
type Facebook struct {
}
type facebookCreatePost struct {
Message string `json:"message"`
Link string `json:"link"`
AccessToken string `json:"access_token"`
}
func (m Facebook) Post(configData config.Config, post Post) error {
var accessToken = os.Getenv("FACEBOOK_ACCESS_TOKEN")
var url = fmt.Sprintf(FacebookCreatePostUrl, os.Getenv("FACEBOOK_PAGE_ID"))
content := facebookCreatePost{
Message: fmt.Sprintf("Just posted a new blog \n%s\n%s", post.Title, post.HashTags),
AccessToken: accessToken,
Link: post.Link,
}
reqBody, err := json.Marshal(content)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(reqBody))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
panic(err)
}
}(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return nil
}