Skip to content

Commit

Permalink
refactor: refactor func get commands
Browse files Browse the repository at this point in the history
  • Loading branch information
totanvix committed Jul 3, 2023
1 parent 470c5b5 commit 6fc7837
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
8 changes: 6 additions & 2 deletions utils/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ func (b Bot) sendStartMessage(data structs.DataTele) error {

func (b Bot) sendHelpMessage(data structs.DataTele) error {
messages := ""
botCommands := b.getBotCommands()
botCommands, err := b.getBotCommands()

if err != nil {
return err
}

for _, command := range botCommands.Result {
messages += fmt.Sprintf("<code>/%s</code> - %s\n\n", command.Command, command.Description)
Expand All @@ -222,7 +226,7 @@ func (b Bot) sendHelpMessage(data structs.DataTele) error {
return telegram.SendMessage(data)
}

func (b Bot) getBotCommands() structs.BotCommands {
func (b Bot) getBotCommands() (*structs.BotCommands, error) {
return telegram.GetBotCommands()
}

Expand Down
3 changes: 2 additions & 1 deletion utils/middleware/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func Recoverer(next http.Handler) http.Handler {
w.WriteHeader(http.StatusOK)

resp["status"] = "ERROR"
resp["message"] = rvr.(string)
resp["code"] = "internal_error"
resp["message"] = "Something went wrong"
jsonResp, _ := json.Marshal(resp)

logEntry := middleware.GetLogEntry(r)
Expand Down
19 changes: 10 additions & 9 deletions utils/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,41 +226,42 @@ func SetTypingAction(data structs.DataTele) error {
return nil
}

func GetBotCommands() structs.BotCommands {
func GetBotCommands() (*structs.BotCommands, error) {
url := getApiURL("getMyCommands")
req, err := http.NewRequest("GET", url, nil)

if err != nil {
log.Panic(err)
return nil, err
}

client := &http.Client{}

res, err := client.Do(req)

if err != nil {
log.Panic(err)
return nil, err
}

defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)

if err != nil {
log.Panic(err)
return nil, err
}

var botCommands structs.BotCommands

err = json.Unmarshal(body, &botCommands)
if err != nil {
log.Panic(err)
return nil, err
}

if botCommands.Ok == false {
log.Fatalln(string(body))
if botCommands.Ok {
log.Println("GetBotCommands OK")

return &botCommands, nil
}

log.Println("GetBotCommands OK")
return botCommands
return nil, errors.New(string(body))
}

0 comments on commit 6fc7837

Please sign in to comment.