Skip to content

Commit

Permalink
Improve several parts.
Browse files Browse the repository at this point in the history
- use go modele
- dont use attachments
- refactor fat main.go
- refactor fat main.go
- add Blocks at slack-go/slack/webhooks.go
  • Loading branch information
hokita committed Apr 11, 2020
1 parent 5b1012f commit b2ce8bc
Show file tree
Hide file tree
Showing 117 changed files with 14,401 additions and 48 deletions.
14 changes: 14 additions & 0 deletions article.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

// Ranking structure
type Ranking struct {
Entries []Article `json:"entries"`
}

// Article structure
type Article struct {
Title string `json:"title"`
URL string `json:"url"`
Categories string `json:"categories"`
Image string `json:"image"`
}
34 changes: 34 additions & 0 deletions article_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"encoding/json"
"io/ioutil"
"net/http"
)

// ArticleRepository class
type ArticleRepository struct {
}

// FindRanking func
func (a ArticleRepository) FindRanking() ([]Article, error) {
resp, err := http.Get(endpoint)

if err != nil {
return nil, err
}

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var ranking Ranking
if err := json.Unmarshal(body, &ranking); err != nil {
return nil, err
}

return ranking.Entries, nil
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module article_slack

go 1.13

require github.com/slack-go/slack v0.6.3
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/gorilla/websocket v1.2.0 h1:VJtLvh6VQym50czpZzx07z/kw9EgAxI3x1ZB8taTMQQ=
github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/nlopes/slack v0.6.0 h1:jt0jxVQGhssx1Ib7naAOZEZcGdtIhTzkP0nopK0AsRA=
github.com/nlopes/slack v0.6.0/go.mod h1:JzQ9m3PMAqcpeCam7UaHSuBuupz7CmpjehYMayT6YOk=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/slack-go/slack v0.6.3 h1:qU037g8gQ71EuH6S9zYKnvYrEUj0fLFH4HFekFqBoRU=
github.com/slack-go/slack v0.6.3/go.mod h1:HE4RwNe7YpOg/F0vqo5PwXH3Hki31TplTvKRW9dGGaw=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
59 changes: 11 additions & 48 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package main

import (
"encoding/json"
"fmt"
"github.com/nlopes/slack"
"io/ioutil"
"net/http"
"strconv"

"github.com/slack-go/slack"
)

const (
Expand All @@ -15,21 +13,9 @@ const (
rankCount = 10
)

// Ranking structure
type Ranking struct {
Entries []Article `json:"entries"`
}

// Article structure
type Article struct {
Title string `json:"title"`
URL string `json:"url"`
Categories string `json:"categories"`
Image string `json:"image"`
}

func main() {
articles, err := getArticles()
repo := ArticleRepository{}
articles, err := repo.FindRanking()
if err != nil {
fmt.Println(err)
return
Expand All @@ -54,11 +40,9 @@ func postArticle(articles []Article, count int) error {
// postSlack function
func postSlack(article Article, rank int) error {
payload := &slack.WebhookMessage{
Attachments: []slack.Attachment{
{
Blocks: []slack.Block{
getArticleSectionBlock(article, rank),
},
Blocks: slack.Blocks{
BlockSet: []slack.Block{
getArticleSectionBlock(article, rank),
},
},
}
Expand All @@ -74,9 +58,11 @@ func postSlack(article Article, rank int) error {
// getArticleSectionBlock function
func getArticleSectionBlock(article Article, rank int) slack.Block {
rankText := "rank: " + strconv.Itoa(rank)
titleText := "*" + article.Title + "*"
title := fmt.Sprintf(
"<%s|%s>", article.URL, article.Title,
)
categoryText := "category: " + article.Categories
text := rankText + "\n" + titleText + "\n" + categoryText + "\n" + article.URL
text := rankText + "\n" + title + "\n" + categoryText
textBlockObject := slack.NewTextBlockObject(
"mrkdwn",
text,
Expand All @@ -91,26 +77,3 @@ func getArticleSectionBlock(article Article, rank int) slack.Block {
)
return section
}

// get article data from api
func getArticles() ([]Article, error) {
resp, err := http.Get(endpoint)

if err != nil {
return nil, err
}

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var ranking Ranking
if err := json.Unmarshal(body, &ranking); err != nil {
return nil, err
}

return ranking.Entries, nil
}
25 changes: 25 additions & 0 deletions vendor/github.com/gorilla/websocket/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/gorilla/websocket/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/github.com/gorilla/websocket/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/gorilla/websocket/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions vendor/github.com/gorilla/websocket/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b2ce8bc

Please sign in to comment.