-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.go
249 lines (213 loc) · 5.83 KB
/
main.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"time"
"watgbridge/database"
"watgbridge/modules"
"watgbridge/state"
"watgbridge/telegram"
"watgbridge/utils"
"watgbridge/whatsapp"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/go-co-op/gocron"
"go.uber.org/zap"
)
func main() {
// Load configuration file
cfg := state.State.Config
cfg.SetDefaults()
if len(os.Args) > 1 {
cfg.Path = os.Args[1]
}
err := cfg.LoadConfig()
if err != nil {
panic(fmt.Errorf("failed to load config file: %s", err))
}
if cfg.Telegram.APIURL == "" {
cfg.Telegram.APIURL = gotgbot.DefaultAPIURL
}
if cfg.DebugMode {
developmentConfig := zap.NewDevelopmentConfig()
developmentConfig.OutputPaths = append(developmentConfig.OutputPaths, "debug.log")
state.State.Logger, err = developmentConfig.Build()
if err != nil {
panic(fmt.Errorf("failed to initialize development logger: %s", err))
}
state.State.Logger = state.State.Logger.Named("WaTgBridge_Dev")
} else {
productionConfig := zap.NewProductionConfig()
state.State.Logger, err = productionConfig.Build()
if err != nil {
panic(fmt.Errorf("failed to initialize production logger: %s", err))
}
state.State.Logger = state.State.Logger.Named("WaTgBridge")
}
logger := state.State.Logger
logger.Debug("loaded config file and started logger",
zap.String("config_path", cfg.Path),
zap.Bool("development_mode", cfg.DebugMode),
)
_ = logger.Sync()
// Create local location for time
if cfg.TimeZone == "" {
cfg.TimeZone = "UTC"
}
locLoc, err := time.LoadLocation(cfg.TimeZone)
if err != nil {
logger.Fatal("failed to set time zone",
zap.String("time_zone", cfg.TimeZone),
zap.Error(err),
)
}
state.State.LocalLocation = locLoc
if cfg.WhatsApp.SessionName == "" {
cfg.WhatsApp.SessionName = "watgbridge"
}
if cfg.WhatsApp.LoginDatabase.Type == "" || cfg.WhatsApp.LoginDatabase.URL == "" {
cfg.WhatsApp.LoginDatabase.Type = "sqlite3"
cfg.WhatsApp.LoginDatabase.URL = "file:wawebstore.db?foreign_keys=on"
logger.Debug("using sqlite3 as WhatsApp login database")
_ = logger.Sync()
}
if cfg.GitExecutable == "" {
gitPath, err := exec.LookPath("git")
if err != nil && !errors.Is(err, exec.ErrDot) {
logger.Fatal("failed to set git executable path",
zap.Error(err),
)
}
cfg.GitExecutable = gitPath
logger.Info("setting path to git executable",
zap.String("path", gitPath),
)
_ = logger.Sync()
if err = cfg.SaveConfig(); err != nil {
logger.Fatal("failed to save config file",
zap.Error(err),
)
}
}
if cfg.GoExecutable == "" {
goPath, err := exec.LookPath("go")
if err != nil && !errors.Is(err, exec.ErrDot) {
logger.Fatal("failed to set go executable path",
zap.Error(err),
)
}
cfg.GoExecutable = goPath
logger.Info("setting path to go executable",
zap.String("path", goPath),
)
_ = logger.Sync()
if err = cfg.SaveConfig(); err != nil {
logger.Fatal("failed to save config file",
zap.Error(err),
)
}
}
if cfg.FfmpegExecutable == "" && !cfg.Telegram.SkipVideoStickers {
ffmpegPath, err := exec.LookPath("ffmpeg")
if err != nil && !errors.Is(err, exec.ErrDot) {
logger.Fatal("failed to set ffmpeg executable path",
zap.Error(err),
)
}
cfg.FfmpegExecutable = ffmpegPath
logger.Info("setting path to ffmpeg executable",
zap.String("path", ffmpegPath),
)
_ = logger.Sync()
if err = cfg.SaveConfig(); err != nil {
logger.Fatal("failed to save config file",
zap.Error(err),
)
}
}
// Setup database
db, err := database.Connect()
if err != nil {
logger.Fatal("could not connect to database",
zap.Error(err),
)
}
state.State.Database = db
err = database.AutoMigrate()
if err != nil {
logger.Fatal("could not migrate database tabels",
zap.Error(err),
)
}
err = telegram.NewTelegramClient()
if err != nil {
logger.Fatal("failed to initialize telegram client",
zap.Error(err),
)
}
_ = logger.Sync()
err = whatsapp.NewWhatsAppClient()
if err != nil {
panic(err)
}
_ = logger.Sync()
state.State.StartTime = time.Now().UTC()
s := gocron.NewScheduler(time.UTC)
s.TagsUnique()
_, _ = s.Every(1).Hour().Tag("foo").Do(func() {
contacts, err := state.State.WhatsAppClient.Store.Contacts.GetAllContacts()
if err == nil {
_ = database.ContactNameBulkAddOrUpdate(contacts)
}
})
state.State.WhatsAppClient.AddEventHandler(whatsapp.WhatsAppEventHandler)
telegram.AddTelegramHandlers()
modules.LoadModuleHandlers()
if !cfg.Telegram.SkipSettingCommands {
err = utils.TgRegisterBotCommands(state.State.TelegramBot, state.State.TelegramCommands...)
if err != nil {
logger.Error("failed to set my commands",
zap.Error(err),
)
}
} else {
err = utils.TgRegisterBotCommands(state.State.TelegramBot)
if err != nil {
logger.Error("failed to set my commands to empty",
zap.Error(err),
)
}
}
_ = logger.Sync()
startMessageSuccessful := false
{
isRestarted, found := os.LookupEnv("WATG_IS_RESTARTED")
if !found || isRestarted != "1" {
goto SKIP_RESTART
}
chatIdString, chatIdFound := os.LookupEnv("WATG_CHAT_ID")
msgIdString, msgIdFound := os.LookupEnv("WATG_MESSAGE_ID")
if !chatIdFound || !msgIdFound {
goto SKIP_RESTART
}
chatId, chatIdSuccess := strconv.ParseInt(chatIdString, 10, 64)
msgId, msgIdSuccess := strconv.ParseInt(msgIdString, 10, 64)
if chatIdSuccess != nil || msgIdSuccess != nil {
goto SKIP_RESTART
}
opts := gotgbot.SendMessageOpts{
ReplyParameters: &gotgbot.ReplyParameters{
MessageId: msgId,
},
}
state.State.TelegramBot.SendMessage(chatId, "Successfully restarted", &opts)
startMessageSuccessful = true
}
SKIP_RESTART:
if !startMessageSuccessful && !cfg.Telegram.SkipStartupMessage {
state.State.TelegramBot.SendMessage(cfg.Telegram.OwnerID, "Successfully started WaTgBridge", &gotgbot.SendMessageOpts{})
}
state.State.TelegramUpdater.Idle()
}