/
main.go
264 lines (226 loc) · 6.17 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"bufio"
"flag"
"fmt"
"os"
"path"
"strings"
"github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
const (
VERSION = "0.1.4"
AUTHOR = "travis"
LICENSE = "MIT license"
WEBSITE = "https://github.com/travislee8964/tgsend"
)
var (
version bool
format string
pre bool // preformatted fixed-width.
preview bool // disable link previews in the message(s).
debug bool
token string
timeout int
ChatID int64
ChannelName string
message string
filename string
filetype string
caption string
location bool
longitude float64
latitude float64
disNotice bool
)
var (
bot *tgbotapi.BotAPI
err error
)
func initBot(token string, timeout int, debug bool) {
bot, err = tgbotapi.NewBotAPI(token)
if err != nil {
fmt.Printf("Create bot failed. error msg: %v\n", err.Error())
os.Exit(1)
}
bot.Debug = debug
u := tgbotapi.NewUpdate(0)
u.Timeout = timeout
}
func sendMessage(message string) {
var msg tgbotapi.MessageConfig
if ChatID != 0 {
msg = tgbotapi.NewMessage(ChatID, message)
} else if len(ChannelName) != 0 {
msg = tgbotapi.NewMessageToChannel(ChannelName, message)
} else {
os.Exit(1)
}
msg.DisableWebPagePreview = preview
msg.DisableNotification = disNotice
if pre || format == "markdown" {
msg.ParseMode = tgbotapi.ModeMarkdown
} else if format == "html" {
msg.ParseMode = tgbotapi.ModeHTML
}
_, err = bot.Send(msg)
if err != nil {
errorExit(err.Error())
}
}
func sendLocation(latitude float64, longitude float64) {
if longitude < -180 || longitude > 180 || latitude < -90 || latitude > 90 {
fmt.Printf("Longitude or latitude value invalid: %v, %v\n", latitude, longitude)
os.Exit(1)
}
msg := tgbotapi.NewLocation(ChatID, latitude, longitude)
msg.DisableNotification = disNotice
_, err = bot.Send(msg)
if err != nil {
errorExit(err.Error())
}
}
func sendFile(filename string, filetype string, caption string) {
file := fileReader(filename, filetype, caption)
var msg tgbotapi.Chattable
switch filetype {
case "photo":
msg = tgbotapi.NewPhoto(ChatID, file)
case "video":
msg = tgbotapi.NewVideo(ChatID, file)
case "audio":
msg = tgbotapi.NewAudio(ChatID, file)
case "sticker":
msg = tgbotapi.NewSticker(ChatID, file)
case "animation":
msg = tgbotapi.NewAnimation(ChatID, file)
default:
msg = tgbotapi.NewDocument(ChatID, file)
}
_, err = bot.Send(msg)
if err != nil {
errorExit(err.Error())
}
}
func fileReader(filename string, filetype string, caption string) (file tgbotapi.FileReader) {
reader, err := os.Open(filename)
if err != nil {
fmt.Printf("Reading file %v error: %v\n", filename, err.Error())
os.Exit(1)
}
stat, _ := reader.Stat()
size(stat, filetype)
if caption == "" {
caption = path.Base(filename)
}
file = tgbotapi.FileReader{
Name: caption,
Reader: reader,
}
return file
}
func checkParam() {
if ChatID == 0 && len(ChannelName) == 0 {
fmt.Println("ChatID or ChannelName must be set.")
os.Exit(1)
}
if len(token) < 45 {
fmt.Printf("Token is invalid: %v\n", token)
os.Exit(1)
}
}
func getMessageStdin() {
if message == "-" { // from stdin
var (
scanner *bufio.Scanner
line string
)
scanner = bufio.NewScanner(os.Stdin)
message = ""
for scanner.Scan() {
line = scanner.Text()
if len(line) == 1 && line[0] == '\x1D' {
break
}
message = strings.Join([]string{message, line, "\n"}, "")
}
if scanner.Err() != nil {
fmt.Printf("Read stdin error: %v\n", scanner.Err())
os.Exit(1)
}
}
}
func errorExit(errMsg string) {
fmt.Printf("send message failed. error msg: %v\n", errMsg)
os.Exit(1)
}
func size(fileInfo os.FileInfo, filetype string) {
if fileInfo.IsDir() {
fmt.Printf("Error: '%v' is a directory.\n", filename)
os.Exit(1)
}
var sizeLimit int64
switch filetype {
case "photo":
sizeLimit = 10 * 1024 * 1024 // image max size is 10M.
default:
sizeLimit = 50 * 1024 * 1024 // Telegram bot api limit file size to 50MB.
}
fileSize := fileInfo.Size()
if fileSize > sizeLimit {
fmt.Printf("File %v is too large, size: %.2f MB, size limit: %v MB\n",
filename, float64(fileSize)/(1024*1024), sizeLimit/(1024*1024))
os.Exit(1)
}
}
func versionInfo() {
fmt.Println("Send message via Telegram Bot.")
fmt.Println("Version:", VERSION)
fmt.Println("Author: ", AUTHOR)
fmt.Println("License:", LICENSE)
fmt.Println("Website:", WEBSITE)
os.Exit(0)
}
func main() {
initFlag()
if flag.NFlag() == 0 || version {
versionInfo()
}
checkParam()
getMessageStdin()
initBot(token, timeout, debug)
if len(message) != 0 {
sendMessage(message)
os.Exit(0)
}
if location {
sendLocation(latitude, longitude)
os.Exit(0)
}
if len(filename) != 0 {
sendFile(filename, filetype, caption)
os.Exit(0)
}
}
func initFlag() {
flag.BoolVar(&version, "version", false, "Print version information.")
flag.StringVar(&format, "format", "text", "How to format the message(s). "+
"Choose from ['text', 'markdown', 'html']")
flag.BoolVar(&pre, "pre", false, "Send preformatted fixed-width (monospace) text.")
flag.BoolVar(&preview, "preview", false, "disable link previews in the message(s)")
flag.BoolVar(&debug, "debug", false, "Show debug message.")
flag.StringVar(&token, "token", "", "Set the bot token.")
flag.IntVar(&timeout, "timeout", 30, "Set the read timeout for network operations(in seconds).")
flag.Int64Var(&ChatID, "chatid", 0, "Send message to this chatID.")
flag.StringVar(&ChannelName, "channel", "", "Send message to the public channel.")
flag.StringVar(&message, "message", "", "The message to sent.")
flag.StringVar(&filename, "filename", "", "The file to sent. images up to 10 MiB, files up to 50 MiB.")
flag.StringVar(&filetype, "filetype", "document",
"Set the file type, Choose from ['photo', 'video', 'document', 'audio', 'sticker', 'animation']")
flag.StringVar(&caption, "caption", "", "Set the photo/video/document caption")
flag.BoolVar(&location, "location", false, "Send location")
flag.Float64Var(&longitude, "longitude", 0, "Set longitude, value valid [-180, 180]")
flag.Float64Var(&latitude, "latitude", 0, "Set latitude, value valid [-90, 90]")
flag.BoolVar(&disNotice, "disable_notification", false, "Disable notification")
flag.Parse()
}