forked from cljoly/telegram-tee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (82 loc) · 1.91 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
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"bufio"
"fmt"
"os"
"strconv"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
const (
exitOK = 0
exitErr = 1
// a few messages were succeeded sent
exitPartialErr = 2
)
// Create a bot with token
func login(token string) (*tgbotapi.BotAPI, error) {
return tgbotapi.NewBotAPI(token)
}
// Parse chat ID from command line arguments
func parseChatID(args []string) (chatIDs []int64, err error) {
chatIDs = make([]int64, len(args))
err = nil
for i := 0; i < len(args); i++ {
chatIDs[i], err = strconv.ParseInt(args[i], 10, 64)
if err != nil {
return
}
}
return
}
// Create a message handler to write content of stdout (each line) to someone
func messageWriter(bot *tgbotapi.BotAPI, chatIDs []int64) int {
var msgTxt string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
msgTxt += scanner.Text() + "\n"
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
return exitErr
}
exitCode := exitOK
errCode := exitErr
for _, chatID := range chatIDs {
msg := tgbotapi.NewMessage(chatID, msgTxt)
msg.ParseMode = tgbotapi.ModeHTML
_, err := bot.Send(msg)
if err != nil {
fmt.Fprintf(os.Stderr, "Error sending message to %d from stdin: '%s'\n", chatID, err.Error())
exitCode = errCode
} else {
errCode = exitPartialErr
if exitCode != exitOK {
exitCode = errCode
}
}
}
return exitCode
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintln(os.Stderr, "missing chat IDs")
os.Exit(exitErr)
}
token := os.Getenv("TLGCLI_TOKEN")
if token == "" {
fmt.Fprintln(os.Stderr, "You need to set TLGCLI_TOKEN in your env")
os.Exit(exitErr)
}
bot, err := login(token)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(exitErr)
}
bot.Debug = false
ids, err := parseChatID(os.Args[1:])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(exitErr)
}
os.Exit(messageWriter(bot, ids))
}