Skip to content

Commit

Permalink
Add bodka commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kozaktomas committed Nov 23, 2024
1 parent 4151232 commit 4aac1d9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 10 deletions.
77 changes: 70 additions & 7 deletions backend/pkg/hook/botka.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"
"sync"
"time"

"github.com/kotrzina/keg-scale/pkg/config"
"github.com/kotrzina/keg-scale/pkg/utils"
Expand All @@ -19,14 +20,15 @@ type Botka struct {
brain *BotkaBrain
config *config.Config

mtx sync.Mutex
mtx sync.RWMutex
logger *logrus.Logger
}

type BotkaBrain struct {
Weight float64
BeerLeft int
ActiveKeg int
Weight float64
BeerLeft int
ActiveKeg int
ActiveKegAt time.Time

Warehouse map[int]int // keys 10, 15, 20, 30, 50
WarehouseTotal int
Expand All @@ -38,14 +40,16 @@ func NewBotka(client *wa.WhatsAppClient, conf *config.Config, logger *logrus.Log
brain: &BotkaBrain{},
config: conf,

mtx: sync.Mutex{},
mtx: sync.RWMutex{},
logger: logger,
}

if !conf.Debug {
// replies only on production
client.RegisterEventHandler(w.helpHandler())
client.RegisterEventHandler(w.kegHandler())
client.RegisterEventHandler(w.pricesHandler())
client.RegisterEventHandler(w.warehouseHandler())
}

return w
Expand All @@ -62,6 +66,9 @@ func (b *Botka) UpdateBotkaBrain(bb *BotkaBrain) {

func (b *Botka) SendOpen() {
go func() {
b.mtx.RLock()
defer b.mtx.RUnlock()

msg := "Pivo! 🍺"

if b.brain.ActiveKeg > 0 {
Expand Down Expand Up @@ -91,18 +98,42 @@ func (b *Botka) SendOpen() {
func (b *Botka) helpHandler() wa.EventHandler {
return wa.EventHandler{
MatchFunc: func(msg string) bool {
return strings.HasPrefix(sanitizeCommand(msg), "help")
return strings.HasPrefix(sanitizeCommand(msg), "help") ||
strings.HasPrefix(sanitizeCommand(msg), "napoveda") ||
strings.HasPrefix(sanitizeCommand(msg), "pomoc")
},
Handler: func(from, _ string) error {
reply := "Příkazy: \n" +
"/help - zobrazí nápovědu \n" +
"/cenik - ceník"
"/becka - informace o aktuální bečce \n" +
"/cenik - ceník \n" +
"/sklad - stav skladu"
err := b.whatsapp.SendText(from, reply)
return err
},
}
}

func (b *Botka) kegHandler() wa.EventHandler {
return wa.EventHandler{
MatchFunc: func(msg string) bool {
return strings.HasPrefix(sanitizeCommand(msg), "becka")
},
Handler: func(from, _ string) error {
msg := fmt.Sprintf(
"\nMáme naraženou %dl bečku a zbývá v ní %d %s. Naražena byla %s v %s.",
b.brain.ActiveKeg,
b.brain.BeerLeft,
utils.FormatBeer(b.brain.BeerLeft),
utils.FormatDateShort(b.brain.ActiveKegAt),
utils.FormatTime(b.brain.ActiveKegAt),
)
err := b.whatsapp.SendText(from, msg)
return err
},
}
}

func (b *Botka) pricesHandler() wa.EventHandler {
return wa.EventHandler{
MatchFunc: func(msg string) bool {
Expand All @@ -118,6 +149,38 @@ func (b *Botka) pricesHandler() wa.EventHandler {
}
}

func (b *Botka) warehouseHandler() wa.EventHandler {
return wa.EventHandler{
MatchFunc: func(msg string) bool {
return strings.HasPrefix(sanitizeCommand(msg), "sklad")
},
Handler: func(from, _ string) error {
b.mtx.RLock()
defer b.mtx.RUnlock()

reply := fmt.Sprintf("Ve skladu máme celkem %d piv.", b.brain.WarehouseTotal)
if b.brain.Warehouse[10] > 0 {
reply += fmt.Sprintf("\n%d x 10l", b.brain.Warehouse[10])
}
if b.brain.Warehouse[15] > 0 {
reply += fmt.Sprintf("\n%d x 15l", b.brain.Warehouse[15])
}
if b.brain.Warehouse[20] > 0 {
reply += fmt.Sprintf("\n%d x 20l", b.brain.Warehouse[20])
}
if b.brain.Warehouse[30] > 0 {
reply += fmt.Sprintf("\n%d x 30l", b.brain.Warehouse[30])
}
if b.brain.Warehouse[50] > 0 {
reply += fmt.Sprintf("\n%d x 50l", b.brain.Warehouse[50])
}

err := b.whatsapp.SendText(from, reply)
return err
},
}
}

func sanitizeCommand(command string) string {
return strings.ToLower(strings.TrimSpace(strings.TrimPrefix(command, "/")))
}
7 changes: 4 additions & 3 deletions backend/pkg/scale/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,10 @@ func (s *Scale) addCurrentKegToTotal() error {

func (s *Scale) updateBotka() {
bb := &hook.BotkaBrain{
Weight: s.weight,
BeerLeft: s.beersLeft,
ActiveKeg: s.activeKeg,
Weight: s.weight,
BeerLeft: s.beersLeft,
ActiveKeg: s.activeKeg,
ActiveKegAt: s.activeKegAt,
Warehouse: map[int]int{
10: s.warehouse[0],
15: s.warehouse[1],
Expand Down
8 changes: 8 additions & 0 deletions backend/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func FormatDate(t time.Time) string {
return t.In(getTz()).Format("2006-01-02 15:04:05")
}

func FormatDateShort(t time.Time) string {
if t.Unix() <= 0 {
return ""
}

return t.In(getTz()).Format("02. 01.")
}

func FormatTime(t time.Time) string {
if t.Unix() <= 0 {
return ""
Expand Down

0 comments on commit 4aac1d9

Please sign in to comment.