-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram_sender.go
58 lines (49 loc) · 1.11 KB
/
telegram_sender.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
package mail
import (
"sync"
alog "github.com/apex/log"
"gitlab.com/toby3d/telegram"
)
type TelegramSubSender struct {
AccessToken string
SendDebugInfo bool
botKey string
initBot sync.Once
log *alog.Entry
bot *telegram.Bot
botInitErr error
}
func (tg *TelegramSubSender) Send(msg Message, recipient string) (id string, err error) {
if len(tg.AccessToken) == 0 {
return "", NewCredReqErr("AccessToken", "TelegramSub")
}
tg.initBot.Do(tg.warmup)
msgID := tgGenMsgID(msg)
if tg.botInitErr != nil {
tg.initBot = sync.Once{}
return "", tg.botInitErr
}
tgRegister.RLock()
botReg := tgRegister.bots[tg.botKey]
tgRegister.RUnlock()
botReg.RLock()
subs, ok := botReg.chans[recipient]
botReg.RUnlock()
if !ok {
return "", NewNoSuchRecipientErr(recipient, "TelegramSub")
}
msgSrc := msg.Source()
if tg.SendDebugInfo {
msgSrc += "\n\n"
msgSrc += "**UUID:** " + msgID
}
for _, chatID := range subs {
tgMsg := &telegram.SendMessageParameters{
ParseMode: "Markdown",
ChatID: chatID,
Text: msgSrc,
}
tg.sendMsg(tgMsg)
}
return msgID, nil
}