Skip to content

Commit

Permalink
✨ feat: added timezone for message pattern telegram #53
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jan 15, 2024
1 parent 6cea845 commit b46b382
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 11 deletions.
14 changes: 14 additions & 0 deletions blueprint/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions blueprint/blueprint_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
5 changes: 5 additions & 0 deletions bot/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions bot/telegram/telegram_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
20 changes: 10 additions & 10 deletions bot/telegram/telegram_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}

Expand Down
5 changes: 4 additions & 1 deletion example/govm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import (
"testing"

"github.com/sivaosorg/govm/bot/telegram"
"github.com/sivaosorg/govm/timex"
)

// bot: t.me/javis_notify_forum_bot
// group: https://t.me/javis_forum_bot
// 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").
Expand Down

0 comments on commit b46b382

Please sign in to comment.