-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
86 lines (70 loc) · 1.5 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
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
tb "gopkg.in/tucnak/telebot.v2"
)
type channel string
func (c channel) Recipient() string {
return string(c)
}
func main() {
var comment string
var b *tb.Bot
var err error
users := strings.Split(os.Getenv("ALLOWED_USERS"), ",")
poller := &tb.LongPoller{Timeout: 15 * time.Second}
authMiddlewarePoller := tb.NewMiddlewarePoller(poller, func(upd *tb.Update) bool {
if contains(users, upd.Message.Sender.Username) {
return true
}
b.Send(upd.Message.Sender, "Я тебя не знаю. Иди нахуй")
return false
})
b, err = tb.NewBot(tb.Settings{
Token: os.Getenv("TELEGRAM_API_TOKEN"),
Poller: authMiddlewarePoller,
Verbose: true,
})
if err != nil {
log.Fatal(err)
}
b.Handle(tb.OnText, func(m *tb.Message) {
comment = m.Text
})
b.Handle(tb.OnAnimation, func(m *tb.Message) {
m.Animation.Caption = comment
send(b, m.Animation)
comment = ""
})
b.Handle(tb.OnPhoto, func(m *tb.Message) {
m.Photo.Caption = comment
send(b, m.Photo)
comment = ""
})
b.Handle(tb.OnVideo, func(m *tb.Message) {
m.Video.Caption = comment
send(b, m.Video)
comment = ""
})
fmt.Println("Starting...")
b.Start()
}
func send(b *tb.Bot, attach interface{}) {
sendTo := channel(os.Getenv("CHANNEL_NAME"))
_, err := b.Send(sendTo, attach)
if err != nil {
log.Println(err)
}
}
func contains(slice []string, val string) bool {
for _, item := range slice {
if item == val {
return true
}
}
return false
}