-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtelegram.go
173 lines (146 loc) · 4.18 KB
/
telegram.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
package main
import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
lru "github.com/hashicorp/golang-lru"
)
func sendTelegramMessageWithAnimationId(chatId int64, fileId string, message string) (id interface{}) {
video := tgbotapi.NewAnimationShare(chatId, fileId)
video.Caption = message
video.ParseMode = "HTML"
c, err := bot.Send(video)
if err != nil {
log.Warn().Str("id", fileId).Str("message", message).Err(err).
Msg("sending telegram video")
}
return c.MessageID
}
func isAdmin(chat *tgbotapi.Chat, user *tgbotapi.User) bool {
if chat.Type == "supergroup" || chat.Type == "group" {
chatmember, err := bot.GetChatMember(tgbotapi.ChatConfigWithUser{
ChatID: chat.ID,
SuperGroupUsername: chat.ChatConfig().SuperGroupUsername,
UserID: user.ID,
})
if err != nil ||
(chatmember.Status != "administrator" && chatmember.Status != "creator") {
log.Warn().Err(err).Int64("group", chat.ID).
Int("user-tg", user.ID).Msg("can't get user or not an admin.")
return false
}
return true
} else if chat.Type == "channel" {
// if you're posting then you're an admin
return true
} else {
return false
}
}
func deleteMessage(message *tgbotapi.Message) {
if message == nil || message.Chat == nil {
return
}
bot.Send(tgbotapi.NewDeleteMessage(message.Chat.ID, message.MessageID))
}
func forwardMessage(message *tgbotapi.Message, targetChat int64) {
bot.Send(tgbotapi.NewForward(targetChat, message.Chat.ID, message.MessageID))
}
func getChatOwner(chatId int64) (*User, error) {
admins, err := bot.GetChatAdministrators(tgbotapi.ChatConfig{
ChatID: chatId,
})
if err != nil {
return nil, err
}
for _, admin := range admins {
if admin.Status == "creator" {
user, tcase, err := ensureTelegramUser(&tgbotapi.Message{From: admin.User})
if err != nil {
log.Warn().Err(err).Int("case", tcase).
Str("username", admin.User.UserName).
Int("id", admin.User.ID).
Msg("failed to ensure user when fetching chat owner")
return user, err
}
return user, nil
}
}
return nil, errors.New("chat has no owner")
}
var pictureCache, _ = lru.NewARC(25)
func getTelegramUserPicture(username string) ([]byte, error) {
if i, ok := pictureCache.Get(username); ok {
return i.([]byte), nil
}
url, err := getTelegramUserPictureURL(http.DefaultClient, username)
if err != nil {
return nil, err
}
if url == "https://telegram.org/img/t_logo.png" {
// got the default telegram logo, try using tor instead
if s.TorProxyURL != nil && s.TorProxyURL.Host != "" {
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(s.TorProxyURL),
},
}
url, err = getTelegramUserPictureURL(client, username)
if err != nil {
return nil, err
}
}
}
b, err := imageBytesFromURL(url)
if err != nil {
return nil, err
}
pictureCache.Add(username, b)
return b, nil
}
func getTelegramUserPictureURL(client *http.Client, username string) (string, error) {
resp, err := client.Get("https://t.me/" + username)
if err != nil {
return "", err
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return "", err
}
url, ok := doc.Find(`meta[property="og:image"]`).First().Attr("content")
if !ok {
return "", errors.New("no image available for this user")
}
return url, nil
}
func examineTelegramUsername(username string) (*User, error) {
if username == "" {
return nil, errors.New("username is blank")
}
if !strings.HasPrefix(username, "@") {
return nil, errors.New("username doesn't start with @")
}
username = strings.ToLower(username)
username = username[1:] // exclude initial @
user, err := ensureTelegramUsername(username)
if err != nil {
return nil, err
}
return &user, nil
}
func messageHasCaption(message *tgbotapi.Message) bool {
return message.Caption != "" ||
message.Photo != nil ||
message.Document != nil ||
message.Audio != nil ||
message.Animation != nil
}
func telegramMessageLink(message *tgbotapi.Message) string {
return fmt.Sprintf("https://t.me/c/%s/%d",
strconv.FormatInt(message.Chat.ID, 10)[4:], message.MessageID)
}