-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
105 lines (87 loc) · 2.67 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
package main
import (
"flag"
"log"
"github.com/elemc/gotelegrambot/db"
"github.com/elemc/gotelegrambot/httpserver"
"gopkg.in/telegram-bot-api.v4"
)
var (
bot *tgbotapi.BotAPI
settings Settings
)
func init() {
LoadConfig()
flag.StringVar(&settings.APIKey, "api-key", settings.APIKey, "API key for Telegram bot")
flag.StringVar(&settings.Addr, "addr", settings.Addr, "address string host:port for listen http server")
flag.StringVar(&settings.Couchbase.Cluster, "couch-cluster", settings.Couchbase.Cluster, "url to couchbase cluster")
flag.StringVar(&settings.Couchbase.Bucket, "couch-bucket", settings.Couchbase.Bucket, "couchbase bucket name")
flag.StringVar(&settings.Couchbase.Secret, "couch-secret", settings.Couchbase.Secret, "couchbase bucket password")
flag.StringVar(&settings.StaticDirPath, "static-dir-path", "static", "set path to static dir")
}
func main() {
flag.Parse()
//SaveConfig()
db.InitCouchbase(settings.Couchbase.Cluster, settings.Couchbase.Bucket, settings.Couchbase.Secret)
bot, err := tgbotapi.NewBotAPI(settings.APIKey)
if err != nil {
log.Fatalf("Cannot create bot api")
}
bot.Debug = false // true
log.Printf("Authorized on account %s", bot.Self.UserName)
// start http server
s := httpserver.Server{Addr: settings.Addr, Bot: bot}
s.PhotoCache = make(httpserver.PhotosCache)
s.FileCache = make(httpserver.FilesCache)
s.APIKey = settings.APIKey
s.StaticDirPath = settings.StaticDirPath
go s.FillCens()
go s.Start()
//s.Start()
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
log.Panic(err.Error())
}
for update := range updates {
if update.Message == nil {
continue
}
go db.GoSaveMessage(update.Message)
// Photo
id := int64(update.Message.From.ID)
if _, ok := s.PhotoCache[id]; !ok {
go s.GetPhoto(id)
}
// Files
if update.Message.Audio != nil {
go s.GetFile(update.Message.Audio.FileID, update.Message.Chat.ID)
}
if update.Message.Document != nil {
go s.GetFile(update.Message.Document.FileID, update.Message.Chat.ID)
}
if update.Message.Photo != nil {
for _, f := range *update.Message.Photo {
go s.GetFile(f.FileID, update.Message.Chat.ID)
}
}
if update.Message.Sticker != nil {
go s.GetFile(update.Message.Sticker.FileID, update.Message.Chat.ID)
}
if update.Message.Video != nil {
go s.GetFile(update.Message.Video.FileID, update.Message.Chat.ID)
}
if update.Message.Voice != nil {
go s.GetFile(update.Message.Voice.FileID, update.Message.Chat.ID)
}
// Commands
if update.Message.IsCommand() {
go s.CommandHandler(update.Message)
} else {
// Cens
// Disable cens
//go s.Cens(update.Message)
}
}
}