-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
88 lines (74 loc) · 1.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"strings"
"sync"
"github.com/acarl005/stripansi"
)
var wg sync.WaitGroup
func main() {
var oneLine, verboseMode bool
var ApiToken, lines string
flag.StringVar(&ApiToken, "u", "", "Telegram ApiToken")
flag.BoolVar(&oneLine, "1", false, "Send message line-by-line")
flag.BoolVar(&verboseMode, "v", false, "Verbose mode")
flag.Parse()
apitokenEnv := os.Getenv("TELEGRAM_API_TOKEN")
TelegramEnv := "https://api.telegram.org/bot"+apitokenEnv+"/sendMessage"
chatid := os.Getenv("TELEGRAM_CHAT_ID")
if TelegramEnv != "" {
ApiToken = TelegramEnv
} else {
if ApiToken == "" {
if verboseMode {
fmt.Println("Telegram ApiToken not set!")
}
}
}
if !isStdin() {
os.Exit(1)
}
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
line := sc.Text()
fmt.Println(line)
if oneLine {
if ApiToken != "" {
wg.Add(1)
go teleman(ApiToken, line,chatid)
}
} else {
lines += line
lines += "\n"
}
}
if !oneLine {
wg.Add(1)
go teleman(ApiToken, lines,chatid)
}
wg.Wait()
}
func isStdin() bool {
f, e := os.Stdin.Stat()
if e != nil {
return false
}
if f.Mode()&os.ModeNamedPipe == 0 {
return false
}
return true
}
type data struct {
Chat_id string `json:"chat_id"`
Text string `json:"text"`
}
func teleman(url string, line string,chatid string) {
data, _ := json.Marshal(data{Chat_id:chatid,Text: stripansi.Strip(line)})
http.Post(url, "application/json", strings.NewReader(string(data)))
wg.Done()
}