-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
68 lines (56 loc) · 1.68 KB
/
Copy pathmain.go
File metadata and controls
68 lines (56 loc) · 1.68 KB
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
package main
import (
"fmt"
"os"
"regexp"
"github.com/andoniaf/telegram-pr-notify/pkg/events"
"github.com/andoniaf/telegram-pr-notify/pkg/telegram"
"github.com/andoniaf/telegram-pr-notify/pkg/templates"
)
var chatIDPattern = regexp.MustCompile(`^-?\d+$`)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "::error::%v\n", err)
os.Exit(1)
}
fmt.Println("Notification sent successfully")
}
func run() error {
botToken := os.Getenv("INPUT_BOT_TOKEN")
chatID := os.Getenv("INPUT_CHAT_ID")
topicID := os.Getenv("INPUT_TOPIC_ID")
customTemplate := os.Getenv("INPUT_CUSTOM_TEMPLATE")
eventPayload := os.Getenv("INPUT_EVENT_PAYLOAD")
if botToken != "" {
fmt.Fprintf(os.Stdout, "::add-mask::%s\n", botToken)
}
if botToken == "" {
return fmt.Errorf("bot_token is required")
}
if chatID == "" {
return fmt.Errorf("chat_id is required")
}
if !chatIDPattern.MatchString(chatID) {
return fmt.Errorf("chat_id must be a numeric value (e.g., -100123456789)")
}
if eventPayload == "" {
return fmt.Errorf("event_payload is required")
}
data, err := events.Parse([]byte(eventPayload))
if err != nil {
return fmt.Errorf("parsing event: %w", err)
}
message, err := templates.Render(data, customTemplate)
if err != nil {
return fmt.Errorf("rendering template: %w", err)
}
buttons := []telegram.Button{{Text: data.ButtonText(), URL: data.RelevantURL()}}
for _, issue := range data.LinkedIssues() {
buttons = append(buttons, telegram.Button{Text: issue.Text, URL: issue.URL})
}
client := telegram.NewClient(botToken, chatID, topicID)
if err := client.SendMessage(message, buttons); err != nil {
return fmt.Errorf("sending message: %w", err)
}
return nil
}