Skip to content

Commit

Permalink
feat: use slack webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
slowhigh committed Jun 8, 2024
1 parent a7a05b9 commit 5a736ef
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
3 changes: 3 additions & 0 deletions cmd/server/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"PORT": 5000,
"USER_NAME": "nerd",
"PASSWORD": "planet1!"
},
"WEBHOOK": {
"KEY": "key"
}
}
52 changes: 45 additions & 7 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"encoding/json"
"fmt"
"log"
"log/slog"
Expand All @@ -15,6 +16,7 @@ import (
"time"

"github.com/jasonlvhit/gocron"
"github.com/slack-go/slack"
"github.com/spf13/viper"
"github.com/team-nerd-planet/send-server/entity"
"gorm.io/driver/postgres"
Expand Down Expand Up @@ -73,15 +75,24 @@ func main() {
panic(err)
}

var sb strings.Builder
sb.WriteString("안녕하세요! plaa 입니다.\n너드플라넷의 소식을 아래와 같이 전달했어요!!\n\n")

for _, subscription := range subscriptionArr {
publish(conf, db, subscription)
count := publish(conf, db, subscription)

sb.WriteString(fmt.Sprintf("%s님에게 %d개의 리스트를 보냈어요.\n", subscription.Email, count))
}

if err := webhook(conf, sb.String()); err != nil {
panic(err)
}
})

<-gocron.Start()
}

func publish(conf *Config, db *gorm.DB, subscription entity.Subscription) {
func publish(conf *Config, db *gorm.DB, subscription entity.Subscription) int {
var (
items []entity.ItemView
where = make([]string, 0)
Expand Down Expand Up @@ -119,7 +130,7 @@ func publish(conf *Config, db *gorm.DB, subscription entity.Subscription) {
"feed_name",
).Where(strings.Join(where, " AND "), param...).Limit(10).Find(&items).Error; err != nil {
slog.Error(err.Error(), "error", err)
return
return 0
}

if len(items) > 0 {
Expand All @@ -145,13 +156,13 @@ func publish(conf *Config, db *gorm.DB, subscription entity.Subscription) {
t, err := template.ParseFiles(fmt.Sprintf("%s/template/newsletter.html", configDirPath))
if err != nil {
slog.Error(err.Error(), "error", err)
return
return 0
}

var body bytes.Buffer
if err := t.Execute(&body, data); err != nil {
slog.Error(err.Error(), "error", err)
return
return 0
}

auth := smtp.PlainAuth("", conf.Smtp.UserName, conf.Smtp.Password, conf.Smtp.Host)
Expand All @@ -163,15 +174,17 @@ func publish(conf *Config, db *gorm.DB, subscription entity.Subscription) {
err = smtp.SendMail(fmt.Sprintf("%s:%d", conf.Smtp.Host, conf.Smtp.Port), auth, from, to, msg)
if err != nil {
slog.Error(err.Error(), "error", err)
return
return 0
}
}

subscription.Published = time.Now()
if err := db.Save(subscription).Error; err != nil {
slog.Error(err.Error(), "error", err)
return
return 0
}

return len(items)
}

func getArrToString(arr []int64) string {
Expand All @@ -186,6 +199,7 @@ func getArrToString(arr []int64) string {
type Config struct {
Database Database `mapstructure:"DATABASE"`
Smtp Smtp `mapstructure:"SMTP"`
Webhook Webhook `mapstructure:"WEBHOOK"`
}

type Database struct {
Expand All @@ -204,6 +218,10 @@ type Smtp struct {
Password string `mapstructure:"PASSWORD"`
}

type Webhook struct {
Key string `mapstructure:"KEY"`
}

func NewConfig() (*Config, error) {
_, b, _, _ := runtime.Caller(0)
configDirPath := path.Join(path.Dir(b))
Expand All @@ -229,3 +247,23 @@ func NewConfig() (*Config, error) {

return &conf, nil
}

func webhook(conf *Config, text string) error {
attachment := slack.Attachment{
Color: "good",
Fallback: "You successfully posted by Incoming Webhook URL!",
AuthorName: "plaa",
AuthorSubname: "I live in Nerd Planet.",
AuthorLink: "https://www.nerdplanet.app",
AuthorIcon: "https://avatars.slack-edge.com/2024-06-08/7245446528738_95ffe7a911c7aced7f3c_512.png",
Text: text,
Footer: "plaa",
FooterIcon: "https://avatars.slack-edge.com/2024-06-08/7245446528738_95ffe7a911c7aced7f3c_512.png",
Ts: json.Number(strconv.FormatInt(time.Now().Unix(), 10)),
}
msg := slack.WebhookMessage{
Attachments: []slack.Attachment{attachment},
}

return slack.PostWebhook(conf.Webhook.Key, &msg)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ go 1.22.3
require (
github.com/jasonlvhit/gocron v0.0.1
github.com/lib/pq v1.10.9
github.com/slack-go/slack v0.13.0
github.com/spf13/viper v1.18.2
gorm.io/driver/postgres v1.5.7
gorm.io/gorm v1.25.10
)

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-redis/redis v6.15.5+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
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/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
Expand Down Expand Up @@ -50,6 +55,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/slack-go/slack v0.13.0 h1:7my/pR2ubZJ9912p9FtvALYpbt0cQPAqkRy2jaSI1PQ=
github.com/slack-go/slack v0.13.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
Expand All @@ -63,6 +70,7 @@ github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMV
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down Expand Up @@ -93,6 +101,7 @@ golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down

0 comments on commit 5a736ef

Please sign in to comment.