valera/internal/states/stat_bot_state/stat_bot_state.go

94 lines
2.5 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-15 12:13:10 +00:00
2023-04-08 11:55:16 +00:00
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"golang.org/x/exp/slices"
2023-04-16 10:08:40 +00:00
"valera/internal/db"
"valera/internal/modules/times"
"valera/internal/states"
2023-04-08 11:55:16 +00:00
)
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" {
2023-04-15 12:13:10 +00:00
s.sendStatToChatAfter(chatInfo.ChatID, "Результаты за сегодня:\n", times.GetStartDay())
2023-04-09 07:15:53 +00:00
}
if text == "/stat_week" {
2023-04-15 12:13:10 +00:00
s.sendStatToChatAfter(chatInfo.ChatID, "Результаты за неделю:\n", times.GetStartWeek())
2023-04-09 07:15:53 +00:00
}
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-15 12:13:10 +00:00
t := times.GetStartDay()
if err := s.sendStatToChatAfter(chatID, "Напоминаю:\n- Cегодня больше не жрем!\n\nРезультаты за сегодня:\n", times.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 {
2023-04-15 12:13:10 +00:00
if err := s.sendStatToChatAfter(chatID, "Результаты за неделю:\n", times.GetStartWeek()); err != nil {
2023-04-09 07:15:53 +00:00
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
2023-04-16 10:08:40 +00:00
stat, err := s.dataBase.GetStatBetween(chatID, t, times.GetNow())
2023-04-08 11:55:16 +00:00
if err != nil {
return err
}
for k, v := range stat {
2023-04-09 08:48:31 +00:00
msgText += fmt.Sprintf("- %s: %v\n", k, v)
2023-04-08 11:55:16 +00:00
}
_, _ = s.bot.Send(tgbot.NewMessage(chatID, msgText))
return nil
}