1
0
Fork 0
hswro-alarm-bot/command_handler.go

55 lines
1.4 KiB
Go
Raw Permalink Normal View History

package main
import (
"html/template"
"log"
"sync"
"git.sr.ht/~michalr/go-satel"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func handleStatusCommand(dataStore *DataStore, msg tgbotapi.MessageConfig, bot *tgbotapi.BotAPI, tpl *template.Template,
s SatelNameGetter, logger *log.Logger) {
data := dataStore.GetGenericMessageOfAllowedPartitions(satel.ArmedPartition)
msg.Text = data.Format(tpl, s, logger)
if _, err := bot.Send(msg); err != nil {
log.Panic(err)
}
}
func HandleIncomingCommands(dataStore *DataStore, tgChan tgbotapi.UpdatesChannel, bot *tgbotapi.BotAPI, wg *sync.WaitGroup,
tpl *template.Template, s SatelNameGetter, quitChan <-chan interface{}, logger *log.Logger) {
wg.Add(1)
defer wg.Done()
for {
select {
case update, ok := <-tgChan:
if ok == false { // quit when tgChan closes
return
}
if update.Message == nil { // ignore any non-Message updates
continue
}
if !update.Message.IsCommand() { // ignore any non-command Messages
continue
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
msg.ReplyToMessageID = update.Message.MessageID
switch update.Message.Command() {
case "status":
handleStatusCommand(dataStore, msg, bot, tpl, s, logger)
default:
logger.Print("Received an unknown command: ", update.Message.Command())
}
case <-quitChan:
return
}
}
}