-
Notifications
You must be signed in to change notification settings - Fork 0
/
webrequests.go
74 lines (54 loc) · 1.52 KB
/
webrequests.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
/*
Copyright © 2023 Patrick Hermann [email protected]
*/
package cli
import (
"io"
"log"
"net/http"
"os"
"path/filepath"
"github.com/schollz/progressbar/v3"
goteamsnotify "github.com/atc0005/go-teams-notify/v2"
"github.com/atc0005/go-teams-notify/v2/messagecard"
)
type MsTeamsWebhook struct {
Title string `yaml:"title"`
Text string `yaml:"text"`
Color string `yaml:"color"`
Url string `yaml:"url"`
}
func SendWebhookToTeams(webhook MsTeamsWebhook) bool {
// Initialize a new Microsoft Teams client.
mstClient := goteamsnotify.NewTeamsClient()
// Set webhook url.
webhookUrl := webhook.Url
// Setup message card.
msgCard := messagecard.NewMessageCard()
msgCard.Title = webhook.Title
msgCard.Text = webhook.Text
msgCard.ThemeColor = webhook.Color
// Send the message with default timeout/retry settings.
if err := mstClient.Send(webhookUrl, msgCard); err != nil {
log.Printf("failed to send message: %v", err)
os.Exit(1)
}
return true
}
func DownloadFileWithProgressBar(downloadUrl, targetDir string) {
req, _ := http.NewRequest("GET", downloadUrl, nil)
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
fileName := filepath.Base(downloadUrl)
f, _ := os.OpenFile(targetDir+"/"+fileName, os.O_CREATE|os.O_WRONLY, 0644)
defer f.Close()
bar := progressbar.DefaultBytes(
resp.ContentLength,
"downloading "+fileName,
)
io.Copy(io.MultiWriter(f, bar), resp.Body)
}
func CheckUrlAvailability(webURL string) bool {
r, e := http.Head(webURL)
return e == nil && r.StatusCode == 200
}