Skip to content

Commit

Permalink
Handle request from Google Form
Browse files Browse the repository at this point in the history
  • Loading branch information
tit8 authored and tit8 committed May 2, 2024
1 parent 0538592 commit 1083aeb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
18 changes: 11 additions & 7 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
# fly.toml app configuration file generated for formapi on 2023-08-27T12:58:16+02:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "formapi"
primary_region = "cdg"
app = 'formapi'
primary_region = 'cdg'

[build]
builder = "paketobuildpacks/builder:base"
buildpacks = ["gcr.io/paketo-buildpacks/go"]
builder = 'paketobuildpacks/builder:base'
buildpacks = ['gcr.io/paketo-buildpacks/go']

[env]
PORT = "8080"
PORT = '8080'

[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ["app"]
processes = ['app']

[[vm]]
memory = '1gb'
cpu_kind = 'shared'
cpus = 1
69 changes: 61 additions & 8 deletions form-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ type Request struct {
Captcha string `json:"frc-captcha-solution"`
}

type GForm struct {
Name string `json:"name"`
Email string `json:"email"`
Message string `json:"message"`
File string `json:"file"`
}

func getUrl() string {

return fmt.Sprintf("https://api.telegram.org/bot%s", os.Getenv("TELEGRAM_TOKEN"))
Expand Down Expand Up @@ -75,6 +82,54 @@ func handler_get(w http.ResponseWriter, r *http.Request) {

}

func handler_sheet(w http.ResponseWriter, r *http.Request) {

if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

r.Body = http.MaxBytesReader(w, r.Body, 5*1024)

w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")

content_type, err := regexp.MatchString("application/json", r.Header.Get("Content-Type"))
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusBadRequest)
return
}
if !content_type {
w.WriteHeader(http.StatusUnsupportedMediaType)
return
}

body, err := io.ReadAll(r.Body)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}

var data GForm
err = json.Unmarshal(body, &data)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}

text := fmt.Sprintf("New message from Google Form!\n\nName:\t%s\n\nE-mail:\t%s\n\nMessage:\t%s\n\nFile link:\t%s", data.Name, data.Email, data.Message, data.File)
result, err := SendMessage(text)
if !result {
log.Printf("Error sending telegram messag, %s\n", err)
w.WriteHeader(http.StatusNotAcceptable)
} else {
w.WriteHeader(http.StatusOK)
}
}

func handler_post(w http.ResponseWriter, r *http.Request) {

if r.Method != "POST" {
Expand Down Expand Up @@ -128,48 +183,45 @@ func handler_post(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("Formatting body failed: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

post := Post{}
err = json.Unmarshal(body, &post)
if err != nil {
log.Printf("Reading body failed: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
//log.Printf("Captcha validation: %v", post.Success)

var ok string
if post.Success {

text := fmt.Sprintf("New message!\n\nName:\t%s\n\nE-mail:\t%s\n\nMessage:\t%s\n", req.Name, req.Email, req.Message)
text := fmt.Sprintf("New message from HTML form!\n\nName:\t%s\n\nE-mail:\t%s\n\nMessage:\t%s\n", req.Name, req.Email, req.Message)

result, err := SendMessage(text)
if !result {

log.Printf("Error sending telegram messag, %s\n", err)
w.WriteHeader(http.StatusNotAcceptable)
ok = "Valid CAPTCHA but error on Telegram request"

} else {

w.WriteHeader(http.StatusOK)
ok = "Valid CAPTCHA and Telegram sent"

}

} else {

ok = "CAPTCHA validation failed"
w.WriteHeader(http.StatusNotAcceptable)

}

response := Response{ok}
res, err := json.Marshal(response)
if err != nil {
log.Println(err)
}
log.Println(string(res))
//log.Println(string(res))

w.Header().Set("Content-Type", "application/json")
w.Write(res)
Expand All @@ -180,6 +232,7 @@ func main() {

http.HandleFunc("/", handler_get)
http.HandleFunc("/post", handler_post)
http.HandleFunc("/sheet", handler_sheet)

log.Fatal(http.ListenAndServe(":8080", nil))

Expand Down

0 comments on commit 1083aeb

Please sign in to comment.