feat: publish standalone worker
Separate worker packaging and service lifecycle from the control plane.
Этот коммит содержится в:
77
app/models/telegram_bot.go
Обычный файл
77
app/models/telegram_bot.go
Обычный файл
@@ -0,0 +1,77 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"rsgit.ru/rsmon/rsmon/app/models/concerns"
|
||||
)
|
||||
|
||||
const (
|
||||
// TelegramBotMessageReceived marks inbound bot messages.
|
||||
TelegramBotMessageReceived = "received"
|
||||
// TelegramBotMessageSent marks outbound bot replies.
|
||||
TelegramBotMessageSent = "sent"
|
||||
// TelegramBotStatusMain is the singleton status row name for the bot.
|
||||
TelegramBotStatusMain = "main"
|
||||
)
|
||||
|
||||
// TelegramBotMessage stores a Telegram bot chat message for admin history.
|
||||
type TelegramBotMessage struct {
|
||||
concerns.Model
|
||||
|
||||
Direction string `gorm:"size:16;index" json:"direction"`
|
||||
ChatID int64 `gorm:"index" json:"chat_id"`
|
||||
ChatType string `gorm:"size:32" json:"chat_type"`
|
||||
Username string `gorm:"size:255" json:"username"`
|
||||
Text string `gorm:"type:text" json:"text"`
|
||||
Command string `gorm:"size:64" json:"command"`
|
||||
ContactID *int64 `gorm:"index" json:"contact_id,omitempty"`
|
||||
Error string `gorm:"type:text" json:"error,omitempty"`
|
||||
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// TableName overrides the default table name.
|
||||
func (TelegramBotMessage) TableName() string {
|
||||
return "telegram_bot_messages"
|
||||
}
|
||||
|
||||
// TelegramBotStatus stores the current Telegram bot heartbeat/status.
|
||||
type TelegramBotStatus struct {
|
||||
concerns.Model
|
||||
|
||||
Name string `gorm:"uniqueIndex;size:64;not null" json:"name"`
|
||||
Username string `gorm:"size:255" json:"username"`
|
||||
Online bool `gorm:"not null;default:false" json:"online"`
|
||||
LastSeen *time.Time `json:"last_seen,omitempty"`
|
||||
LastError string `gorm:"type:text" json:"last_error,omitempty"`
|
||||
|
||||
concerns.Timestamped
|
||||
}
|
||||
|
||||
// TableName overrides the default table name.
|
||||
func (TelegramBotStatus) TableName() string {
|
||||
return "telegram_bot_statuses"
|
||||
}
|
||||
|
||||
// RecentTelegramBotMessages returns the latest Telegram bot messages, capped to a safe limit.
|
||||
func RecentTelegramBotMessages(limit int) ([]TelegramBotMessage, error) {
|
||||
if limit <= 0 || limit > 500 {
|
||||
limit = 100
|
||||
}
|
||||
messages := []TelegramBotMessage{}
|
||||
err := DB().Order("id DESC").Limit(limit).Find(&messages).Error
|
||||
return messages, err
|
||||
}
|
||||
|
||||
// TelegramBotCurrentStatus returns the singleton Telegram bot status row.
|
||||
func TelegramBotCurrentStatus() (*TelegramBotStatus, error) {
|
||||
status := TelegramBotStatus{}
|
||||
if err := DB().Where("name = ?", TelegramBotStatusMain).First(&status).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if status.LastSeen == nil || time.Since(*status.LastSeen) > 2*time.Minute {
|
||||
status.Online = false
|
||||
}
|
||||
return &status, nil
|
||||
}
|
||||
Ссылка в новой задаче
Block a user