-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
220 lines (191 loc) · 4.75 KB
/
router.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"encoding/base64"
"errors"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"github.com/gin-gonic/gin"
"gopkg.in/tucnak/telebot.v2"
)
func (sol *solution) setupRoutes() error {
sol.loadTemplates("./templates/*")
sol.Gin.Static("/assets", "./assets")
sol.Gin.GET("/", func(c *gin.Context) {
sol.renderTemplate(
c,
http.StatusOK,
"home.html",
sol.getDefaultRequestHeaders(),
)
})
sol.Gin.NoRoute(func(c *gin.Context) {
sol.renderTemplate(
c,
http.StatusNotFound,
"404.html",
sol.getDefaultRequestHeaders(),
)
})
sol.Gin.POST("/send", func(c *gin.Context) {
sol.handleMessageRequest(c)
})
sol.Gin.POST("/upload", func(c *gin.Context) {
fileHandler, err := c.FormFile("files[]")
if err != nil {
handleRequestError(c, errors.New("failed to get file from context: "+err.Error()))
return
}
fileExtension := filepath.Ext(fileHandler.Filename)
switch fileExtension {
default:
handleRequestError(c, errors.New("unknown file extension: "+fileExtension))
return
case ".png":
break
case ".jpg":
break
case ".jpeg":
break
}
err = c.SaveUploadedFile(fileHandler, uploadedImageFilename)
if err != nil {
handleRequestError(c, errors.New("failed to save uploaded file: "+err.Error()))
return
}
handleRequestSuccess(c)
})
sol.Gin.GET("/exit", func(c *gin.Context) {
os.Exit(1)
})
sol.Gin.POST("/check", func(c *gin.Context) {
if sol.LastError != nil {
handleRequestError(c, sol.LastError)
return
}
if sol.Messengers.Utopia == nil {
handleRequestError(c, errors.New("utopia not connected. wait to reconnect"))
return
}
handleRequestSuccess(c)
})
return nil
}
func (sol *solution) handleMessageRequest(c *gin.Context) {
msg := c.PostForm("post_text")
if msg == "" {
handleRequestError(c, errors.New("post message is empty"))
return
}
sendToTelegram := c.PostForm("post_telegram") == "1"
sendToUtopia := c.PostForm("post_utopia") == "1"
if !sendToTelegram && !sendToUtopia {
handleRequestError(c, errors.New("no messenger is selected"))
return
}
// hasimage
postHasImage := c.PostForm("hasimage") == "1"
imageFilename := ""
if postHasImage {
imageFilename = "image.jpg"
}
if sendToTelegram {
if !sol.sendTelegramPost(msg, imageFilename, c) {
return
}
}
if sendToUtopia {
if !sol.sendUtopiaPost(msg, imageFilename, c) {
return
}
}
handleRequestSuccess(c)
}
func (sol *solution) sendTelegramPost(postText string, imageFilename string, c *gin.Context) bool {
var msg interface{}
if imageFilename != "" {
msg = &telebot.Photo{
File: telebot.FromDisk(imageFilename),
Caption: postText,
}
} else {
msg = postText
}
postOptions := []interface{}{
telebot.ModeMarkdown,
}
if sol.Config.Telegram.SilentMode {
postOptions = append(postOptions, telebot.Silent)
}
_, err := sol.Messengers.Telegram.Send(
telebot.ChatID(sol.Config.Telegram.ChatID),
msg, postOptions...,
)
if err != nil {
handleRequestError(c, errors.New("failed to send post to Telegram: "+err.Error()))
return false
}
return true
}
func (sol *solution) sendUtopiaPost(postText string, imageFilename string, c *gin.Context) bool {
if sol.Config.Utopia.ChannelID == "" {
handleRequestError(c, errors.New("utopia channel ID is not set"))
return false
}
if !sol.Messengers.Utopia.CheckClientConnection() {
// try to reconnect
err := sol.connectUtopia()
if err != nil {
handleRequestError(c, err)
return false
}
}
if imageFilename != "" {
imageBytes, err := ioutil.ReadFile(imageFilename)
if err != nil {
handleRequestError(c, errors.New("failed to read uploaded image: "+err.Error()))
return false
}
_, err = sol.Messengers.Utopia.SendChannelPicture(
sol.Config.Utopia.ChannelID,
base64.StdEncoding.EncodeToString(imageBytes),
postText,
uploadedImageFilename,
)
if err != nil {
handleRequestError(c, errors.New("failed to send post with image to Utopia: "+err.Error()))
return false
}
return true
}
// send plain text
_, err := sol.Messengers.Utopia.SendChannelMessage(sol.Config.Utopia.ChannelID, postText)
if err != nil {
handleRequestError(c, errors.New("failed to send post to Utopia: "+err.Error()))
return false
}
return true
}
func handleRequestError(c *gin.Context, err error) {
c.JSON(http.StatusOK, response{
Status: "error",
ErrorInfo: err.Error(),
})
}
func handleRequestSuccess(c *gin.Context) {
c.JSON(http.StatusOK, response{
Status: "success",
})
}
func (sol *solution) getDefaultRequestHeaders() gin.H {
return gin.H{
"version": sol.Config.FrontEnd.Version,
}
}
func (sol *solution) loadTemplates(pattern string) {
sol.Gin.LoadHTMLGlob(pattern)
}
func (sol *solution) renderTemplate(c *gin.Context, code int, name string, obj interface{}) {
c.HTML(code, name, obj)
}