-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
executable file
·200 lines (168 loc) · 5.31 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
package main
import (
"crypto/md5"
"database/sql"
"fmt"
"github.com/jmoiron/sqlx"
"github.com/krol44/telegram-bot-api"
log "github.com/sirupsen/logrus"
"path"
"regexp"
"strconv"
"strings"
"time"
)
var Postgres *sqlx.DB
func main() {
log.Info("TorPurrBot is running...")
Postgres = PostgresConnect()
app := Run()
go app.ObserverQueue()
for update := range app.BotUpdates {
if update.Message != nil {
// logs
app.Logs(update.Message)
isBlock := app.IsBlockUser(update.Message.From.ID)
if update.Message.Text != "" {
suffix := "sent message: "
if isBlock {
suffix = "[blocked] sent message: "
}
app.SendLogToChannel(update.Message.From, "mess", suffix+update.Message.Text)
}
if isBlock {
continue
}
app.Queue <- struct{ Message *tgbotapi.Message }{Message: update.Message}
}
if update.InlineQuery != nil {
if update.InlineQuery.Query == "" {
continue
}
var urlVideo string
sp := strings.Split(update.InlineQuery.Query, "&")
if len(sp) >= 1 {
urlVideo = sp[0]
}
var cache struct {
ID int `db:"id"`
TgFileId string `db:"tg_file_id"`
Caption string `db:"caption"`
Path string `db:"native_path_file"`
}
err := Postgres.Get(&cache, `SELECT tg_file_id, caption, native_path_file
FROM cache WHERE caption LIKE $1`, "%"+urlVideo+"%")
if err != nil && err != sql.ErrNoRows {
log.Error(err)
continue
}
md5Url := fmt.Sprintf("%x", md5.Sum([]byte(update.InlineQuery.Query)))
_, err = Postgres.Exec(`INSERT INTO links (md5_url, url, telegram_id, date_create)
VALUES($1, $2, $3, $4)`,
md5Url, update.InlineQuery.Query, update.InlineQuery.From.ID, time.Now())
var inlineConf tgbotapi.InlineConfig
if cache.TgFileId == "" {
inlineConf = tgbotapi.InlineConfig{
InlineQueryID: update.InlineQuery.ID,
IsPersonal: true,
CacheTime: 1,
SwitchPMText: "No found cache video, click to create",
SwitchPMParameter: md5Url,
}
} else {
var res []interface{}
if path.Ext(cache.Path) == ".mp4" {
b := tgbotapi.NewInlineQueryResultCachedVideo(fmt.Sprintf("%d", cache.ID),
cache.TgFileId, cache.Caption)
b.Caption = cache.Caption + signAdvt
res = append(res, b)
} else if path.Ext(cache.Path) == ".mp3" {
b := tgbotapi.NewInlineQueryResultCachedAudio(fmt.Sprintf("%d", cache.ID), cache.TgFileId)
b.Caption = cache.Caption + signAdvt
res = append(res, b)
} else {
b := tgbotapi.NewInlineQueryResultCachedDocument(fmt.Sprintf("%d", cache.ID),
cache.TgFileId, cache.Caption)
b.Caption = cache.Caption + signAdvt
res = append(res, b)
}
inlineConf = tgbotapi.InlineConfig{
InlineQueryID: update.InlineQuery.ID,
IsPersonal: true,
CacheTime: 1,
Results: res,
}
}
if err != nil {
log.Error(err)
continue
}
if _, err := app.Bot.Request(inlineConf); err != nil {
log.Error(err)
}
}
if update.ChannelPost != nil && update.ChannelPost.Chat.ID == config.ChatIdChannelLog {
sp := strings.Split(update.ChannelPost.Text, " ")
if len(sp) == 2 && sp[0] == "/premium" {
var userFromDB User
_ = Postgres.Get(&userFromDB, `SELECT name, premium, language_code
FROM users WHERE telegram_id = $1`, sp[1])
tr := Translate{Code: userFromDB.LanguageCode}
premium := 0
premiumText := tr.Lang("Premium is disabled") + " 😔"
if userFromDB.Premium == 0 {
premium = 1
premiumText = tr.Lang("Premium is enabled") + " 🎉"
}
_, err := Postgres.Exec("UPDATE users SET premium = $1 WHERE telegram_id = $2", premium, sp[1])
if err != nil {
log.Error(err)
}
if whoId, err := strconv.Atoi(sp[1]); err == nil {
app.SendLogToChannel(&tgbotapi.User{ID: int64(whoId)}, "mess", premiumText)
_, _ = app.Bot.Send(tgbotapi.NewMessage(int64(whoId), premiumText))
}
}
if len(sp) == 2 && sp[0] == "/block" {
var userFromDB User
_ = Postgres.Get(&userFromDB, "SELECT name, block FROM users WHERE telegram_id = $1", sp[1])
block := 0
if userFromDB.Block == 0 {
block = 1
}
_, err := Postgres.Exec("UPDATE users SET block = $1 WHERE telegram_id = $2", block, sp[1])
if err != nil {
log.Error(err)
}
if whoId, err := strconv.Atoi(sp[1]); err == nil {
app.SendLogToChannel(&tgbotapi.User{ID: int64(whoId)}, "mess",
fmt.Sprintf("block=%d", block))
}
}
if update.ChannelPost.ReplyToMessage != nil {
regx := regexp.MustCompile(` \((.*?)\) `)
matches := regx.FindStringSubmatch(update.ChannelPost.ReplyToMessage.Text)
if len(matches) == 2 {
replayChatId, _ := strconv.Atoi(matches[1])
_, err := app.Bot.Send(tgbotapi.NewMessage(int64(replayChatId),
"Support: "+update.ChannelPost.Text))
if err != nil {
log.Error(err)
}
}
}
}
if update.MyChatMember != nil {
if update.MyChatMember.NewChatMember.Status == "kicked" {
_, err := Postgres.Exec("UPDATE users SET block_why = $1 WHERE telegram_id = $2",
"user kicked bot", update.MyChatMember.From.ID)
if err != nil {
log.Error(err)
}
app.SendLogToChannel(&tgbotapi.User{ID: update.MyChatMember.From.ID,
UserName: update.MyChatMember.From.UserName},
"mess", "🩸 user kicked bot")
}
}
}
}