Skip to content

Commit

Permalink
Fix actions
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoVG committed May 28, 2023
1 parent 18200b1 commit 7481ada
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
1 change: 0 additions & 1 deletion container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
logger "github.com/br0-space/bot-logger"
"github.com/br0-space/bot/interfaces"
"github.com/br0-space/bot/pkg/config"
_ "github.com/br0-space/bot/pkg/config"
"github.com/br0-space/bot/pkg/db"
"github.com/br0-space/bot/pkg/fortune"
"github.com/br0-space/bot/pkg/matcher"
Expand Down
12 changes: 6 additions & 6 deletions pkg/webhook/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (h *Handler) InitMatchers() {
func (h *Handler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
h.log.Debugf("%s %s %s from %s", req.Method, req.URL, req.Proto, req.RemoteAddr)

messageIn, err, status := h.parseRequest(req)
messageIn, status, err := h.parseRequest(req)
if err != nil {
h.log.Error(err)
http.Error(res, err.Error(), status)
Expand All @@ -45,21 +45,21 @@ func (h *Handler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
h.processRequest(*messageIn)
}

func (h *Handler) parseRequest(req *http.Request) (*interfaces.TelegramWebhookMessageStruct, error, int) {
func (h *Handler) parseRequest(req *http.Request) (*interfaces.TelegramWebhookMessageStruct, int, error) {
if req.Method != http.MethodPost {
return nil, fmt.Errorf("method not allowed: %s (actual) != POST (expected)", req.Method), http.StatusMethodNotAllowed
return nil, http.StatusMethodNotAllowed, fmt.Errorf("method not allowed: %s (actual) != POST (expected)", req.Method)
}

body := &interfaces.TelegramWebhookBodyStruct{}
if err := json.NewDecoder(req.Body).Decode(body); err != nil {
return nil, fmt.Errorf("unable to decode request body: %s", err.Error()), http.StatusBadRequest
return nil, http.StatusBadRequest, fmt.Errorf("unable to decode request body: %s", err.Error())
}

if body.Message.Chat.ID != h.cfg.Telegram.ChatID {
return nil, fmt.Errorf("chat id mismatch: %d (actual) != %d (expected)", body.Message.Chat.ID, h.cfg.Telegram.ChatID), http.StatusOK
return nil, http.StatusOK, fmt.Errorf("chat id mismatch: %d (actual) != %d (expected)", body.Message.Chat.ID, h.cfg.Telegram.ChatID)
}

return &body.Message, nil, 0
return &body.Message, 0, nil
}

func (h *Handler) processRequest(messageIn interfaces.TelegramWebhookMessageStruct) {
Expand Down

0 comments on commit 7481ada

Please sign in to comment.