-
Notifications
You must be signed in to change notification settings - Fork 0
/
DailyFrenchNewsText.go
83 lines (61 loc) · 1.97 KB
/
DailyFrenchNewsText.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"strings"
"net/http"
"net/url"
"math/rand"
"time"
"encoding/json"
"io/ioutil"
"github.com/jasonlvhit/gocron"
)
func main() {
s := gocron.NewScheduler()
s.Every(1).Day().Do(task)
<- s.Start()
}
func task() {
// urrl here b/c url used for twilio portion, need to be different
urrl := "https://newsapi.org/v2/top-headlines?apiKey=APIKEY&language=fr"
reqq, _ := http.NewRequest("GET", urrl, nil)
reqq.Header.Add("cache-control", "no-cache")
reqq.Header.Add("Postman-Token", "dcc8010d-6bc7-4112-93bb-98eae13fcdb2")
res, _ := http.DefaultClient.Do(reqq)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(res)
// fmt.Println(string(body))
var x string
x = (string(body))
var y string
y = x[56:1600]
//100 - 1650 works, 1550 (maybe 1600) appears to be the character limit for Twilio texts
fmt.Println(y)
// https://www.twilio.com/blog/2017/09/send-text-messages-golang.html
accountSid := "ACCOUNTSID"
authToken := "AUTHTOKEN"
urlStr := "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/Messages.json"
rand.Seed(time.Now().Unix())
varieble := url.Values{}
varieble.Set("To","+1234567890")
varieble.Set("From","+1234567890")
varieble.Set("Body",(string(y)))
msgDataReader := *strings.NewReader(varieble.Encode())
client := &http.Client{}
req, _ := http.NewRequest("POST", urlStr, &msgDataReader)
req.SetBasicAuth(accountSid, authToken)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, _ := client.Do(req)
if (resp.StatusCode >= 200 && resp.StatusCode < 300) {
var data map[string]interface{}
decoder := json.NewDecoder(resp.Body)
err := decoder.Decode(&data)
if (err == nil) {
fmt.Println(data["sid"])
}
} else {
fmt.Println(resp.Status);
}
}