-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (117 loc) · 3.83 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"fmt"
"log"
"os"
"strings"
"strconv"
"context"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
"github.com/JohannesKaufmann/html-to-markdown/escape"
)
var (
icons = map[string] string {
"success": "✅",
"failure": "🔴",
"cancelled": "❌",
"info": "🔔",
}
// Init zero value for ChatID
chat_id = 0
// Arguments list
token = os.Getenv("INPUT_TOKEN") // required
chat = os.Getenv("INPUT_CHAT") // required
status = os.Getenv("INPUT_STATUS") // recommended
title = os.Getenv("INPUT_TITLE") // optional
message = os.Getenv("INPUT_MESSAGE") // optional
footer = os.Getenv("INPUT_FOOTER") // optional
// Github enviroment variables
actor = os.Getenv("GITHUB_ACTOR") // who's made a commit
server = os.Getenv("GITHUB_SERVER_URL") // git-server
workflow = os.Getenv("GITHUB_WORKFLOW") // workflow name
repo = os.Getenv("GITHUB_REPOSITORY") // repository name
commit = os.Getenv("GITHUB_SHA") // commit message
// Format template for title
fmt_title = "%s %s *%s*" // icon, message, status
)
func main() {
log.Printf("🚀 Starting Telegram Notify...")
s := false
if token == "" {
log.Printf(" ⚡ Token is required!")
s = true
}
if chat == "" {
log.Printf(" ⚡ Chat ID is required!")
s = true
}
if s == true {
fatal("Notification was interrupted!")
}
// Prepare ChatID numeric value (int64)
chat_id, err := strconv.ParseInt(chat, 10, 64)
if err != nil {
fatal(err)
}
// Make status icon text
log.Printf(" - Check status message and create icon")
if status == "" {
warning("Status is not given! Set to 'info'...")
status = "info"
} else if status != "success" && status != "failure" && status != "cancelled" {
warning(fmt.Sprintf("Invalid status %v! Set to 'info'...", status))
status = "info"
}
icon := icons[strings.ToLower(status)]
workflow = escape.MarkdownCharacters(workflow)
title = escape.MarkdownCharacters(title)
message = escape.MarkdownCharacters(message)
footer = escape.MarkdownCharacters(footer)
// Make title text
m_title := ""
log.Printf(" - Create notification title")
if title == "" {
m_title = fmt.Sprintf(fmt_title, icon, workflow, status)
} else {
m_title = fmt.Sprintf(fmt_title, icon, title, status)
}
msg := fmt.Sprintf("%s", m_title)
// Append message text
log.Printf(" - Prepare notification message")
if message != "" {
msg = fmt.Sprintf("%v\n%v", msg, message)
} else {
warning("No message given! Using default notification message...")
m_commit := fmt.Sprintf("💬 Commit: [%v](%v/%v/commit/%v)", "open link", server, repo, commit)
msg = fmt.Sprintf("%v\n%v", msg, m_commit)
}
// Append footer text
if footer != "" {
msg = fmt.Sprintf("%v\n%v", msg, footer)
}
// Sending notification message
log.Printf("📢 Sending notification message...")
opts := []bot.Option{}
b, err := bot.New(token, opts...)
if err != nil {
fatal(err)
}
_, err = b.SendMessage(context.TODO(), &bot.SendMessageParams{
ChatID: chat_id,
Text: msg,
ParseMode: models.ParseModeMarkdown,
})
if err != nil {
fatal(err)
}
log.Printf("✅ Success!")
}
// Output fatal message and terminate
func fatal(msg any) {
log.Fatal(fmt.Sprintf(`❗ Fatal: %s`, msg))
}
// Output warning message
func warning(msg any) {
log.Printf(fmt.Sprintf(`⚠️ Warning: %s`, msg))
}