Archived
1
This repository has been archived on 2022-11-04. You can view files and clone it, but cannot push or open issues or pull requests.
i2_bot/lib/reminder/updaters.go

67 lines
2.1 KiB
Go
Raw Normal View History

// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package reminder
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
2018-01-21 23:28:53 +04:00
"git.wtfteam.pro/fat0troll/i2_bot/lib/dbmapping"
"strconv"
"strings"
"time"
)
// CreateAlarmSetting creates alarm setting for user
func (r *Reminder) CreateAlarmSetting(update *tgbotapi.Update, playerRaw *dbmapping.Player) string {
turnirNumber := strings.TrimPrefix(update.CallbackQuery.Data, "enable_reminder_")
turnirNumberInt, err := strconv.Atoi(turnirNumber)
if err != nil {
c.Log.Error(err.Error())
return "fail"
}
alarm := dbmapping.Alarm{}
alarm.PlayerID = playerRaw.ID
alarm.TurnirNumber = turnirNumberInt
alarm.CreatedAt = time.Now().UTC()
_, err = c.Db.NamedExec("INSERT INTO `alarms` VALUES(NULL, :player_id, :turnir_number, :created_at)", &alarm)
if err != nil {
c.Log.Error(err.Error())
return "fail"
}
message := r.formatRemindersMessageText(playerRaw)
keyboard := r.formatRemindersButtons(playerRaw)
messageUpdate := tgbotapi.NewEditMessageText(update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Message.MessageID, message)
messageUpdate.ParseMode = "Markdown"
messageUpdate.ReplyMarkup = &keyboard
c.Bot.Send(messageUpdate)
return "ok"
}
// DestroyAlarmSetting creates alarm setting for user
func (r *Reminder) DestroyAlarmSetting(update *tgbotapi.Update, playerRaw *dbmapping.Player) string {
turnirNumber := strings.TrimPrefix(update.CallbackQuery.Data, "disable_reminder_")
turnirNumberInt, err := strconv.Atoi(turnirNumber)
if err != nil {
c.Log.Error(err.Error())
return "fail"
}
_, err = c.Db.Exec(c.Db.Rebind("DELETE FROM `alarms` WHERE player_id=? AND turnir_number=?"), playerRaw.ID, turnirNumberInt)
if err != nil {
c.Log.Error(err.Error())
}
message := r.formatRemindersMessageText(playerRaw)
keyboard := r.formatRemindersButtons(playerRaw)
messageUpdate := tgbotapi.NewEditMessageText(update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Message.MessageID, message)
messageUpdate.ParseMode = "Markdown"
messageUpdate.ReplyMarkup = &keyboard
c.Bot.Send(messageUpdate)
return "ok"
}