Last active
December 19, 2019 15:24
-
-
Save tuhuynh27/ab4fa5223ae93b5b2f71b3e5b82da632 to your computer and use it in GitHub Desktop.
Trua nay an zi Slack - Run: brew install go && go run trua-nay-an-zi.go || go run trua-nay-an-zi.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"container/list" | |
"encoding/json" | |
"errors" | |
"log" | |
"math/rand" | |
"net/http" | |
"time" | |
) | |
// Set quan | |
var angi []string = []string{"Quan 1", "Quan 2", "Quan 3", "Quan 4", "Quan 5", "Quan 6", "Quan 7"} | |
// Set gio: 11:55:00 everyday | |
const hourToTick int = 11 | |
const minuteToTick int = 55 | |
const secondToTick int = 00 | |
const intervalPeriod time.Duration = 24 * time.Hour | |
// Link Slack webhook | |
const webhookURL string = "https://hooks.slack.com/services/X1234" | |
func main() { | |
queue := list.New() | |
initQueue(queue) | |
jt := newJobTicker() | |
for { | |
<-jt.t.C | |
pingEveryone(queue) | |
jt.updateJobTicker() | |
} | |
} | |
func pingEveryone(queue *list.List) { | |
quan := getFood(queue).Value.(string) | |
msg := "Di an trua thoi moi nguoi oi, an o quan " + quan + " nha!" | |
log.Println(msg) | |
err := sendSlackNotification(webhookURL, msg) | |
if err != nil { | |
log.Println(err) | |
} | |
} | |
type jobTicker struct { | |
t *time.Timer | |
} | |
func getNextTickDuration() time.Duration { | |
now := time.Now() | |
nextTick := time.Date(now.Year(), now.Month(), now.Day(), hourToTick, minuteToTick, secondToTick, 0, time.Local) | |
if nextTick.Before(now) { | |
nextTick = nextTick.Add(intervalPeriod) | |
} | |
return nextTick.Sub(time.Now()) | |
} | |
func newJobTicker() jobTicker { | |
log.Println("Dang chay trong nen ahihi...") | |
return jobTicker{time.NewTimer(getNextTickDuration())} | |
} | |
func (jt jobTicker) updateJobTicker() { | |
jt.t.Reset(getNextTickDuration()) | |
} | |
func initQueue(queue *list.List) { | |
r := rand.New(rand.NewSource(time.Now().Unix())) | |
for _, i := range r.Perm(len(angi)) { | |
queue.PushBack(angi[i]) | |
} | |
} | |
func getFood(queue *list.List) *list.Element { | |
if queue.Len() > 0 { | |
e := queue.Front() | |
queue.Remove(e) | |
return e | |
} | |
initQueue(queue) | |
return getFood(queue) | |
} | |
type slackRequestBody struct { | |
Text string `json:"text"` | |
} | |
func sendSlackNotification(webhookURL string, msg string) error { | |
slackBody, _ := json.Marshal(slackRequestBody{Text: msg}) | |
req, err := http.NewRequest(http.MethodPost, webhookURL, bytes.NewBuffer(slackBody)) | |
if err != nil { | |
return err | |
} | |
req.Header.Add("Content-Type", "application/json") | |
client := &http.Client{Timeout: 30 * time.Second} | |
resp, err := client.Do(req) | |
if err != nil { | |
return err | |
} | |
buf := new(bytes.Buffer) | |
buf.ReadFrom(resp.Body) | |
if buf.String() != "ok" { | |
return errors.New("Slack khong cho gui") | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment