From 079e32e89314e18a2f5abfc0730b746bb0dc52aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Sun, 18 Feb 2024 17:28:32 +0100 Subject: [PATCH] Some TG-specific improvements with possible other methods as well --- main.go | 27 +++++++++++++++++++++------ sender_worker.go | 8 ++++++-- sender_worker_test.go | 25 ++++++++++++++++++------- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 0263e09..974aea6 100644 --- a/main.go +++ b/main.go @@ -30,12 +30,13 @@ func (self TgSender) Send(msg GenericMessage) error { } b := strings.Builder{} for _, msg := range msg.msgs { - b.WriteString(msg.String()) + b.WriteString(msg.TgString()) b.WriteRune('\n') } message := b.String() for _, chatId := range *chatIds { toSend := tgbotapi.NewMessage(chatId, message) + toSend.ParseMode = "HTML" _, err := self.bot.Send(toSend) if err != nil { return err @@ -44,14 +45,22 @@ func (self TgSender) Send(msg GenericMessage) error { return nil } -func sendTgMessage(tgEvents chan GenericMessage, msg string, chatId int64) { - tgEvents <- GenericMessage{TgChatId{chatId}, []MsgContent{{msg}}} +func sendTgMessage(tgEvents chan GenericMessage, msg MsgContent, chatId int64) { + tgEvents <- GenericMessage{TgChatId{chatId}, []MsgContent{msg}} } type RealSleeper struct { duration time.Duration } +type SatelMsgContent struct { + ev satel.Event +} + +func (self SatelMsgContent) TgString() string { + return fmt.Sprint("", self.ev.Type, ", index:", self.ev.Index, ", value:", self.ev.Value, "") +} + func (self RealSleeper) Sleep(ch chan<- interface{}) { go func() { time.Sleep(self.duration) @@ -110,11 +119,17 @@ func main() { logger.Print("Created Telegram Bot API client") tgSender := TgSender{bot} - Consume(SendToTg(tgSenderWorker(tgEvents, &wg, sleeper, log.New(os.Stderr, "TgSender", log.Lmicroseconds)), - tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds))) + + Consume( + SendToTg( + tgSenderWorker(tgEvents, &wg, sleeper, log.New(os.Stderr, "TgSender", log.Lmicroseconds)), + tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds))) + for e := range FilterByLastSeen(s.Events, "hs_wro_last_seen.bin", log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)) { logger.Print("Received change from SATEL: ", e) - sendTgMessage(tgEvents, fmt.Sprint("Change from SATEL: type:", e.Type, ", index:", e.Index, ", value:", e.Value), chatIds[0]) + for _, chatId := range chatIds { + sendTgMessage(tgEvents, SatelMsgContent{e}, chatId) + } } close(tgEvents) diff --git a/sender_worker.go b/sender_worker.go index 1f16586..af41ce3 100644 --- a/sender_worker.go +++ b/sender_worker.go @@ -32,11 +32,15 @@ func Consume(events <-chan GenericMessage) { }() } -type MsgContent struct { +type MsgContent interface { + TgString() string +} + +type StringMsgContent struct { msg string } -func (self MsgContent) String() string { return self.msg } +func (self StringMsgContent) TgString() string { return self.msg } type GenericMessage struct { chatIds ChatId diff --git a/sender_worker_test.go b/sender_worker_test.go index eff800f..c7922f1 100644 --- a/sender_worker_test.go +++ b/sender_worker_test.go @@ -30,6 +30,14 @@ func (self *MockSleeper) Sleep(ch chan<- interface{}) { self.callCount += 1 } +type FakeChatId struct { + FakeId int64 +} + +func (self FakeChatId) GetTgIds() *[]int64 { + return nil +} + func TestMessageThrottling(t *testing.T) { testEvents := make(chan GenericMessage) wg := sync.WaitGroup{} @@ -37,18 +45,21 @@ func TestMessageThrottling(t *testing.T) { mockSleeper := MockSleeper{nil, 0} Consume(SendToTg(tgSenderWorker(testEvents, &wg, &mockSleeper, log.New(io.Discard, "", log.Ltime)), &mockSender, &wg, log.New(io.Discard, "", log.Ltime))) - testEvents <- GenericMessage{TgChatId{123}, []MsgContent{{"test1"}}} - testEvents <- GenericMessage{TgChatId{124}, []MsgContent{{"test3"}}} - testEvents <- GenericMessage{TgChatId{123}, []MsgContent{{"test2"}}} - testEvents <- GenericMessage{TgChatId{124}, []MsgContent{{"test4"}}} + testEvents <- GenericMessage{TgChatId{123}, []MsgContent{StringMsgContent{"test1"}}} + testEvents <- GenericMessage{TgChatId{124}, []MsgContent{StringMsgContent{"test3"}}} + testEvents <- GenericMessage{TgChatId{123}, []MsgContent{StringMsgContent{"test2"}}} + testEvents <- GenericMessage{TgChatId{124}, []MsgContent{StringMsgContent{"test4"}}} + testEvents <- GenericMessage{FakeChatId{123}, []MsgContent{StringMsgContent{"testFake"}}} assert.Equal(t, 1, mockSleeper.callCount) *mockSleeper.ch <- nil assert.Equal(t, 1, mockSleeper.callCount) - testEvents <- GenericMessage{TgChatId{123}, []MsgContent{{"test5"}}} + testEvents <- GenericMessage{TgChatId{123}, []MsgContent{StringMsgContent{"test5"}}} close(testEvents) wg.Wait() assert.Equal(t, 2, mockSleeper.callCount) - assert.Len(t, mockSender.messages, 3) - assert.Contains(t, mockSender.messages, GenericMessage{TgChatId{123}, []MsgContent{{"test1"}, {"test2"}}}) + assert.Len(t, mockSender.messages, 4) + assert.Contains(t, mockSender.messages, GenericMessage{TgChatId{123}, []MsgContent{StringMsgContent{"test1"}, StringMsgContent{"test2"}}}) + assert.Contains(t, mockSender.messages, GenericMessage{TgChatId{124}, []MsgContent{StringMsgContent{"test3"}, StringMsgContent{"test4"}}}) + assert.Contains(t, mockSender.messages, GenericMessage{FakeChatId{123}, []MsgContent{StringMsgContent{"testFake"}}}) }