-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
130 lines (115 loc) · 3.17 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
// Package botmeans provides a framework for creation of complex high-loaded Telegram bots with rich behaviour
package botmeans
import (
"fmt"
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/jinzhu/gorm"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
//MeansBot is a body of botmeans framework instance.
type MeansBot struct {
bot *tgbotapi.BotAPI
db *gorm.DB
netConfig NetConfig
tlgConfig TelegramConfig
}
//NetConfig is a MeansBot network config for using with New function
type NetConfig struct {
ListenIP string
ListenPort int16
}
//TelegramConfig is a MeansBot telegram API config for using with New function
type TelegramConfig struct {
BotToken string
WebhookHost string
SSLCertFile string
BotName string
TemplateDir string
}
//New creates new MeansBot instance
func New(DB *gorm.DB, netConfig NetConfig, tlgConfig TelegramConfig) (*MeansBot, error) {
if DB == nil {
return &MeansBot{}, fmt.Errorf("No db connection given")
}
bot, err := tgbotapi.NewBotAPI(tlgConfig.BotToken)
if err != nil {
return &MeansBot{}, err
}
ret := &MeansBot{
bot: bot,
db: DB,
netConfig: netConfig,
tlgConfig: tlgConfig,
}
if os.Getenv("BOTMEANS_SET_WEBHOOK") == "TRUE" {
ret.bot.RemoveWebhook()
_, err = ret.bot.SetWebhook(tgbotapi.NewWebhookWithCert(fmt.Sprintf("https://%v:8443/%v", ret.tlgConfig.WebhookHost, ret.bot.Token),
ret.tlgConfig.SSLCertFile))
if err != nil {
return nil, err
} else {
log.Println("Webhook set")
}
}
SessionInitDB(DB)
BotMessageInitDB(DB)
return ret, nil
}
//Run starts updates handling. Returns stop chan
func (ui *MeansBot) Run(handlersProvider ActionHandlersProvider) chan interface{} {
templateDir := ui.tlgConfig.TemplateDir
botID, _ := strconv.ParseInt(strings.Split(ui.bot.Token, ":")[0], 10, 64)
sessionFactory := func(base SessionBase) (SessionInterface, error) {
return SessionLoader(base, ui.db, botID, ui.bot)
}
actionFactory := func(
sessionBase SessionBase,
sessionFactory SessionFactory,
getters actionExecuterFactoryConfig,
out chan Executer,
) {
ActionFactory(
sessionBase,
sessionFactory,
getters,
func(s senderSession) SenderInterface {
return &Sender{
session: s,
bot: ui.bot,
templateDir: templateDir,
msgFactory: func() BotMessageInterface { return NewBotMessage(s.ChatId(), ui.db) },
}
},
out,
handlersProvider,
)
}
aliaser := AliaserFromTemplateDir(templateDir)
argsParser := func(tgUpdate tgbotapi.Update) Args {
return ArgsParser(tgUpdate, sessionFactory, aliaser)
}
cmdParser := func(tgUpdate tgbotapi.Update) string {
return CmdParser(tgUpdate, aliaser)
}
botMsgFactory := func(chatID int64, msgId int64, callbackID string) BotMessageInterface {
return BotMessageDBLoader(chatID, msgId, callbackID, ui.db)
}
updatesChan := ui.bot.ListenForWebhook("/" + ui.bot.Token)
go http.ListenAndServe(fmt.Sprintf("%v:%v", ui.netConfig.ListenIP, ui.netConfig.ListenPort), nil)
actionsChan := createTGUpdatesParser(
updatesChan,
parserConfig{
sessionFactory,
actionFactory,
botMsgFactory,
cmdParser,
argsParser,
},
)
return RunMachine(actionsChan, time.Minute)
}