This repository was archived by the owner on Jul 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend.go
More file actions
58 lines (48 loc) · 1.21 KB
/
Copy pathsend.go
File metadata and controls
58 lines (48 loc) · 1.21 KB
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
package main
import (
"bufio"
"fmt"
"io"
"os"
"gopkg.in/telegram-bot-api.v1"
)
// send starts the loop to send everything that is written on
// stdin to all the memebers of the group
func send(groupName string) error {
group, err := getGroupByName(groupName)
if err != nil {
return err
}
token, err := getBotNameByName(group.BotName)
if err != nil {
return err
}
bot, err := tgbotapi.NewBotAPI(token)
if err != nil {
return fmt.Errorf("could not create bot\n%s\n", err.Error())
}
// https://gist.github.com/svett/a95595069e560173a3c8
info, _ := os.Stdin.Stat()
if (info.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
return fmt.Errorf("The command is intended to work with pipes.")
} else if info.Size() >= 0 {
reader := bufio.NewReader(os.Stdin)
redirect(reader, bot, group.Users)
} else {
return fmt.Errorf("Negative size")
}
return nil
}
func redirect(reader *bufio.Reader, bot *tgbotapi.BotAPI, receiver []int) {
for {
input, err := reader.ReadString('\n')
if err != nil && err == io.EOF {
break
}
for _, id := range receiver {
if _, err := bot.Send(tgbotapi.NewMessage(id, input)); err != nil {
fmt.Printf("Error while sending:\n%s\n", err.Error())
}
}
}
}