-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
319 lines (266 loc) · 7.17 KB
/
main.go
File metadata and controls
319 lines (266 loc) · 7.17 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"sync"
"github.com/gin-gonic/gin"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
type form struct {
Id int `json:"id"`
Name string `json:"name"`
Gmail string `json:"gmail"`
Description string `json:"description"`
}
type BotInfo struct {
adminChatID int64
}
var (
sheetsService *sheets.Service
serviceReady = false
serviceMutex sync.RWMutex
)
func isServiceReady() bool {
serviceMutex.RLock()
defer serviceMutex.RUnlock()
return serviceReady
}
func setServiceReady(srv *sheets.Service) {
serviceMutex.Lock()
defer serviceMutex.Unlock()
sheetsService = srv
serviceReady = true
}
func main() {
err := godotenv.Load(".env.local")
if err != nil {
log.Fatal("Error loading .env file in main")
}
// Initialize Telegram bot
botInfo := &BotInfo{}
bot, err := tgbotapi.NewBotAPI(os.Getenv("TG_BOT_TOKEN"))
if err != nil {
log.Fatalf("Failed to create bot: %v", err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
// Start Telegram
go setupTelegramBot(bot, botInfo)
// Initialize db
DB_URL := os.Getenv("DB_URL")
db, err := sql.Open("postgres", DB_URL)
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec("CREATE TABLE IF NOT EXISTS formdata (id SERIAL PRIMARY KEY, name TEXT, gmail TEXT, description TEXT)")
if err != nil {
log.Fatal(err)
}
r := gin.Default()
// Setup routes
setupRoutes(r, db, bot, botInfo)
// Start GoogleOAuth
go setupGoogleOAuth()
// Start server
log.Println("Starting server on :8080")
if err := r.Run(); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}
func setupTelegramBot(bot *tgbotapi.BotAPI, botInfo *BotInfo) {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message != nil {
botInfo.adminChatID = update.Message.Chat.ID
log.Printf("Set admin chat ID to: %d", botInfo.adminChatID)
}
}
}
func setupRoutes(r *gin.Engine, db *sql.DB, bot *tgbotapi.BotAPI, botInfo *BotInfo) {
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{"status": "Server is running"})
})
// OAuth callback route
r.GET("/callback", handleOAuthCallback)
r.POST("/SendForm", func(c *gin.Context) {
if !isServiceReady() {
c.JSON(http.StatusServiceUnavailable, gin.H{
"error": "Google Sheets service is not ready yet. Please try again in a moment.",
})
return
}
handleFormSubmission(c, db, bot, botInfo, sheetsService)
})
r.GET("/status", func(c *gin.Context) {
if isServiceReady() {
c.JSON(http.StatusOK, gin.H{"status": "Google Sheets service is ready"})
} else {
c.JSON(http.StatusOK, gin.H{"status": "Google Sheets service is initializing"})
}
})
}
var (
oauthConfig *oauth2.Config
oauthToken *oauth2.Token
tokenFile = "token.json"
)
func setupGoogleOAuth() {
ctx := context.Background()
b, err := os.ReadFile("web.json")
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
return
}
oauthConfig, err = google.ConfigFromJSON(b, "https://www.googleapis.com/auth/spreadsheets")
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
return
}
oauthConfig.RedirectURL = "http://localhost:8080/callback"
client, err := getOAuthClient(ctx)
if err != nil {
authURL := oauthConfig.AuthCodeURL("state")
fmt.Printf("Visit this URL to authorize: %v\n", authURL)
return
}
srv, err := sheets.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Unable to retrieve Sheets client: %v", err)
return
}
setServiceReady(srv)
log.Println("Google Sheets service is now ready")
}
func getOAuthClient(ctx context.Context) (*http.Client, error) {
tok, err := readTokenFromFile(tokenFile)
if err == nil {
return oauthConfig.Client(ctx, tok), nil
}
return nil, fmt.Errorf("no saved token")
}
func handleOAuthCallback(c *gin.Context) {
ctx := context.Background()
code := c.Query("code")
if code == "" {
c.String(400, "Code not found")
return
}
tok, err := oauthConfig.Exchange(ctx, code)
if err != nil {
c.String(500, "Failed to exchange token: "+err.Error())
return
}
if err := saveTokenToFile(tokenFile, tok); err != nil {
log.Printf("Unable to save token: %v", err)
}
client := oauthConfig.Client(ctx, tok)
srv, err := sheets.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
c.String(500, "Failed to create sheets service: "+err.Error())
return
}
setServiceReady(srv)
log.Println("Google Sheets service is now ready")
c.String(200, "Authorization successful! You can close this window and use the form now.")
}
func saveTokenToFile(file string, token *oauth2.Token) error {
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()
return json.NewEncoder(f).Encode(token)
}
func readTokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
tok := &oauth2.Token{}
err = json.NewDecoder(f).Decode(tok)
return tok, err
}
func handleFormSubmission(c *gin.Context, db *sql.DB, bot *tgbotapi.BotAPI, botInfo *BotInfo, srv *sheets.Service) {
name := c.PostForm("name")
if name == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Name is not provided"})
return
}
gmail := c.PostForm("gmail")
if gmail == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Gmail is not provided"})
return
}
description := c.PostForm("description")
if description == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Description is not provided"})
return
}
Data := form{
Name: name,
Gmail: gmail,
Description: description,
}
// DB insertion
err := db.QueryRow("INSERT INTO formdata (name, gmail, description) VALUES ($1, $2, $3) RETURNING id",
Data.Name, Data.Gmail, Data.Description).Scan(&Data.Id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to insert data into database: " + err.Error(),
})
return
}
// TG sent
chatID := botInfo.adminChatID
if chatID != 0 {
text := fmt.Sprintf("New Contact Request!\n\nName: %s\nEmail: %s\nMessage: %s",
Data.Name, Data.Gmail, Data.Description)
msg := tgbotapi.NewMessage(chatID, text)
_, err := bot.Send(msg)
if err != nil {
log.Printf("Failed to send Telegram message: %v", err)
}
}
// Sheet save
spreadsheetId := "1X5nVMQqSsYuJhxu_DAXuy0ZycKBWgSVJIeBIRZc04KA"
readRange := "Sheet1!A2:E"
values := [][]interface{}{{
Data.Name,
Data.Gmail,
Data.Description,
}}
valueRange := &sheets.ValueRange{
Values: values,
}
_, err = srv.Spreadsheets.Values.Append(spreadsheetId, readRange, valueRange).
ValueInputOption("RAW").
InsertDataOption("INSERT_ROWS").
Do()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to append data to spreadsheet: " + err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"_id": Data.Id,
"name": Data.Name,
"email": Data.Gmail,
"description": Data.Description,
})
}