Compare commits
6 Commits
cdcef69618
...
ba5e74d3c6
| Author | SHA1 | Date |
|---|---|---|
|
|
ba5e74d3c6 | |
|
|
e0fe26c41b | |
|
|
803a174ace | |
|
|
f6ff93c783 | |
|
|
0e59be7421 | |
|
|
d03bfb9896 |
|
|
@ -0,0 +1,11 @@
|
|||
FROM debian:12-slim
|
||||
RUN apt update && apt install golang -y --no-install-recommends && apt install ca-certificates -y
|
||||
RUN mkdir /src && mkdir /conf
|
||||
COPY . /src
|
||||
RUN cd /src && go build
|
||||
|
||||
FROM debian:12-slim
|
||||
COPY --from=0 /conf /conf
|
||||
COPY --from=0 /src/alarm_bot /
|
||||
WORKDIR /conf
|
||||
CMD ["/alarm_bot"]
|
||||
|
|
@ -10,7 +10,7 @@ In other words - treat it as a toy, not as a tool that will save your life or va
|
|||
## Running
|
||||
|
||||
```
|
||||
$ TELEGRAM_APITOKEN=YOUR_API_TOKEN ./alarm-bot
|
||||
$ ./alarm-bot
|
||||
```
|
||||
|
||||
Remember that `hswro-alarm-bot.yml` should be present in the current directory.
|
||||
|
|
@ -32,6 +32,7 @@ allowed-indexes:
|
|||
- 5678
|
||||
- 1337
|
||||
pool-interval: 5m
|
||||
telegram-api-key: "telegram api key"
|
||||
arm-callback-urls:
|
||||
- "http://192.168.1.10/hello"
|
||||
- "http://example.com/api"
|
||||
|
|
@ -51,6 +52,7 @@ matterbridge:
|
|||
- `pool-interval` sets how often will the Satel device be asked for changes
|
||||
- `satel-addr` sets the IP address of the Satel device
|
||||
- `tg-chat-ids` sets which telegram groups will receive notifications about alarm state changes
|
||||
- `telegram-api-key` sets the Bot API key for Telegram that was obtained from BotFather
|
||||
|
||||
### Matterbridge integration
|
||||
|
||||
|
|
@ -132,7 +134,6 @@ After=network.target
|
|||
[Service]
|
||||
Type=simple
|
||||
ExecStart=/path/to/alarm_bot
|
||||
Environment=TELEGRAM_APITOKEN=YOUR_API_TOKEN
|
||||
DynamicUser=True
|
||||
RuntimeDirectory=hswro-alarm-bot
|
||||
StateDirectory=hswro-alarm-bot
|
||||
|
|
@ -149,7 +150,3 @@ MemoryMax=50M
|
|||
WantedBy=multi-user.target
|
||||
|
||||
```
|
||||
|
||||
## Debugging
|
||||
|
||||
Set the `OMIT_TG` environment variable to, well, omit sending anything over to Telegram and just see the logs instead.
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ type AppConfig struct {
|
|||
AlarmCallbackUrls []string `yaml:"alarm-callback-urls"`
|
||||
WriteMemoryProfile bool `yaml:"write-memory-profile"`
|
||||
Matterbridge []MatterbridgeConfig `yaml:"matterbridge"`
|
||||
TelegramApiKey string `yaml:"telegram-api-key"`
|
||||
}
|
||||
|
||||
func (m *SatelChangeType) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ disarm-callback-urls:
|
|||
alarm-callback-urls:
|
||||
- "test alarm callback url"
|
||||
- "second test alarm callback url"
|
||||
telegram-api-key: "test api key"
|
||||
matterbridge:
|
||||
- uri: test_uri_1
|
||||
token: test_token_1
|
||||
|
|
@ -56,6 +57,7 @@ func TestParseYamlConfig(t *testing.T) {
|
|||
a.ElementsMatch([]string{"test arm callback url", "second test arm callback url"}, actualConfig.ArmCallbackUrls)
|
||||
a.ElementsMatch([]string{"test disarm callback url", "second test disarm callback url"}, actualConfig.DisarmCallbackUrls)
|
||||
a.ElementsMatch([]string{"test alarm callback url", "second test alarm callback url"}, actualConfig.AlarmCallbackUrls)
|
||||
a.Equal("test api key", actualConfig.TelegramApiKey)
|
||||
|
||||
a.Equal(actualConfig.Matterbridge[0].URI, "test_uri_1")
|
||||
a.Equal(actualConfig.Matterbridge[0].Token, "test_token_1")
|
||||
|
|
|
|||
8
main.go
8
main.go
|
|
@ -61,11 +61,17 @@ func main() {
|
|||
s := makeSatel(config.SatelAddr, config.PoolInterval.GetDuration())
|
||||
logger.Printf("Connected to Satel: %s", config.SatelAddr)
|
||||
|
||||
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
|
||||
var bot TelegramBotSender = nil
|
||||
if len(config.TelegramApiKey) != 0 {
|
||||
b, err := tgbotapi.NewBotAPI(config.TelegramApiKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
logger.Print("Created Telegram Bot API client")
|
||||
bot = b
|
||||
} else {
|
||||
bot = EmptySender{}
|
||||
}
|
||||
|
||||
tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), config.ChatIds}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,12 +8,14 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.sr.ht/~michalr/go-satel"
|
||||
)
|
||||
|
||||
const (
|
||||
NotificationPartitionIndex = 0
|
||||
httpTimeout = 1 * time.Second
|
||||
)
|
||||
|
||||
type Sender interface {
|
||||
|
|
@ -61,7 +63,10 @@ func doHttpNotification(url string, logger *log.Logger, wg *sync.WaitGroup) {
|
|||
}
|
||||
req, err := http.NewRequest(http.MethodPost, url, nil)
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
client := http.Client{
|
||||
Timeout: httpTimeout,
|
||||
}
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
logger.Print("Could not POST ", url, ": ", err)
|
||||
return
|
||||
|
|
@ -139,7 +144,10 @@ func SendToMatterbridge(events <-chan GenericMessage, s SatelNameGetter, config
|
|||
}
|
||||
req, err := http.NewRequest(http.MethodPost, matterbridgeConfig.URI, bytes.NewBuffer(body))
|
||||
req.Header["Authorization"] = []string{fmt.Sprint("Bearer ", matterbridgeConfig.Token)}
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
client := http.Client{
|
||||
Timeout: httpTimeout,
|
||||
}
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
logger.Print("Could not POST ", matterbridgeConfig.URI, ": ", err)
|
||||
return
|
||||
|
|
|
|||
|
|
@ -11,6 +11,12 @@ type TelegramBotSender interface {
|
|||
Send(c tgbotapi.Chattable) (tgbotapi.Message, error)
|
||||
}
|
||||
|
||||
type EmptySender struct{}
|
||||
|
||||
func (self EmptySender) Send(_ tgbotapi.Chattable) (tgbotapi.Message, error) {
|
||||
return tgbotapi.Message{}, nil
|
||||
}
|
||||
|
||||
type TgSender struct {
|
||||
bot TelegramBotSender
|
||||
s SatelNameGetter
|
||||
|
|
|
|||
Loading…
Reference in New Issue