2023-04-08 11:55:16 +00:00
|
|
|
package ping_bot_state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
|
|
|
|
"golang.org/x/exp/slices"
|
2023-04-22 08:58:23 +00:00
|
|
|
|
|
|
|
"valera/internal/db"
|
|
|
|
"valera/internal/states"
|
2023-04-08 11:55:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var names = []string{
|
|
|
|
"/ping",
|
|
|
|
}
|
|
|
|
|
|
|
|
type pingBotState struct {
|
|
|
|
bot *tgbot.BotAPI
|
|
|
|
dataBase *db.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPingBotState(bot *tgbot.BotAPI, dataBase *db.DB) states.BotState {
|
|
|
|
return &pingBotState{
|
|
|
|
bot: bot,
|
|
|
|
dataBase: dataBase,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 07:47:38 +00:00
|
|
|
func (s *pingBotState) Do(text string, chatInfo *db.ChatInfo) error {
|
2023-04-08 11:55:16 +00:00
|
|
|
if !slices.Contains(names, text) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-22 08:58:23 +00:00
|
|
|
msg := "Понг"
|
2023-04-08 11:55:16 +00:00
|
|
|
_, _ = s.bot.Send(tgbot.NewMessage(chatInfo.ChatID, msg))
|
|
|
|
return s.dataBase.SetStatusToChat(chatInfo.ChatID, db.UserStateNone)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *pingBotState) GetHandler() (string, func(http.ResponseWriter, *http.Request)) {
|
|
|
|
return names[0], func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|