├── .gitignore
├── .env.dist
├── overview.png
├── api
├── Notification.go
├── telegram.go
└── api.go
├── go-telegram-notifier-logo.jpg
├── main.go
├── Dockerfile
├── config
└── config.go
├── helper
└── error.go
├── README.md
└── deployment.yaml
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
2 | .env
--------------------------------------------------------------------------------
/.env.dist:
--------------------------------------------------------------------------------
1 | APP_PORT=5000
2 | TG_CHAT_ID=
3 | TG_BOT_TOKEN=
4 | TOKEN=
--------------------------------------------------------------------------------
/overview.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SlitiBrahim/go-telegram-notifier/HEAD/overview.png
--------------------------------------------------------------------------------
/api/Notification.go:
--------------------------------------------------------------------------------
1 | package api
2 |
3 | type Notification struct {
4 | Message string `json:"message"`
5 | }
6 |
--------------------------------------------------------------------------------
/go-telegram-notifier-logo.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/SlitiBrahim/go-telegram-notifier/HEAD/go-telegram-notifier-logo.jpg
--------------------------------------------------------------------------------
/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "go-telegram-notifier/api"
5 | )
6 |
7 | func main() {
8 | api.Start()
9 | }
10 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM golang:1.15 AS build
2 | WORKDIR /go/src/go-telegram-notifier
3 | COPY . .
4 | # install dependencies
5 | RUN go get -v ./...
6 | RUN go build -o telegram-notifier .
7 |
8 | CMD ["./telegram-notifier"]
--------------------------------------------------------------------------------
/config/config.go:
--------------------------------------------------------------------------------
1 | package config
2 |
3 | import (
4 | "fmt"
5 | "os"
6 | )
7 |
8 | // TODO: better solution ?
9 | var Config = make(map[string]interface{})
10 |
11 | func init() {
12 | Config["APP_PORT"] = os.Getenv("APP_PORT")
13 | Config["TG_CHAT_ID"] = os.Getenv("TG_CHAT_ID")
14 | Config["TG_BOT_TOKEN"] = os.Getenv("TG_BOT_TOKEN")
15 | Config["TOKEN"] = os.Getenv("TOKEN")
16 | Config["TG_API_BOT_BASE_URL"] = fmt.Sprintf("https://api.telegram.org/bot%s/", Config["TG_BOT_TOKEN"])
17 | }
18 |
--------------------------------------------------------------------------------
/helper/error.go:
--------------------------------------------------------------------------------
1 | package helper
2 |
3 | import (
4 | "encoding/json"
5 | "log"
6 | "net/http"
7 | )
8 |
9 | func FailOnError(err error) {
10 | if err != nil {
11 | log.Fatal(err)
12 | }
13 | }
14 |
15 | func SendApiError(w http.ResponseWriter, err error, httpStatus int) {
16 | if err != nil {
17 | httpErr := map[string]interface{}{
18 | "error": err.Error(),
19 | }
20 |
21 | w.Header().Set("Content-Type", "application/json")
22 | w.WriteHeader(httpStatus)
23 |
24 | json.NewEncoder(w).Encode(httpErr)
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/api/telegram.go:
--------------------------------------------------------------------------------
1 | package api
2 |
3 | import (
4 | "bytes"
5 | "encoding/json"
6 | "go-telegram-notifier/config"
7 | "go-telegram-notifier/helper"
8 | "net/http"
9 | "net/url"
10 | "path"
11 | )
12 |
13 | type Message struct {
14 | ChatID string `json:"chat_id"`
15 | Text string `json:"text"`
16 | }
17 |
18 | func getSendMessageURL() string {
19 | baseURL, err := url.Parse(config.Config["TG_API_BOT_BASE_URL"].(string))
20 | helper.FailOnError(err)
21 |
22 | baseURL.Path = path.Join(baseURL.Path, "/sendMessage")
23 |
24 | return baseURL.String()
25 | }
26 |
27 | func sendMessage(message Message) (*http.Response, error) {
28 | body, err := json.Marshal(message)
29 | helper.FailOnError(err)
30 |
31 | return http.Post(getSendMessageURL(), "application/json", bytes.NewReader(body))
32 | }
33 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
29 |
30 |