From a38926062fb3e5adcd1d766813907f25064a3a0f Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 8 Sep 2020 20:20:52 -0700 Subject: [PATCH 1/4] rough weather fetcher --- bot/bot.go | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/bot/bot.go b/bot/bot.go index 0c3504e..ea0333c 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -4,6 +4,7 @@ package bot import ( "fmt" "github.com/mattermost/mattermost-server/v5/model" + "net/http" "os" "os/signal" "regexp" @@ -71,7 +72,7 @@ func (b *Bot) Main() int { // Lets create a bot channel for logging debug messages into b.CreateBotDebuggingChannelIfNeeded() - msg := fmt.Sprintf("_**%s** has started up_\n\nrunning on version %s", b.BotName, b.Version) + msg := fmt.Sprintf("_**%s** (version `%s`) is now starting up", b.BotName, b.Version) b.SendMsgToDebuggingChannel(msg, "") // Lets start listening to some channels via the websocket! @@ -229,6 +230,38 @@ func (b *Bot) Shutdown() { syscall.Kill(syscall.Getpid(), syscall.SIGINT) } +func (b *Bot) HandleWeatherRequest(channelid string, postid string, message string) { + msg := fmt.Sprintf("weather request received: `%s`", message) + b.SendMsgToChannel(msg, postid, channelid) + + r := regexp.MustCompile(`metar\s+([A-Za-z]{4})`) + loc := r.FindString(message) + if loc == "" { + b.SendMsgToChannel("error, sorry", postid, channelid) + } + + token := os.Getenv("METAR_API_TOKEN") + url := fmt.Sprintf("https://avwx.rest/api/metar/%s?options=&airport=true&reporting=true&format=json&onfail=cache", loc) + + msg = fmt.Sprintf("calculated url: `%s`", url) + b.SendMsgToChannel(msg, postid, channelid) + + client := http.Client{ + Timeout: 5 * time.Second, + } + req, err := http.NewRequest("GET", url, nil) + req.Header.Add("Authorization", `Token `+token) + resp, err := client.Do(req) + + if err != nil { + b.SendMsgToChannel(fmt.Sprintf("weather fetch error: %s", err), postid, channelid) + return + } + + b.SendMsgToChannel(fmt.Sprintf("weather %s: %s", loc, resp), postid, channelid) + +} + func (b *Bot) HandleMsgFromChannel(event *model.WebSocketEvent) { post := model.PostFromJson(strings.NewReader(event.Data["post"].(string))) @@ -237,6 +270,10 @@ func (b *Bot) HandleMsgFromChannel(event *model.WebSocketEvent) { } //pretty.Print(post) + if matched, _ := regexp.MatchString(`(?:^|\W)metar(?:$|\W)`, post.Message); matched { + b.HandleWeatherRequest(post.ChannelId, post.Id, post.Message) + return + } if matched, _ := regexp.MatchString(`(?:^|\W)alive(?:$|\W)`, post.Message); matched { b.SendMsgToChannel("yes I'm running", post.Id, post.ChannelId) From 5b5e1f216762e96e6b6c2cb09b4b4cdc17bcffb5 Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 8 Sep 2020 20:23:26 -0700 Subject: [PATCH 2/4] add ci completion webhook --- .drone.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.drone.yml b/.drone.yml index 5dba997..5d5bd90 100644 --- a/.drone.yml +++ b/.drone.yml @@ -11,3 +11,7 @@ steps: tags: - ${DRONE_COMMIT_SHA} - ${DRONE_BRANCH} +- name: send + image: plugins/webhook + settings: + urls: https://chat.sneak.cloud/hooks/5ojqjwe4stgamx8in8q3rtxnca From 27fab7155c35a7a645d1bd6bbe4b9515ed0a1f06 Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 8 Sep 2020 20:32:16 -0700 Subject: [PATCH 3/4] big drone update --- .drone.yml | 62 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/.drone.yml b/.drone.yml index 5d5bd90..68c6a6d 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,8 +1,30 @@ kind: pipeline -name: default +name: notify-pipeline-start steps: -- name: docker +- name: slack + image: plugins/slack + settings: + webhook: + from_secret: SLACK_WEBHOOK + link_names: true + template: > + {{#if build.pull }} + *Build started*: {{ repo.owner }}/{{ repo.name }} - + {{else}} + *Build started: {{ repo.owner }}/{{ repo.name }} - Build #{{ build.number }}* (type: `{{ build.event }}`) + {{/if}} + Commit: + Branch: + Author: {{ build.author }} + <{{ build.link }}|Visit build page ↗> + +--- +kind: pipeline +name: test-docker-build + +steps: +- name: test-docker-build image: plugins/docker network_mode: bridge settings: @@ -11,7 +33,35 @@ steps: tags: - ${DRONE_COMMIT_SHA} - ${DRONE_BRANCH} -- name: send - image: plugins/webhook - settings: - urls: https://chat.sneak.cloud/hooks/5ojqjwe4stgamx8in8q3rtxnca + + +--- +kind: pipeline +name: notify-pipeline-end + +steps: + - name: slack + image: plugins/slack + settings: + webhook: + from_secret: SLACK_WEBHOOK + link_names: true + template: > + {{#if build.pull }} + *{{#success build.status}}✔{{ else }}✘{{/success}} {{ uppercasefirst build.status }}*: {{ repo.owner }}/{{ repo.name }} - + {{else}} + *{{#success build.status}}✔{{ else }}✘{{/success}} {{ uppercasefirst build.status }}: {{ repo.owner }}/{{ repo.name }} - Build #{{ build.number }}* (type: `{{ build.event }}`) + {{/if}} + Commit: + Branch: + Author: {{ build.author }} + Duration: {{ since build.created }} + <{{ build.link }}|Visit build page ↗> + +depends_on: + - test-docker-build + +trigger: + status: + - success + - failure From e55f1d3d976da8bac58c7de9a4e7db8e10e17f6f Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 8 Sep 2020 20:33:03 -0700 Subject: [PATCH 4/4] fix indentation --- .drone.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.drone.yml b/.drone.yml index 68c6a6d..4bd768f 100644 --- a/.drone.yml +++ b/.drone.yml @@ -3,8 +3,8 @@ name: notify-pipeline-start steps: - name: slack - image: plugins/slack - settings: + image: plugins/slack + settings: webhook: from_secret: SLACK_WEBHOOK link_names: true