Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

Commit

Permalink
Add error handling for Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyaglow committed Nov 27, 2018
1 parent 67c214a commit b5ff9b3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
)

// Auth handles simple password authentication of a user
func (c *Client) Auth(input *tgbotapi.Message) {
func (c *Client) Auth(input *tgbotapi.Message) error {
msg := tgbotapi.NewMessage(input.Chat.ID, "")
msg.ReplyToMessageID = input.MessageID
if input.Text == c.Password {
err := c.registerUser(input.From)
if err != nil {
log.Fatal(err)
return err
}

log.Printf("Allowed users: %s", strings.Join(c.listUsers(), ","))
Expand All @@ -24,8 +24,10 @@ func (c *Client) Auth(input *tgbotapi.Message) {
msg.Text = "Wrong password"
}
if _, err := c.Bot.Send(msg); err != nil {
log.Println(err)
return err
}

return nil
}

// CheckAuth checks if user is allowed to interact with a bot
Expand Down
8 changes: 5 additions & 3 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"github.com/go-telegram-bot-api/telegram-bot-api"
)

const defaultTimeout = 20
const defaultPollTimeout = 20

// Run represents infinite function that waits for a message,
// authenticate user and process task
func (c *Client) Run() {
defer c.DB.Close()

u := tgbotapi.NewUpdate(0)
u.Timeout = defaultTimeout
u.Timeout = defaultPollTimeout

updates, err := c.Bot.GetUpdatesChan(u)
if err != nil {
Expand Down Expand Up @@ -59,7 +59,9 @@ func (c *Client) Run() {
}
}()
} else {
c.Auth(update.Message)
if err := c.Auth(update.Message); err != nil {
log.Println(err)
}
}
}
}
Expand Down

0 comments on commit b5ff9b3

Please sign in to comment.