Skip to content

Commit

Permalink
init commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
makinje16 committed Oct 25, 2018
0 parents commit baec2ce
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
*.exe
assets/
124 changes: 124 additions & 0 deletions news.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"time"
)

// CHARACTER_LIMIT is the twilio api character limit
const CHARACTER_LIMIT = 1600

type headlines struct {
Author string
Title string
Description string
Url string
UrlToImage string
PublishedAt string
Content string
}

type IgnResponse struct {
Status string
TotalResults int
Articles []headlines
}

func main() {
setTimer()
}

func setTimer() {
t := time.Now()
n := time.Date(t.Year(), t.Month(), t.Day(), 12, 0, 0, 0, t.Location())
d := n.Sub(t)

if d < 0 {
n = n.Add(24 * time.Hour)
d = n.Sub(t)
}
for {
time.Sleep(d)
d = 24 * time.Hour
parseHeadlines()
}
}

func parseHeadlines() {
count := 0
var message string
newsAPIKey := os.Getenv("NEWS_API_KEY")
twilioNumber := os.Getenv("TWILIO_NUMBER")
receivingNumber := os.Args[1]

resp, err := http.Get("https://newsapi.org/v2/top-headlines?sources=ign&apiKey=" + newsAPIKey)

if err != nil {
panic(err)
}

defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
var ignResponse IgnResponse
err := json.Unmarshal(bodyBytes, &ignResponse)
if err == nil {
for _, articles := range ignResponse.Articles {
if len(message)+len(articles.Description+"\n"+articles.Url+" \n\n") > CHARACTER_LIMIT {
sendMessage(message, twilioNumber, receivingNumber)
message = articles.Description + "\n" + articles.Url + " \n\n"
count = 0
} else {
message += articles.Description + "\n" + articles.Url + " \n\n"
count = len(message)
}
}
}
}
if count > 0 {
sendMessage(message, twilioNumber, receivingNumber)
}
}

func sendMessage(body string, from string, to string) {

accountSid := os.Getenv("TWILIO_SID")
authToken := os.Getenv("TWILIO_API_KEY")

urlStr := "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/Messages.json"

msg := url.Values{}
msg.Set("To", to)
msg.Set("From", from)
msg.Set("Body", body)
msgDataReader := *strings.NewReader(msg.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)
reader, _ := ioutil.ReadAll(resp.Body)
print(string(reader))
}

}

0 comments on commit baec2ce

Please sign in to comment.