1
0
Fork 0
hswro-alarm-bot/sender_worker.go

55 lines
883 B
Go
Raw Normal View History

2024-02-10 23:13:31 +00:00
package main
import (
2024-02-19 18:52:40 +00:00
"html/template"
2024-02-11 21:48:05 +00:00
"log"
2024-02-10 23:13:31 +00:00
"sync"
)
type Sender interface {
2024-02-19 18:52:40 +00:00
Send(msg GenericMessage, tpl *template.Template) error
2024-02-10 23:13:31 +00:00
}
2024-02-11 10:51:41 +00:00
type Sleeper interface {
Sleep(ch chan<- interface{})
}
type ChatId interface {
GetTgIds() *[]int64
}
type TgChatId struct {
tgChatId int64
}
func (self TgChatId) GetTgIds() *[]int64 {
return &[]int64{self.tgChatId}
}
2024-02-18 00:44:10 +00:00
func Consume(events <-chan GenericMessage) {
go func() {
for range events {
}
}()
}
2024-02-19 18:52:40 +00:00
func SendToTg(events <-chan GenericMessage, s Sender, wg *sync.WaitGroup, logger *log.Logger, tpl *template.Template) <-chan GenericMessage {
2024-02-18 00:44:10 +00:00
returnEvents := make(chan GenericMessage)
go func() {
wg.Add(1)
defer wg.Done()
for e := range events {
returnEvents <- e
2024-02-19 18:52:40 +00:00
err := s.Send(e, tpl)
2024-02-18 00:44:10 +00:00
if err != nil {
// TODO: handle it better
panic(err)
}
}
close(returnEvents)
}()
return returnEvents
}