-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_telegram.go
71 lines (60 loc) · 1.32 KB
/
send_telegram.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
package main
import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"log"
"os"
)
const (
parseMode = "html"
)
func SendTelegramMessage() {
cfg, err := ProcessConfigParameters(confFile)
if err != nil {
log.Fatal("Unable to ProcessConfigParameters ", err)
}
// prepare data for Telegram
result, err := FormatTelegramMessage()
if err != nil {
log.Panic(err)
}
// init TgBot
bot, err := tgbotapi.NewBotAPI(cfg.Telegram.Token)
if err != nil {
log.Panic(err)
}
// debug mode
if cfg.Telegram.Debug {
bot.Debug = true
log.Print("FormatTelegramMessage: result ", result)
defer log.Printf("Authorized on account: %s", bot.Self.UserName)
defer log.Printf("Message send from: %s ", bot.Self.FirstName)
// write env to log
x, err := os.Create("debug.log")
if err != nil {
log.Println(err)
}
for _, env := range os.Environ() {
_, err := x.WriteString(env + "\n")
if err != nil {
log.Println(err)
err = x.Close()
if err != nil {
log.Fatal(err)
}
return
}
}
}
// if there is empty messages - dont try to send it
if len(result) == 0 {
log.Println("No messages to send")
os.Exit(0)
}
// send message to Telegram
msg := tgbotapi.NewMessage(cfg.Telegram.ChatID, result)
msg.ParseMode = parseMode
_, err = bot.Send(msg)
if err != nil {
log.Fatal(err)
}
}