/
telegram.go
80 lines (60 loc) · 1.43 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
package telegram
import (
"context"
"io"
"log"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/copybird/copybird/core"
)
const GROUP_NAME = "global"
const TYPE_NAME = "notifier"
const MODULE_NAME = "telegram"
type GlobalNotifierTelegram struct {
core.Module
config *Config
reader io.Reader
writer io.Writer
}
func (m *GlobalNotifierTelegram) GetGroup() core.ModuleGroup {
return GROUP_NAME
}
func (m *GlobalNotifierTelegram) GetType() core.ModuleType {
return TYPE_NAME
}
func (m *GlobalNotifierTelegram) GetName() string {
return MODULE_NAME
}
func (m *GlobalNotifierTelegram) InitPipe(w io.Writer, r io.Reader) error {
m.reader = r
m.writer = w
return nil
}
func (m *GlobalNotifierTelegram) InitModule(_cfg interface{}) error {
m.config = _cfg.(*Config)
return nil
}
func (m *GlobalNotifierTelegram) Run(ctx context.Context) error {
if err := m.config.NotifyTelegramChannel(); err != nil {
return err
}
return nil
}
func (m *GlobalNotifierTelegram) GetConfig() interface{} {
return &Config{}
}
func (m *GlobalNotifierTelegram) Close() error {
return nil
}
func (conf *Config) NotifyTelegramChannel() error {
bot, err := tgbotapi.NewBotAPI(conf.Token)
if err != nil {
return err
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
msg := tgbotapi.NewMessage(conf.ChannelID, conf.Message)
bot.Send(msg)
return nil
}