-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
123 lines (84 loc) · 2.59 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
package main
import (
"log"
"os"
"strconv"
"strings"
tg "gopkg.in/telegram-bot-api.v4"
)
var fwdGroupIP int64 = 430838894
var authorizedUser int
var privateChats []int64
// AppendIfMissing appends an element to slice if the newElement
// doesn't already exists, otherwise, returns slice unmodified
func AppendIfMissing(slice []int64, newElement int64) []int64 {
for _, element := range slice {
if element == newElement {
return slice
}
}
return append(slice, newElement)
}
// RemoveIfExisting removes an element from slice if the newElement
// already exists, otherwise, returns slice unmodified
func RemoveIfExisting(slice []int64, newElement int64) []int64 {
for index, element := range slice {
if element == newElement {
return append(slice[:index], slice[index+1:]...)
}
}
return slice
}
func main() {
bot, err := tg.NewBotAPI(os.Getenv("TELEGRAM_BOT_TOKEN"))
if err != nil {
log.Panic(err)
}
bot.Debug = true
log.Printf("Starting %s", bot.Self.UserName)
updateCfg := tg.NewUpdate(0)
updateCfg.Timeout = 60
updates, err := bot.GetUpdatesChan(updateCfg)
fwdGroupIP, err = strconv.ParseInt(os.Getenv("TELEGRAM_GROUP_ID"), 10, 64)
if err != nil {
log.Panic(err)
}
authorizedUser, err = strconv.Atoi(os.Getenv("TELEGRAM_USER_ID"))
if err != nil {
authorizedUser = 0
}
for update := range updates {
msg := update.Message
if msg == nil {
continue
}
if strings.HasPrefix(update.Message.Text, "/help") {
bot.Send(tg.NewMessage(update.Message.Chat.ID, "Code available at https://github.com/raelga/BanMeNotBot."))
}
if msg.Chat.Type == "private" {
if authorizedUser > 0 && msg.From.ID != authorizedUser {
bot.Send(tg.NewMessage(update.Message.Chat.ID, "Not authorized."))
} else {
privateMessageHandler(bot, update.Message)
}
} else {
for _, privateChat := range privateChats {
bot.Send(tg.NewForward(privateChat, msg.Chat.ID, msg.MessageID))
}
}
}
}
func privateMessageHandler(bot *tg.BotAPI, msg *tg.Message) {
if strings.HasPrefix(msg.Text, "/start") {
privateChats = AppendIfMissing(privateChats, msg.Chat.ID)
bot.Send(tg.NewMessage(fwdGroupIP, msg.From.UserName+" started following the group in a private chat."))
} else if strings.HasPrefix(msg.Text, "/stop") {
privateChats = RemoveIfExisting(privateChats, msg.Chat.ID)
bot.Send(tg.NewMessage(fwdGroupIP, msg.From.UserName+" stopped following the group in private chat."))
} else {
_, err := bot.Send(tg.NewForward(fwdGroupIP, msg.Chat.ID, msg.MessageID))
if err != nil {
bot.Send(tg.NewMessage(msg.Chat.ID, "Unable to FWD the message"))
}
}
}