Skip to content

Commit

Permalink
feat: cron jobs send quote
Browse files Browse the repository at this point in the history
  • Loading branch information
totanvix committed Nov 28, 2023
1 parent 8db1ab5 commit 4d5b0ce
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .example.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
APP_URL=""
TELE_BOT_TOKEN=""
OPEN_WEATHER_MAP_APP_ID=""
UPSTASH_URL=""
UPSTASH_URL=""
CRON_SECRET=""
BOT_OWNER_CHAT_ID=
55 changes: 55 additions & 0 deletions api/cron/index.go
Original file line number Diff line number Diff line change
@@ -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)
}
16 changes: 5 additions & 11 deletions api/hook/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}

Expand All @@ -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)
}
2 changes: 2 additions & 0 deletions api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions utils/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bot

import (
"errors"
"fmt"
"log"
"strings"
"time"
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions utils/request/request.go
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 6 additions & 8 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 4d5b0ce

Please sign in to comment.