1
0
Fork 0

(hopefully) fix not all chatIds being used

This commit is contained in:
Michał Rudowicz 2024-03-10 21:15:51 +01:00
parent 0c873ff77d
commit 7815009318
6 changed files with 15 additions and 30 deletions

View File

@ -89,7 +89,7 @@ func FilterByLastSeen(ev <-chan satel.Event, wg *sync.WaitGroup, dataStore *Data
func appendToGenericMessage(msg *GenericMessage, new *GenericMessage) *GenericMessage { func appendToGenericMessage(msg *GenericMessage, new *GenericMessage) *GenericMessage {
if msg == nil { if msg == nil {
msg = &GenericMessage{new.ChatIds, make([]satel.BasicEventElement, 0)} msg = &GenericMessage{make([]satel.BasicEventElement, 0)}
} }
throughNewMessages: throughNewMessages:

View File

@ -251,12 +251,12 @@ func TestThrottle(t *testing.T) {
wg.Done() wg.Done()
}() }()
testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest1}} testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest1}}
testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest2}} testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest2}}
testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest3}} testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest3}}
*mockSleeper.ch <- nil *mockSleeper.ch <- nil
testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest4}} testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest4}}
close(testEvents) close(testEvents)
wg.Wait() wg.Wait()
@ -272,7 +272,7 @@ func TestThrottle(t *testing.T) {
} }
func makeMassiveEvent(element satel.BasicEventElement, numElements int) GenericMessage { func makeMassiveEvent(element satel.BasicEventElement, numElements int) GenericMessage {
retval := GenericMessage{TgChatId{123}, make([]satel.BasicEventElement, 0)} retval := GenericMessage{make([]satel.BasicEventElement, 0)}
for i := 0; i < numElements; i++ { for i := 0; i < numElements; i++ {
retval.Messages = append(retval.Messages, element) retval.Messages = append(retval.Messages, element)

16
main.go
View File

@ -22,18 +22,18 @@ const (
) )
type TgSender struct { type TgSender struct {
bot *tgbotapi.BotAPI bot *tgbotapi.BotAPI
s SatelNameGetter s SatelNameGetter
logger *log.Logger logger *log.Logger
chatIds []int64
} }
func (self TgSender) Send(msg GenericMessage, tpl *template.Template) error { func (self TgSender) Send(msg GenericMessage, tpl *template.Template) error {
chatIds := msg.ChatIds.GetTgIds() if len(self.chatIds) == 0 {
if chatIds == nil {
return nil return nil
} }
message := msg.Format(tpl, self.s, self.logger) message := msg.Format(tpl, self.s, self.logger)
for _, chatId := range *chatIds { for _, chatId := range self.chatIds {
toSend := tgbotapi.NewMessage(chatId, message) toSend := tgbotapi.NewMessage(chatId, message)
toSend.ParseMode = "HTML" toSend.ParseMode = "HTML"
_, err := self.bot.Send(toSend) _, err := self.bot.Send(toSend)
@ -45,7 +45,7 @@ func (self TgSender) Send(msg GenericMessage, tpl *template.Template) error {
} }
func sendTgMessage(tgEvents chan GenericMessage, msg []satel.BasicEventElement, chatId int64) { func sendTgMessage(tgEvents chan GenericMessage, msg []satel.BasicEventElement, chatId int64) {
tgEvents <- GenericMessage{TgChatId{chatId}, msg} tgEvents <- GenericMessage{msg}
} }
type RealSleeper struct { type RealSleeper struct {
@ -144,7 +144,7 @@ func main() {
} }
logger.Print("Created Telegram Bot API client") logger.Print("Created Telegram Bot API client")
tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds)} tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), chatIds}
tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate)) tpl := template.Must(template.New("TelegramMessage").Parse(TelegramMessageTemplate))

View File

@ -32,12 +32,10 @@ type SatelMsgContent struct {
} }
type GenericMessage struct { type GenericMessage struct {
ChatIds ChatId
Messages []satel.BasicEventElement Messages []satel.BasicEventElement
} }
type SatelMessage struct { type SatelMessage struct {
ChatIds ChatId
Messages []SatelMsgContent Messages []SatelMsgContent
} }
@ -66,7 +64,6 @@ func (self SatelMsgContent) GetName() string {
func (self GenericMessage) Format(template *template.Template, s SatelNameGetter, logger *log.Logger) string { func (self GenericMessage) Format(template *template.Template, s SatelNameGetter, logger *log.Logger) string {
b := strings.Builder{} b := strings.Builder{}
template.Execute(&b, SatelMessage{ template.Execute(&b, SatelMessage{
self.ChatIds,
getSatelMessageContent(self.Messages, s, logger), getSatelMessageContent(self.Messages, s, logger),
}) })
return b.String() return b.String()

View File

@ -22,18 +22,6 @@ type Sleeper interface {
Sleep(ch chan<- interface{}) Sleep(ch chan<- interface{})
} }
type ChatId interface {
GetTgIds() *[]int64
}
type TgChatId struct {
tgChatId int64
}
func (self TgChatId) GetTgIds() *[]int64 {
return &[]int64{self.tgChatId}
}
func Consume(events <-chan GenericMessage) { func Consume(events <-chan GenericMessage) {
go func() { go func() {
for range events { for range events {

View File

@ -47,7 +47,7 @@ func TestTelegramTemplate(t *testing.T) {
tpl, err := template.New("TestTemplate").Parse(TelegramMessageTemplate) tpl, err := template.New("TestTemplate").Parse(TelegramMessageTemplate)
assert.NoError(t, err) assert.NoError(t, err)
Consume(SendToTg(testEvents, &mockSender, &wg, log.New(io.Discard, "", log.Ltime), tpl)) Consume(SendToTg(testEvents, &mockSender, &wg, log.New(io.Discard, "", log.Ltime), tpl))
testEvents <- GenericMessage{TgChatId{123}, []satel.BasicEventElement{tplMessageTest1, tplMessageTest2}} testEvents <- GenericMessage{[]satel.BasicEventElement{tplMessageTest1, tplMessageTest2}}
close(testEvents) close(testEvents)
wg.Wait() wg.Wait()