valera/internal/states/stat_bot_state/stat_bot_state.go

105 lines
2.7 KiB
Go
Raw Normal View History

2023-04-08 11:55:16 +00:00
package stat_bot_state
import (
"fmt"
"net/http"
2023-04-09 07:15:53 +00:00
"time"
2023-04-08 11:55:16 +00:00
"valera/internal/db"
"valera/internal/states"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"golang.org/x/exp/slices"
)
var names = []string{
"/stat",
2023-04-09 07:15:53 +00:00
"/stat_week",
2023-04-08 11:55:16 +00:00
}
type statBotState struct {
bot *tgbot.BotAPI
dataBase *db.DB
}
func NewStatBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState {
return &statBotState{
bot: bot,
dataBase: dataBase,
}
}
2023-04-09 07:47:38 +00:00
func (s *statBotState) Do(text string, chatInfo *db.ChatInfo) error {
2023-04-08 11:55:16 +00:00
if !slices.Contains(names, text) {
return nil
}
2023-04-09 07:15:53 +00:00
if text == "/stat" {
s.sendStatToChatAfter(chatInfo.ChatID, "Результаты за сегодня:\n", s.getStartDay())
}
if text == "/stat_week" {
s.sendStatToChatAfter(chatInfo.ChatID, "Результаты за неделю:\n", s.getStartWeek())
}
2023-04-08 11:55:16 +00:00
return nil
}
func (s *statBotState) GetHandler() (string, func(http.ResponseWriter, *http.Request)) {
return names[0], func(w http.ResponseWriter, r *http.Request) {
chats, err := s.dataBase.GetAllChats()
if err != nil {
fmt.Println(err)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintf(w, `{"result":"error"}`)
return
}
for _, chatID := range chats {
2023-04-09 07:15:53 +00:00
t := s.getStartDay()
if err := s.sendStatToChatAfter(chatID, "Напоминаю:\n- Cегодня больше не жрем!\n\nРезультаты за сегодня:\n", s.getStartDay()); err != nil {
2023-04-08 11:55:16 +00:00
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintf(w, `{"result":"error"}`)
return
}
2023-04-09 07:15:53 +00:00
if t.Weekday() == time.Sunday {
if err := s.sendStatToChatAfter(chatID, "Результаты за неделю:\n", s.getStartWeek()); err != nil {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintf(w, `{"result":"error"}`)
return
}
}
2023-04-08 11:55:16 +00:00
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, `{"result":"ok"}`)
}
}
2023-04-09 07:15:53 +00:00
func (s *statBotState) sendStatToChatAfter(chatID int64, prefix string, t time.Time) error {
msgText := prefix
stat, err := s.dataBase.GetStatAfter(chatID, t)
2023-04-08 11:55:16 +00:00
if err != nil {
return err
}
for k, v := range stat {
msgText += fmt.Sprintf("- %s: %d\n", k, v)
}
_, _ = s.bot.Send(tgbot.NewMessage(chatID, msgText))
return nil
}
2023-04-09 07:15:53 +00:00
func (s *statBotState) getStartDay() time.Time {
loc, _ := time.LoadLocation("Asia/Novosibirsk")
t := time.Now().In(loc)
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)
return t
}
func (s *statBotState) getStartWeek() time.Time {
t := s.getStartDay()
t = t.Add(-6 * 24 * time.Hour)
return t
}