diff --git a/blueprint/blueprint.go b/blueprint/blueprint.go index 3e11614..c0c28ae 100644 --- a/blueprint/blueprint.go +++ b/blueprint/blueprint.go @@ -18,6 +18,20 @@ func NewCard() *card { return c } +func WithCard(timezone string) *card { + if utils.IsEmpty(timezone) { + timezone = timex.DefaultTimezoneVietnam + } + c := NewCard() + c.SetTimestamp(timex.AdjustTimezone(time.Now(), timezone)) + return c +} + +func (c *card) SetTimezone(value string) *card { + c.timezone = value + return c +} + func (c *card) SetTimestamp(value time.Time) *card { c.Timestamp = value.Format(timex.TimeFormat20060102150405) return c diff --git a/blueprint/blueprint_model.go b/blueprint/blueprint_model.go index dea63f5..92c1d3e 100644 --- a/blueprint/blueprint_model.go +++ b/blueprint/blueprint_model.go @@ -3,6 +3,7 @@ package blueprint type IconText string type card struct { + timezone string Timestamp string `json:"timestamp"` Icon string `json:"icon"` IconText IconText `json:"icon_text,omitempty"` diff --git a/bot/telegram/telegram.go b/bot/telegram/telegram.go index f346278..7c65007 100644 --- a/bot/telegram/telegram.go +++ b/bot/telegram/telegram.go @@ -169,6 +169,11 @@ func (t *telegramOptionConfig) SetMaxRetries(value int) *telegramOptionConfig { return t } +func (t *telegramOptionConfig) SetTimezone(value string) *telegramOptionConfig { + t.Timezone = value + return t +} + func (t *telegramOptionConfig) Json() string { return utils.ToJson(t) } diff --git a/bot/telegram/telegram_model.go b/bot/telegram/telegram_model.go index 5683639..6a22b17 100644 --- a/bot/telegram/telegram_model.go +++ b/bot/telegram/telegram_model.go @@ -13,6 +13,7 @@ type TelegramConfig struct { } type telegramOptionConfig struct { + Timezone string `json:"timezone" yaml:"timezone"` Type TelegramFormatType `json:"type" binding:"required" yaml:"type"` MaxRetries int `json:"max_retries" yaml:"max-retries"` } diff --git a/bot/telegram/telegram_service.go b/bot/telegram/telegram_service.go index 563eca5..7f0fe2b 100644 --- a/bot/telegram/telegram_service.go +++ b/bot/telegram/telegram_service.go @@ -45,8 +45,8 @@ type TelegramService interface { } type telegramServiceImpl struct { - config TelegramConfig `json:"-"` - option telegramOptionConfig `json:"-"` + config TelegramConfig + option telegramOptionConfig } func NewTelegramService(config TelegramConfig, option telegramOptionConfig) TelegramService { @@ -280,42 +280,42 @@ func (s *telegramServiceImpl) SendMessageHandshake(request builder.MapBuilder) ( } func (s *telegramServiceImpl) SendNotification(topic, message string) (builder.MapBuilder, error) { - b := blueprint.NewCard().SetIconText(blueprint.TypeNotification).SetDescription(message).SetTitle(topic) + b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeNotification).SetDescription(message).SetTitle(topic) return s.SendMessage(b.GenCardDefault()) } func (s *telegramServiceImpl) SendInfo(topic, message string) (builder.MapBuilder, error) { - b := blueprint.NewCard().SetIconText(blueprint.TypeInfo).SetDescription(message).SetTitle(topic) + b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeInfo).SetDescription(message).SetTitle(topic) return s.SendMessage(b.GenCardDefault()) } func (s *telegramServiceImpl) SendWarning(topic, message string) (builder.MapBuilder, error) { - b := blueprint.NewCard().SetIconText(blueprint.TypeWarning).SetDescription(message).SetTitle(topic) + b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeWarning).SetDescription(message).SetTitle(topic) return s.SendMessage(b.GenCardDefault()) } func (s *telegramServiceImpl) SendError(topic, message string) (builder.MapBuilder, error) { - b := blueprint.NewCard().SetIconText(blueprint.TypeError).SetDescription(message).SetTitle(topic) + b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeError).SetDescription(message).SetTitle(topic) return s.SendMessage(b.GenCardDefault()) } func (s *telegramServiceImpl) SendDebug(topic, message string) (builder.MapBuilder, error) { - b := blueprint.NewCard().SetIconText(blueprint.TypeDebug).SetDescription(message).SetTitle(topic) + b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeDebug).SetDescription(message).SetTitle(topic) return s.SendMessage(b.GenCardDefault()) } func (s *telegramServiceImpl) SendSuccess(topic, message string) (builder.MapBuilder, error) { - b := blueprint.NewCard().SetIconText(blueprint.TypeSuccess).SetDescription(message).SetTitle(topic) + b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeSuccess).SetDescription(message).SetTitle(topic) return s.SendMessage(b.GenCardDefault()) } func (s *telegramServiceImpl) SendBug(topic, message string) (builder.MapBuilder, error) { - b := blueprint.NewCard().SetIconText(blueprint.TypeBug).SetDescription(message).SetTitle(topic) + b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeBug).SetDescription(message).SetTitle(topic) return s.SendMessage(b.GenCardDefault()) } func (s *telegramServiceImpl) SendTrace(topic, message string) (builder.MapBuilder, error) { - b := blueprint.NewCard().SetIconText(blueprint.TypeTrace).SetDescription(message).SetTitle(topic) + b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeTrace).SetDescription(message).SetTitle(topic) return s.SendMessage(b.GenCardDefault()) } diff --git a/example/govm_test.go b/example/govm_test.go index 1f84f11..449a954 100644 --- a/example/govm_test.go +++ b/example/govm_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/sivaosorg/govm/bot/telegram" + "github.com/sivaosorg/govm/timex" ) // bot: t.me/javis_notify_forum_bot @@ -11,7 +12,9 @@ import ( // chat_id: -1002042977093 // token: 6806983892:AAGcPZiuNktLFnyVWrRyOyYssECcVmNJSRo func createTelegramService() telegram.TelegramService { - options := telegram.NewTelegramOptionConfig().SetType(telegram.ModeHTML) + options := telegram.NewTelegramOptionConfig(). + SetType(telegram.ModeHTML). + SetTimezone(timex.DefaultTimezoneVietnam) svc := telegram.NewTelegramService(*telegram.GetTelegramConfigSample(). SetChatId([]int64{-1002042977093}). SetToken("6806983892:AAGcPZiuNktLFnyVWrRyOyYssECcVmNJSRo").