From 6cea84533e46ea26b92711f138360976047dfaed Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Mon, 15 Jan 2024 13:34:56 +0700 Subject: [PATCH] :sparkles: feat: added timestamp message pattern while sending to telegram bot #53 --- blueprint/blueprint.go | 8 ++++++++ blueprint/blueprint_config.go | 2 ++ blueprint/blueprint_model.go | 1 + example/govm_test.go | 26 ++++++++++++++++++++++++++ 4 files changed, 37 insertions(+) diff --git a/blueprint/blueprint.go b/blueprint/blueprint.go index 2e869b3..3e11614 100644 --- a/blueprint/blueprint.go +++ b/blueprint/blueprint.go @@ -5,13 +5,21 @@ import ( "html/template" "log" "net/url" + "time" "github.com/sivaosorg/govm/builder" + "github.com/sivaosorg/govm/timex" "github.com/sivaosorg/govm/utils" ) func NewCard() *card { c := &card{} + c.SetTimestamp(time.Now()) + return c +} + +func (c *card) SetTimestamp(value time.Time) *card { + c.Timestamp = value.Format(timex.TimeFormat20060102150405) return c } diff --git a/blueprint/blueprint_config.go b/blueprint/blueprint_config.go index 8c97fa1..7ecb3b8 100644 --- a/blueprint/blueprint_config.go +++ b/blueprint/blueprint_config.go @@ -25,6 +25,7 @@ var TypeIcons = map[IconText]string{ // CardDefault is the HTML template for the card var CardDefault = ` {{.Icon}} {{.Title}} + 📌 {{.Timestamp}} {{.Description}} {{if .ImageUrl}} @@ -37,6 +38,7 @@ var CardDefault = ` // CardMarkdownDefault is the Markdown template for the card var CardMarkdownDefault = ` **{{.Icon}} {{.Title}}** +**📌 {{.Timestamp}}** *{{.Description}}* {{if .ImageUrl}} ![Image]({{.ImageUrl}}) diff --git a/blueprint/blueprint_model.go b/blueprint/blueprint_model.go index a204dfb..dea63f5 100644 --- a/blueprint/blueprint_model.go +++ b/blueprint/blueprint_model.go @@ -3,6 +3,7 @@ package blueprint type IconText string type card struct { + Timestamp string `json:"timestamp"` Icon string `json:"icon"` IconText IconText `json:"icon_text,omitempty"` Title string `json:"title,omitempty"` diff --git a/example/govm_test.go b/example/govm_test.go index f7ec372..1f84f11 100644 --- a/example/govm_test.go +++ b/example/govm_test.go @@ -1 +1,27 @@ package example + +import ( + "testing" + + "github.com/sivaosorg/govm/bot/telegram" +) + +// 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) + svc := telegram.NewTelegramService(*telegram.GetTelegramConfigSample(). + SetChatId([]int64{-1002042977093}). + SetToken("6806983892:AAGcPZiuNktLFnyVWrRyOyYssECcVmNJSRo"). + SetDebugMode(false), + *options) + + return svc +} + +func TestCardNotification(t *testing.T) { + svc := createTelegramService() + svc.SendWarning("Kafka Stream", "Kafka Streams is a part of the Apache Kafka project that enables developers to build real-time processing applications, where data can be ingested, processed, and transformed in real-time as it flows through the Kafka cluster. Kafka Streams is a library for building scalable and fault-tolerant stream processing applications without the need for a separate processing cluster.") +}