-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (101 loc) · 2.6 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
package main
import (
"errors"
"fmt"
"log"
"time"
swissknife "github.com/Sagleft/swiss-knife"
"github.com/Sagleft/uchatbot-engine"
utopiago "github.com/Sagleft/utopialib-go/v2"
"github.com/fatih/color"
"github.com/gin-gonic/gin"
"gopkg.in/tucnak/telebot.v2"
)
func main() {
app := newSolution()
err := checkErrors(
app.parseConfig,
app.initGin,
app.setupRoutes,
)
if err != nil {
log.Fatalln(err)
}
go app.runGin()
if err := app.connectMessengers(); err != nil {
app.setLastError(fmt.Errorf("connect messengers: %w", err))
}
swissknife.WaitForAppFinish()
}
func newSolution() *solution {
return &solution{}
}
func (sol *solution) setLastError(err error) {
sol.LastError = err
color.Red(err.Error())
}
func (sol *solution) initGin() error {
sol.Gin = gin.Default()
return nil
}
func (sol *solution) runGin() {
go sol.Gin.Run(":" + sol.Config.BindPort)
time.Sleep(time.Millisecond * 400)
url := "http://127.0.0.1:" + sol.Config.BindPort
if err := openBrowserURL(url); err != nil {
log.Fatalln(err)
}
}
func (sol *solution) connectMessengers() error {
if err := sol.connectUtopia(); err != nil {
return fmt.Errorf("connect Utopia: %w", err)
}
if err := sol.connectTelegram(); err != nil {
return fmt.Errorf("connect Telegram: %w", err)
}
return nil
}
func (sol *solution) connectUtopia() error {
if sol.Config.Utopia.Token == "" {
return errors.New("utopia token is not set in " + configJSONPath)
}
// create chatbot to handle auto-reconnect
chatBot, err := uchatbot.NewChatBot(uchatbot.ChatBotData{
Config: utopiago.Config{
Host: sol.Config.Utopia.Host,
Token: sol.Config.Utopia.Token,
Port: sol.Config.Utopia.Port,
WsPort: defaultWsPort,
Protocol: sol.Config.Utopia.Protocol,
},
Chats: []uchatbot.Chat{
{ID: sol.Config.Utopia.ChannelID},
},
DisableEvents: true,
UseErrorCallback: true,
ErrorCallback: sol.setLastError,
})
if err != nil {
return fmt.Errorf("setup Utopia: %w", err)
}
sol.setLastError(nil)
sol.Messengers.Utopia = chatBot.GetClient()
if !sol.Messengers.Utopia.CheckClientConnection() {
return errors.New("failed to connect to Utopia messenger")
}
return nil
}
func (sol *solution) connectTelegram() error {
if sol.Config.Telegram.Token == "" {
return errors.New("telegram token is not set in " + configJSONPath)
}
var err error
sol.Messengers.Telegram, err = telebot.NewBot(telebot.Settings{
Token: sol.Config.Telegram.Token,
Poller: &telebot.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
return errors.New("failed to connect to telegram bot: " + err.Error())
}
return nil
}