2025-01-09 20:53:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2025-01-12 10:06:13 +00:00
|
|
|
"html/template"
|
2025-01-09 20:53:58 +00:00
|
|
|
"log"
|
|
|
|
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
|
|
)
|
|
|
|
|
2025-01-12 10:06:13 +00:00
|
|
|
func getCurrentStatus(dataStore *DataStore, logger *log.Logger, config AppConfig, tpl *template.Template, s SatelNameGetter) string {
|
|
|
|
elements := FilterBasicElementsByTypeOrIndex(dataStore.GetBasicEventElements(), config.AllowedTypes, config.AllowedIndexes)
|
|
|
|
msg := GenericMessage{elements}
|
|
|
|
return msg.Format(tpl, s, logger)
|
2025-01-09 20:53:58 +00:00
|
|
|
}
|
|
|
|
|
2025-01-13 19:37:50 +00:00
|
|
|
func HandleTelegramCommands(bot TelegramBot, logger *log.Logger, dataStore *DataStore, config AppConfig, tpl *template.Template, s SatelNameGetter) {
|
2025-01-09 20:53:58 +00:00
|
|
|
u := tgbotapi.NewUpdate(0)
|
|
|
|
u.Timeout = 60
|
|
|
|
|
|
|
|
updates := bot.GetUpdatesChan(u)
|
|
|
|
|
|
|
|
for update := range updates {
|
|
|
|
if update.Message == nil { // ignore any non-Message updates
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !update.Message.IsCommand() { // ignore any non-command Messages
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new MessageConfig. We don't have text yet,
|
|
|
|
// so we leave it empty.
|
|
|
|
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
|
|
|
|
|
|
|
|
// Extract the command from the Message.
|
|
|
|
switch update.Message.Command() {
|
|
|
|
case "status":
|
2025-01-12 10:06:13 +00:00
|
|
|
msg.Text = getCurrentStatus(dataStore, logger, config, tpl, s)
|
2025-01-09 20:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := bot.Send(msg); err != nil {
|
|
|
|
logger.Print(err)
|
|
|
|
}
|
|
|
|
}
|
2025-01-13 19:37:50 +00:00
|
|
|
|
|
|
|
logger.Println("Stopping command handler")
|
2025-01-09 20:53:58 +00:00
|
|
|
}
|