-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
82 lines (68 loc) · 1.4 KB
/
app.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
package main
import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/taras-by/tbot/store"
tlg "github.com/taras-by/tbot/telegram"
"log"
"runtime"
)
type options struct {
TelegramToken string
StorePath string
}
type app struct {
options options
commit string
date string
version string
storage *store.Storage
}
func newApp() (a *app) {
a = &app{
options: Opts,
commit: Commit,
date: Date,
version: Version,
}
a.printVersion()
var err error
a.storage, err = store.NewStorage(a.options.StorePath)
if err != nil {
log.Printf("storage creating error. Path: %s", a.options.StorePath)
log.Panic(err.Error())
}
return a
}
func (a *app) makeBotService() (s *tlg.BotService) {
bot, err := tgbotapi.NewBotAPI(a.options.TelegramToken)
if err != nil {
log.Printf("Telegram connection Error. Token: %s", a.options.TelegramToken)
log.Panic(err.Error())
}
log.Printf("Authorized on account %s", bot.Self.UserName)
handler := &tlg.MessageHandler{
Bot: bot,
Storage: a.storage,
Version: a.version,
}
service := tlg.BotService{
Bot: bot,
Handler: handler,
}
service.Init()
return &service
}
func (a *app) printVersion() {
fmt.Printf("Version: %s\nCommit: %s\nRuntime: %s %s/%s\nDate: %s\n",
a.version,
a.commit,
runtime.Version(),
runtime.GOOS,
runtime.GOARCH,
a.date,
)
}
func (a *app) Close() () {
a.storage.Close()
}