diff --git a/.example.env b/.example.env index 78047b3..db0b8a9 100644 --- a/.example.env +++ b/.example.env @@ -1,4 +1,6 @@ APP_URL="" TELE_BOT_TOKEN="" OPEN_WEATHER_MAP_APP_ID="" -UPSTASH_URL="" \ No newline at end of file +UPSTASH_URL="" +CRON_SECRET="" +BOT_OWNER_CHAT_ID= \ No newline at end of file diff --git a/api/cron/index.go b/api/cron/index.go new file mode 100644 index 0000000..62ac4ca --- /dev/null +++ b/api/cron/index.go @@ -0,0 +1,55 @@ +package cron + +import ( + "fmt" + "net/http" + "os" + "strconv" + "zeril-bot/utils/bot" + "zeril-bot/utils/request" + "zeril-bot/utils/structs" + "zeril-bot/utils/telegram" +) + +func Handler(w http.ResponseWriter, r *http.Request) { + res := make(map[string]string) + if token := r.Header.Get("Authorization"); token != "Bearer "+os.Getenv("CRON_SECRET") { + res["status"] = "ERROR" + res["message"] = "Token invalid" + request.Response(w, res, http.StatusOK) + return + } + + telegram := telegram.New(&http.Client{}, telegram.BASE_URL) + + chatId, _ := strconv.Atoi(os.Getenv("BOT_OWNER_CHAT_ID")) + + data := structs.HookData{ + + Message: structs.Message{ + Text: "/quote", + From: structs.From{ + ID: chatId, + FirstName: "Cron", + }, Chat: structs.Chat{ + ID: chatId, + FirstName: "Cron", + }, + }, + } + + fmt.Println(data) + bot := bot.NewBot(telegram, data) + err := bot.ResolveHook() + + if err != nil { + res["status"] = "ERROR" + res["code"] = "internal_error" + res["message"] = err.Error() + request.Response(w, res, http.StatusInternalServerError) + return + } + + res["status"] = "OK" + request.Response(w, res, http.StatusOK) +} diff --git a/api/hook/index.go b/api/hook/index.go index eefd9dc..0fc089a 100644 --- a/api/hook/index.go +++ b/api/hook/index.go @@ -4,6 +4,7 @@ import ( "encoding/json" "net/http" "zeril-bot/utils/bot" + "zeril-bot/utils/request" "zeril-bot/utils/structs" "zeril-bot/utils/telegram" ) @@ -18,9 +19,9 @@ func Handler(w http.ResponseWriter, r *http.Request) { res := make(map[string]string) if data.Message.Text == "" { - res["status"] = "OK" + res["status"] = "ERROR" res["message"] = "Ignore hook with chat content not found" - Response(w, res, http.StatusOK) + request.Response(w, res, http.StatusOK) return } @@ -34,17 +35,10 @@ func Handler(w http.ResponseWriter, r *http.Request) { res["status"] = "ERROR" res["code"] = "internal_error" res["message"] = err.Error() - Response(w, res, http.StatusInternalServerError) + request.Response(w, res, http.StatusInternalServerError) return } res["status"] = "OK" - Response(w, res, http.StatusOK) -} - -func Response(w http.ResponseWriter, res map[string]string, httpStatus int) { - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(httpStatus) - mRes, _ := json.Marshal(res) - w.Write(mRes) + request.Response(w, res, http.StatusOK) } diff --git a/api/index.go b/api/index.go index 28cb469..125c966 100644 --- a/api/index.go +++ b/api/index.go @@ -2,6 +2,7 @@ package api import ( "net/http" + "zeril-bot/api/cron" "zeril-bot/api/hook" "zeril-bot/api/url" "zeril-bot/utils/middleware" @@ -22,6 +23,7 @@ func Handler(wri http.ResponseWriter, req *http.Request) { w.Write([]byte("Hi there !")) }) r.Post("/api/hook", hook.Handler) + r.Get("/api/cron", cron.Handler) r.Get("/url", url.Handler) r.ServeHTTP(wri, req) diff --git a/utils/bot/bot.go b/utils/bot/bot.go index 8b48cf6..2f627c0 100644 --- a/utils/bot/bot.go +++ b/utils/bot/bot.go @@ -2,6 +2,7 @@ package bot import ( "errors" + "fmt" "log" "strings" "time" @@ -88,6 +89,8 @@ func (b Bot) resolveCommand() error { data := b.getTelegramData() + fmt.Println(data) + switch data.Command { case "/start", "/start@zerill_bot": err = b.sendStartMessage(data) diff --git a/utils/request/request.go b/utils/request/request.go new file mode 100644 index 0000000..f45fe3a --- /dev/null +++ b/utils/request/request.go @@ -0,0 +1,13 @@ +package request + +import ( + "encoding/json" + "net/http" +) + +func Response(w http.ResponseWriter, res map[string]string, httpStatus int) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(httpStatus) + mRes, _ := json.Marshal(res) + w.Write(mRes) +} diff --git a/vercel.json b/vercel.json index 93282c8..bf1451c 100644 --- a/vercel.json +++ b/vercel.json @@ -1,18 +1,16 @@ { "routes": [ - { - "src": "/url/(.*)", - "dest": "/api/url/index.go" - }, - { - "src": "/trip/(.*)", - "dest": "/api/trip/index.go" - }, { "src": "/(.*)", "dest": "/api/index.go" } ], + "crons": [ + { + "path": "/api/cron", + "schedule": "*/5 * * * *" + } + ], "github": { "enabled": false, "silent": true