valera/internal/states/ping_bot_state/ping_bot_state.go

43 lines
924 B
Go

package ping_bot_state
import (
"net/http"
"valera/internal/db"
"valera/internal/states"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"golang.org/x/exp/slices"
)
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,
}
}
func (s *pingBotState) Do(text string, chatInfo *db.ChatInfo, username string) error {
if !slices.Contains(names, text) {
return nil
}
msg := "pong"
_, _ = 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)
}
}