-
Notifications
You must be signed in to change notification settings - Fork 1
/
telegram.go
138 lines (118 loc) · 3.86 KB
/
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
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
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"gopkg.in/yaml.v3"
)
const (
tgPrefix = "[Telegram]\t"
)
// newTelegramBot creates a Telegram bot using the Telegram API
func (tgConfig *Telegram) newTelegramBot() *tgbotapi.BotAPI {
bot, err := tgbotapi.NewBotAPI(tgConfig.BotToken)
if err != nil {
log.Fatal(err)
}
bot.Debug = false
log.Printf("%s authorized on account %s", tgPrefix, bot.Self.UserName)
return bot
}
// initMessage sets a messageId in the config it it's not yet present
// it does so by sending a message to the chat and pinning it afterwards
func (config *Config) initMessage(bot *tgbotapi.BotAPI, configPath string) {
// check if we already have a messageId specified
if config.Telegram.MessageId == 0 {
log.Printf("no messageId specified in config")
// send message
initChattable := tgbotapi.MessageConfig{
BaseChat: tgbotapi.BaseChat{
ChatID: config.Telegram.ChatId,
DisableNotification: true,
},
Text: "init",
}
initMsg, err := bot.Send(initChattable)
if err != nil {
log.Fatal(err)
}
log.Printf("%s sent init message", tgPrefix)
// save messageId to config
config.Telegram.MessageId = initMsg.MessageID
yamlData, err := yaml.Marshal(config)
if err != nil {
log.Fatal(err)
}
err = os.WriteFile(configPath, yamlData, 0644)
if err != nil {
log.Printf("%s unable to save messageId %d to config, %s", tgPrefix, config.Telegram.MessageId, err)
} else {
log.Printf("%s saved messageId %d to config", tgPrefix, config.Telegram.MessageId)
}
// pin message
pinConfig := tgbotapi.PinChatMessageConfig{
ChatID: config.Telegram.ChatId,
ChannelUsername: "",
MessageID: config.Telegram.MessageId,
DisableNotification: true,
}
bot.Send(pinConfig)
log.Printf("%s pinned message", tgPrefix)
}
}
// updateMessage edits the message to display the currently online users
func (tgConfig *Telegram) updateMessage(bot *tgbotapi.BotAPI, onlineUsers []string) {
content := strings.Join(onlineUsers, tgConfig.Separator)
if len(onlineUsers) == 0 {
content = tgConfig.ZeroUsers
}
edit := tgbotapi.NewEditMessageText(tgConfig.ChatId, tgConfig.MessageId, content)
message, err := bot.Send(edit)
if err != nil {
// don't log expected error
if strings.Contains(err.Error(), "exactly the same") {
return
}
log.Printf("%s unable to update message, %s", tgPrefix, err)
return
}
log.Printf("%s updated message with online users: [%s]", tgPrefix, content)
if tgConfig.UpdateTitle {
tgConfig.updateTitle(bot, message.Chat.Title, len(onlineUsers))
}
}
// updateTitle prepends the chat title with the current amount of online users
func (tgConfig *Telegram) updateTitle(bot *tgbotapi.BotAPI, originalTitle string, userAmount int) {
// remove online users from original title
titleFields := strings.Fields(originalTitle)
if _, err := strconv.Atoi(titleFields[0]); err == nil {
originalTitle = strings.Join(titleFields[1:], " ")
}
newTitle := originalTitle
if userAmount != 0 {
newTitle = fmt.Sprintf("%d %s", userAmount, originalTitle)
}
params := tgbotapi.Params{}
params.AddFirstValid("chat_id", tgConfig.ChatId)
params.AddBool("disable_notification", true)
params["title"] = newTitle
_, err := bot.MakeRequest("setChatTitle", params)
if err != nil {
log.Printf("%s unable to update chat title, %s", tgPrefix, err)
}
updates, err := bot.GetUpdates(tgbotapi.UpdateConfig{})
if err != nil {
log.Printf("%s unable to receive updates, %s", tgPrefix, err)
}
update := updates[len(updates)-1]
if update.Message.From.UserName == bot.Self.UserName {
deleteChattable := tgbotapi.NewDeleteMessage(tgConfig.ChatId, update.Message.MessageID)
_, err = bot.Request(deleteChattable)
if err != nil {
log.Printf("%s unable to delete chat title update message, %s", tgPrefix, err)
}
}
}