Skip to content

Commit b46b382

Browse files
committed
✨ feat: added timezone for message pattern telegram #53
1 parent 6cea845 commit b46b382

File tree

6 files changed

+35
-11
lines changed

6 files changed

+35
-11
lines changed

blueprint/blueprint.go

+14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ func NewCard() *card {
1818
return c
1919
}
2020

21+
func WithCard(timezone string) *card {
22+
if utils.IsEmpty(timezone) {
23+
timezone = timex.DefaultTimezoneVietnam
24+
}
25+
c := NewCard()
26+
c.SetTimestamp(timex.AdjustTimezone(time.Now(), timezone))
27+
return c
28+
}
29+
30+
func (c *card) SetTimezone(value string) *card {
31+
c.timezone = value
32+
return c
33+
}
34+
2135
func (c *card) SetTimestamp(value time.Time) *card {
2236
c.Timestamp = value.Format(timex.TimeFormat20060102150405)
2337
return c

blueprint/blueprint_model.go

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package blueprint
33
type IconText string
44

55
type card struct {
6+
timezone string
67
Timestamp string `json:"timestamp"`
78
Icon string `json:"icon"`
89
IconText IconText `json:"icon_text,omitempty"`

bot/telegram/telegram.go

+5
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ func (t *telegramOptionConfig) SetMaxRetries(value int) *telegramOptionConfig {
169169
return t
170170
}
171171

172+
func (t *telegramOptionConfig) SetTimezone(value string) *telegramOptionConfig {
173+
t.Timezone = value
174+
return t
175+
}
176+
172177
func (t *telegramOptionConfig) Json() string {
173178
return utils.ToJson(t)
174179
}

bot/telegram/telegram_model.go

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type TelegramConfig struct {
1313
}
1414

1515
type telegramOptionConfig struct {
16+
Timezone string `json:"timezone" yaml:"timezone"`
1617
Type TelegramFormatType `json:"type" binding:"required" yaml:"type"`
1718
MaxRetries int `json:"max_retries" yaml:"max-retries"`
1819
}

bot/telegram/telegram_service.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ type TelegramService interface {
4545
}
4646

4747
type telegramServiceImpl struct {
48-
config TelegramConfig `json:"-"`
49-
option telegramOptionConfig `json:"-"`
48+
config TelegramConfig
49+
option telegramOptionConfig
5050
}
5151

5252
func NewTelegramService(config TelegramConfig, option telegramOptionConfig) TelegramService {
@@ -280,42 +280,42 @@ func (s *telegramServiceImpl) SendMessageHandshake(request builder.MapBuilder) (
280280
}
281281

282282
func (s *telegramServiceImpl) SendNotification(topic, message string) (builder.MapBuilder, error) {
283-
b := blueprint.NewCard().SetIconText(blueprint.TypeNotification).SetDescription(message).SetTitle(topic)
283+
b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeNotification).SetDescription(message).SetTitle(topic)
284284
return s.SendMessage(b.GenCardDefault())
285285
}
286286

287287
func (s *telegramServiceImpl) SendInfo(topic, message string) (builder.MapBuilder, error) {
288-
b := blueprint.NewCard().SetIconText(blueprint.TypeInfo).SetDescription(message).SetTitle(topic)
288+
b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeInfo).SetDescription(message).SetTitle(topic)
289289
return s.SendMessage(b.GenCardDefault())
290290
}
291291

292292
func (s *telegramServiceImpl) SendWarning(topic, message string) (builder.MapBuilder, error) {
293-
b := blueprint.NewCard().SetIconText(blueprint.TypeWarning).SetDescription(message).SetTitle(topic)
293+
b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeWarning).SetDescription(message).SetTitle(topic)
294294
return s.SendMessage(b.GenCardDefault())
295295
}
296296

297297
func (s *telegramServiceImpl) SendError(topic, message string) (builder.MapBuilder, error) {
298-
b := blueprint.NewCard().SetIconText(blueprint.TypeError).SetDescription(message).SetTitle(topic)
298+
b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeError).SetDescription(message).SetTitle(topic)
299299
return s.SendMessage(b.GenCardDefault())
300300
}
301301

302302
func (s *telegramServiceImpl) SendDebug(topic, message string) (builder.MapBuilder, error) {
303-
b := blueprint.NewCard().SetIconText(blueprint.TypeDebug).SetDescription(message).SetTitle(topic)
303+
b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeDebug).SetDescription(message).SetTitle(topic)
304304
return s.SendMessage(b.GenCardDefault())
305305
}
306306

307307
func (s *telegramServiceImpl) SendSuccess(topic, message string) (builder.MapBuilder, error) {
308-
b := blueprint.NewCard().SetIconText(blueprint.TypeSuccess).SetDescription(message).SetTitle(topic)
308+
b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeSuccess).SetDescription(message).SetTitle(topic)
309309
return s.SendMessage(b.GenCardDefault())
310310
}
311311

312312
func (s *telegramServiceImpl) SendBug(topic, message string) (builder.MapBuilder, error) {
313-
b := blueprint.NewCard().SetIconText(blueprint.TypeBug).SetDescription(message).SetTitle(topic)
313+
b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeBug).SetDescription(message).SetTitle(topic)
314314
return s.SendMessage(b.GenCardDefault())
315315
}
316316

317317
func (s *telegramServiceImpl) SendTrace(topic, message string) (builder.MapBuilder, error) {
318-
b := blueprint.NewCard().SetIconText(blueprint.TypeTrace).SetDescription(message).SetTitle(topic)
318+
b := blueprint.WithCard(s.option.Timezone).SetIconText(blueprint.TypeTrace).SetDescription(message).SetTitle(topic)
319319
return s.SendMessage(b.GenCardDefault())
320320
}
321321

example/govm_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ import (
44
"testing"
55

66
"github.com/sivaosorg/govm/bot/telegram"
7+
"github.com/sivaosorg/govm/timex"
78
)
89

910
// bot: t.me/javis_notify_forum_bot
1011
// group: https://t.me/javis_forum_bot
1112
// chat_id: -1002042977093
1213
// token: 6806983892:AAGcPZiuNktLFnyVWrRyOyYssECcVmNJSRo
1314
func createTelegramService() telegram.TelegramService {
14-
options := telegram.NewTelegramOptionConfig().SetType(telegram.ModeHTML)
15+
options := telegram.NewTelegramOptionConfig().
16+
SetType(telegram.ModeHTML).
17+
SetTimezone(timex.DefaultTimezoneVietnam)
1518
svc := telegram.NewTelegramService(*telegram.GetTelegramConfigSample().
1619
SetChatId([]int64{-1002042977093}).
1720
SetToken("6806983892:AAGcPZiuNktLFnyVWrRyOyYssECcVmNJSRo").

0 commit comments

Comments
 (0)