-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
194 lines (159 loc) · 4.44 KB
/
bot.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"fmt"
"log"
"time"
swissknife "github.com/Sagleft/swiss-knife"
"github.com/Sagleft/uchatbot-engine"
"github.com/Sagleft/utopialib-go/v2/pkg/structs"
"github.com/fatih/color"
tb "gopkg.in/telebot.v3"
)
const (
appName = "bridge"
configFilePath = "config.json"
defaultAccountName = "account.db"
botNickname = "uBridge"
donateAddress = "F50AF5410B1F3F4297043F0E046F205BCBAA76BEC70E936EB0F3AB94BF316804"
welcomeMsg = "Hello. I'm just a bot that transfers messages between messengers.\n\n" +
"By the way, subscribe to my developer channel?\n" +
"7A9F4A0B5B99B61F45E1652560DCF12C"
longPollerInterval = 15 * time.Second
)
func newBot(cfg config) *bot {
b := &bot{
Bridges: bridges{
UtopiaToTelegram: make(map[string]int64),
TelegramToUtopia: make(map[int64]string),
},
}
for _, r := range cfg.Bridges {
log.Printf("init bridge U %q <-> T %v", r.UtopiaChannelID, r.TelegramChatID)
b.Bridges.UtopiaToTelegram[r.UtopiaChannelID] = r.TelegramChatID
b.Bridges.TelegramToUtopia[r.TelegramChatID] = r.UtopiaChannelID
}
return b
}
func (b *bot) setChatBot(cb *uchatbot.ChatBot) {
b.ChatBot = cb
}
func (b *bot) setTelegramBot(tgBot *tb.Bot) {
b.TgBot = tgBot
}
func main() {
swissknife.PrintIntroMessage(appName, donateAddress)
cfg := config{}
if err := swissknife.ParseStructFromJSONFile(configFilePath, &cfg); err != nil {
color.Red("read config: %s", err.Error())
return
}
b := newBot(cfg)
// setup utopia bot
chats := []uchatbot.Chat{}
for _, r := range cfg.Bridges {
chats = append(chats, uchatbot.Chat{ID: r.UtopiaChannelID})
}
cb, err := uchatbot.NewChatBot(uchatbot.ChatBotData{
Config: cfg.Messengers.Utopia,
Chats: chats,
Callbacks: uchatbot.ChatBotCallbacks{
OnContactMessage: onContactMessage,
OnChannelMessage: b.onUtopiaChannelMessage,
OnPrivateChannelMessage: onPrivateChannelMessage,
WelcomeMessage: getWelcomeMessage,
},
UseErrorCallback: true,
ErrorCallback: onError,
})
if err != nil {
log.Fatalln(err)
}
// setup telegram bot
tgBot, err := tb.NewBot(tb.Settings{
Token: cfg.Messengers.Telegram.BotToken,
Poller: getTgPoller(),
})
if err != nil {
log.Fatalf("create tg bot: %s", err.Error())
}
b.setChatBot(cb)
b.setTelegramBot(tgBot)
if err := b.fixAccountName(); err != nil {
log.Fatalf("fix nickname: %s", err.Error())
}
tgBot.Handle(tb.OnText, b.onTelegramMessage)
go tgBot.Start()
log.Println("bot started")
swissknife.RunInBackground()
}
func getWelcomeMessage(_ string) string {
return welcomeMsg
}
func (b *bot) getTelegramBridge(chatID int64) (string, bool) {
br, isExists := b.Bridges.TelegramToUtopia[chatID]
return br, isExists
}
func (b *bot) getUtopiaBridge(channelID string) (int64, bool) {
br, isExists := b.Bridges.UtopiaToTelegram[channelID]
return br, isExists
}
func (b *bot) onTelegramMessage(c tb.Context) error {
var (
user = c.Sender()
text = c.Text()
chatID = c.Chat().ID
)
text = basicAntispam(text)
if text == "" {
return nil
}
uChannelID, isExists := b.getTelegramBridge(chatID)
if !isExists {
log.Printf("unknown telegram chat ID %v, bridge not found", chatID)
return nil
}
nickname := getTelegramNickname(user)
if err := b.sendToUtopia(uChannelID, nickname, text); err != nil {
return fmt.Errorf("send message to utopia: %w", err)
}
return nil
}
func (b *bot) onUtopiaChannelMessage(m structs.WsChannelMessage) {
chatID, isExists := b.getUtopiaBridge(m.ChannelID)
if !isExists {
log.Printf("unknown utopia channel ID %v, bridge not found", chatID)
return
}
message := basicAntispam(m.Text)
if message == "" {
return
}
if err := b.sendToTelegram(chatID, m.Nick, message); err != nil {
color.Red("send message to telegram: %s", err.Error())
}
}
func (b *bot) sendToTelegram(chatID int64, nickname string, message string) error {
_, err := b.TgBot.Send(
tb.ChatID(chatID),
fmt.Sprintf("%s: %s", nickname, message),
)
return err
}
func (b *bot) sendToUtopia(channelID string, nickname string, message string) error {
return b.ChatBot.SendChannelMessage(
channelID,
fmt.Sprintf("%s: %s", nickname, message),
)
}
func (b *bot) fixAccountName() error {
data, err := b.ChatBot.GetOwnContact()
if err != nil {
return fmt.Errorf("get own contact: %w", err)
}
if data.Nick == defaultAccountName {
if err := b.ChatBot.SetAccountNickname(botNickname); err != nil {
return fmt.Errorf("set account nickname: %w", err)
}
}
return nil
}