-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
88 lines (74 loc) · 1.92 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
package main
import (
"log"
"sync"
"time"
config "github.com/ad/corpobot/config"
database "github.com/ad/corpobot/db"
"github.com/ad/corpobot/plugins"
_ "github.com/ad/corpobot/plugins/admin"
_ "github.com/ad/corpobot/plugins/echo"
_ "github.com/ad/corpobot/plugins/groupchats"
_ "github.com/ad/corpobot/plugins/groups"
_ "github.com/ad/corpobot/plugins/me"
_ "github.com/ad/corpobot/plugins/messages"
_ "github.com/ad/corpobot/plugins/starthelp"
_ "github.com/ad/corpobot/plugins/users"
telegram "github.com/ad/corpobot/telegram"
dlog "github.com/amoghe/distillog"
sql "github.com/lazada/sqle"
tgbotapi "gopkg.in/telegram-bot-api.v4"
)
const version = "0.3.1"
var (
err error
bot *tgbotapi.BotAPI
db *sql.DB
)
func main() {
dlog.Infof("Started version %s", version)
// Init Config
config := config.InitConfig()
plugins.Config = config
log.SetFlags(0)
// Init DB
db, err = database.InitDB()
if err != nil {
log.Fatalf("failed to open database: %v", err)
return
}
defer func() { _ = db.Close() }()
plugins.DB = db
// Init Telegram
bot, err = telegram.InitTelegram(config.TelegramToken, config.TelegramProxyHost, config.TelegramProxyPort, config.TelegramProxyUser, config.TelegramProxyPassword, config.TelegramDebug)
if err != nil {
log.Printf("fail on telegram login: %v", err)
return
}
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
log.Printf("[INIT] [Failed to init Telegram updates chan: %v]", err)
return
}
dlog.Debugln("Waiting for plugins...")
for {
if plugins.DB != nil && plugins.DB.Ping() == nil {
var wg sync.WaitGroup
// Bootstrapper for plugins
plugins.Plugins.Range(func(k, v interface{}) bool {
wg.Add(1)
go func() {
defer wg.Done()
v.(plugins.TelegramPlugin).OnStart()
}()
return true
})
wg.Wait()
break
}
time.Sleep(time.Second)
}
telegram.ProcessTelegramMessages(db, bot, updates)
}