1
0
Fork 0

Some TG-specific improvements with possible other methods as well

This commit is contained in:
Michał Rudowicz 2024-02-18 17:28:32 +01:00
parent 1e89fea274
commit 079e32e893
3 changed files with 45 additions and 15 deletions

25
main.go
View File

@ -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("<b>", self.ev.Type, "</b>, <i>index</i>:<b>", self.ev.Index, "</b>, value:<b>", self.ev.Value, "</b>")
}
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)),
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)

View File

@ -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

View File

@ -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"}}})
}