first commit
This commit is contained in:
commit
7a2e0d1caf
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.pkg.tar.zst
|
||||
22
LICENSE
Normal file
22
LICENSE
Normal file
@ -0,0 +1,22 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 Lucas Schumacher
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
39
PKGBUILD
Normal file
39
PKGBUILD
Normal file
@ -0,0 +1,39 @@
|
||||
# simple PKGBUILD for packaging locally
|
||||
pkgname=telegramAlert
|
||||
pkgver=0.0.0
|
||||
pkgrel=0
|
||||
pkgdesc="Server that listens for alerts and forwards them to a telegram user"
|
||||
arch=(x86_64 i686 armv5 armv6h armv7h aarch64)
|
||||
license=("MIT")
|
||||
backup=(
|
||||
etc/$pkgname/$pkgname.env #Don't overwrite config file
|
||||
)
|
||||
depends=()
|
||||
makedepends=("go")
|
||||
source=(
|
||||
'main.go'
|
||||
'go.mod'
|
||||
'telegramAlert.env'
|
||||
'telegramAlert.service'
|
||||
'telegramAlert.sysusers'
|
||||
'LICENSE'
|
||||
)
|
||||
sha256sums=(
|
||||
'f2d19e3b2ed8244d2f539a4d9485961b5ed0fb14a29c3094387cc97124e5b571' #main.go
|
||||
'6d215015f4084363c15c7710b406f7981d759913b88a22060e8c8ad95a3eb18a' #go.mod
|
||||
'8ab2d17885a3dee972db1e74d9371cb9b35cc47fbb8c2d25968a46f3dc351002' #telegramAlert.env
|
||||
'6aedcf80266e272b54cf8385bf4fa9f125ab8aadc92ad1775207e55521226c7b' #telegramAlert.service
|
||||
'13db81008358f744fe84ca20a1f27e46dd11614ff847976a3aa2c95672cf2e02' #telegramAlert.sysusers
|
||||
'5dd6c227cb4a3821fd8d0ddea2b84ceb50f370b121f5e9fbd0986325948aaa78' #LICENSE
|
||||
)
|
||||
|
||||
build() {
|
||||
go build -trimpath
|
||||
}
|
||||
package() {
|
||||
install -D -m555 "$pkgname" "$pkgdir/usr/bin/$pkgname"
|
||||
install -D -m644 "$pkgname.service" "$pkgdir/usr/lib/systemd/system/$pkgname.service"
|
||||
install -D -m644 "$pkgname.sysusers" "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
|
||||
install -D -m600 "$pkgname.env" "$pkgdir/etc/$pkgname/$pkgname.env"
|
||||
install -D -m644 "LICENSE" "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
|
||||
}
|
||||
2
README.md
Normal file
2
README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# telegramAlert
|
||||
Telegram bot that forwards InfluxDB Alerts to a telegream user account
|
||||
196
main.go
Normal file
196
main.go
Normal file
@ -0,0 +1,196 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
var KEY string = os.Getenv("TELEGRAM_TOKEN")
|
||||
var UID string = os.Getenv("TELEGRAM_USERID")
|
||||
var VERBOSE bool = os.Getenv("VERBOSE") != ""
|
||||
var BASE_URL string = "https://api.telegram.org/bot" + KEY
|
||||
|
||||
type InfluxAlert struct {
|
||||
CheckID string `json:"_check_id"`
|
||||
CheckName string `json:"_check_name"`
|
||||
Level string `json:"_level"`
|
||||
Measurement string `json:"_measurement"`
|
||||
Message string `json:"_message"`
|
||||
NotificationEndpointID string `json:"_notification_endpoint_id"`
|
||||
NotificationEndpointName string `json:"_notification_endpoint_name"`
|
||||
NotificationRuleID string `json:"_notification_rule_id"`
|
||||
NotificationRuleName string `json:"_notification_rule_name"`
|
||||
SourceMeasurement string `json:"_source_measurement"`
|
||||
SourceTimestamp float64 `json:"_source_timestamp"`
|
||||
Start string `json:"_start"`
|
||||
StatusTimestamp float64 `json:"_status_timestamp"`
|
||||
Stop string `json:"_stop"`
|
||||
Time string `json:"_time"`
|
||||
Type string `json:"_type"`
|
||||
Version float64 `json:"_version"`
|
||||
}
|
||||
type TelegramMessage struct {
|
||||
ChatID string `json:"chat_id"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
dat, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, "Error reading request", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
incoming := InfluxAlert{}
|
||||
err = json.Unmarshal(dat, &incoming)
|
||||
if err != nil {
|
||||
fmt.Println("Got Error unmarshaling")
|
||||
fmt.Printf("Error: %T\n", err)
|
||||
fmt.Println(err)
|
||||
//http.Error
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(nil)
|
||||
if VERBOSE {
|
||||
fmt.Println(string(dat))
|
||||
// parsed, err := json.MarshalIndent(incoming, "", " ")
|
||||
// if err == nil {
|
||||
// fmt.Println(string(parsed))
|
||||
// }
|
||||
}
|
||||
fmt.Print(incoming.Time + ": ")
|
||||
fmt.Println(incoming.Message)
|
||||
|
||||
// KEY := os.Getenv("TELEGRAM_TOKEN")
|
||||
// if KEY == "" {
|
||||
// fmt.Println("Error: Can not send message - no bot token set")
|
||||
// return
|
||||
// }
|
||||
// UID := os.Getenv("TELEGRAM_USERID")
|
||||
// if UID == "" {
|
||||
// fmt.Println("Error: Can not send message - no recepient user id set")
|
||||
// return
|
||||
// }
|
||||
|
||||
URL := BASE_URL + "/sendMessage"
|
||||
message := TelegramMessage{UID, incoming.Message}
|
||||
outgoing, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
fmt.Println("Error marshaling telegram json")
|
||||
return
|
||||
}
|
||||
|
||||
//http.Post(URL, "application/json", json.Marshal(outgoing))
|
||||
req, err := http.NewRequest(http.MethodPost, URL, bytes.NewBuffer(outgoing))
|
||||
//defer resp.Body.Close();
|
||||
if err != nil {
|
||||
fmt.Println("Error making request to telegram")
|
||||
fmt.Printf("%T: ", err)
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println("Error posting message to telegram")
|
||||
fmt.Printf("%T: ", err)
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
resBody, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
fmt.Println("Error getting response from telegram")
|
||||
fmt.Printf("%T: ", err)
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
// fmt.Print(res.StatusCode)
|
||||
|
||||
print := false
|
||||
resdata := map[string]interface{}{}
|
||||
json.Unmarshal(resBody, &resdata)
|
||||
if resdata["ok"] != true {
|
||||
fmt.Println("Error telegram responded with error")
|
||||
print = true
|
||||
return
|
||||
}
|
||||
|
||||
if VERBOSE || print {
|
||||
pretty, err := json.MarshalIndent(resdata, "", " ")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fmt.Println(string(pretty))
|
||||
}
|
||||
}
|
||||
func main() {
|
||||
//TODO check if uid is valid
|
||||
// Test KEY
|
||||
if KEY == "" {
|
||||
fmt.Println("Error: Can not send message - no bot token set")
|
||||
return
|
||||
}
|
||||
if UID == "" {
|
||||
fmt.Println("Error: Can not send message - no recepient user id set")
|
||||
return
|
||||
}
|
||||
resp, err := http.Get(BASE_URL + "/getMe")
|
||||
if err != nil {
|
||||
fmt.Println("Error could not validate bot token")
|
||||
fmt.Printf("%T: ", err)
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
me := map[string]interface{}{}
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println("Error could not validate bot token")
|
||||
fmt.Printf("%T: ", err)
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
json.Unmarshal(data, &me)
|
||||
if me["ok"] != true {
|
||||
fmt.Println("Error could not validate bot token")
|
||||
//fmt.Printf("%T\n", me["ok"])
|
||||
pretty, err := json.MarshalIndent(me, "", " ")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fmt.Println(string(pretty))
|
||||
return
|
||||
}
|
||||
fmt.Println("Validated bot token")
|
||||
|
||||
if VERBOSE {
|
||||
pretty, err := json.MarshalIndent(me, "", " ")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fmt.Println(string(pretty))
|
||||
}
|
||||
|
||||
http.HandleFunc("/", handler)
|
||||
fmt.Println("server started at localhost:8080")
|
||||
err = http.ListenAndServe(":8080", nil)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("!!! ERROR !!!")
|
||||
fmt.Printf("%T: ", err)
|
||||
fmt.Println(err)
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
2
telegramAlert.env
Normal file
2
telegramAlert.env
Normal file
@ -0,0 +1,2 @@
|
||||
TELEGRAM_TOKEN=
|
||||
TELEGRAM_USERID=
|
||||
14
telegramAlert.service
Normal file
14
telegramAlert.service
Normal file
@ -0,0 +1,14 @@
|
||||
[Unit]
|
||||
Description=telegramAlert
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
User=tgalert
|
||||
#Group=your_groupname
|
||||
ExecStart=/usr/bin/telegramAlert
|
||||
EnvironmentFile=-/etc/telegramAlert/telegramAlert.env
|
||||
# Other settings as needed
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
||||
1
telegramAlert.sysusers
Normal file
1
telegramAlert.sysusers
Normal file
@ -0,0 +1 @@
|
||||
u tgalert - "telegramAlert daemon" /var/lib/telegramAlert
|
||||
Loading…
x
Reference in New Issue
Block a user