-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
108 lines (85 loc) · 2.49 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"time"
"github.com/dustin/go-humanize"
"github.com/gorilla/mux"
"github.com/schmatz/hn-messenger-bot/messenger"
)
var (
pageAccessToken = flag.String("page-access-token", "", "The page access token")
verificationToken = flag.String("token", "", "The challenge verification token")
port = flag.Uint("port", 3000, "The port to listen on")
bot *messenger.Bot
)
func main() {
flag.Parse()
if *pageAccessToken == "" || *verificationToken == "" {
flag.Usage()
log.Fatal("Page access token and verification token are required parameters")
}
bot = messenger.New(*pageAccessToken, *verificationToken, handleMessaging)
r := mux.NewRouter()
r.HandleFunc("/webhook/", bot.HandleVerificationChallenge).Methods("GET")
r.HandleFunc("/webhook/", bot.HandleWebhookPost).Methods("POST")
r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) })
http.ListenAndServe(fmt.Sprintf(":%d", *port), r)
}
func handleMessaging(m messenger.Messaging) (err error) {
topStories, err := getHNTopStoryIDs()
if err != nil {
return
}
var templateItems []messenger.GenericTemplateElement
numStories := 5
for i := 0; i < numStories; i++ {
topStory := topStories[i]
title, url, description, err := getHNStoryDetails(topStory)
if err != nil {
return err
}
item := messenger.GenericTemplateElement{
Title: title,
Subtitle: description,
ItemURL: url,
}
templateItems = append(templateItems, item)
}
err = bot.SendGenericTemplateReply(m.Sender.ID, templateItems)
return
}
func getHNTopStoryIDs() (ids []int64, err error) {
resp, err := http.Get("https://hacker-news.firebaseio.com/v0/topstories.json")
if err != nil {
return
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&ids)
return
}
func getHNStoryDetails(storyID int64) (title string, url string, description string, err error) {
var story struct {
Title string `json:"title"`
URL string `json:"url"`
Author string `json:"by"`
Points int64 `json:"score"`
Time int64 `json:"time"`
}
resp, err := http.Get(fmt.Sprintf("https://hacker-news.firebaseio.com/v0/item/%d.json", storyID))
if err != nil {
return
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&story)
if err != nil {
return
}
title = story.Title
url = story.URL
description = fmt.Sprintf("%d points by %s %s", story.Points, story.Author, humanize.Time(time.Unix(story.Time, 0)))
return
}