-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (85 loc) · 2.04 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"time"
"github.com/joho/godotenv"
tgbotapi "gopkg.in/telegram-bot-api.v4"
)
var Bot *tgbotapi.BotAPI
func Validator(update tgbotapi.Update) {
if update.Message == nil {
return
}
rxStrict := regexp.MustCompile(`(?i)\bhttps?://\S+\b`)
url := rxStrict.FindString(update.Message.Text)
if url != "" {
fmt.Println("Found URL:", url)
apiUrl := "https://gateway.netsepio.com/api/v1.0/stats?siteUrl=" + url
client := &http.Client{Timeout: time.Second * 10}
response, err := client.Get(apiUrl)
if err != nil {
fmt.Println("Error sending GET request:", err)
return
}
defer response.Body.Close()
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return
}
var data map[string]interface{}
err = json.Unmarshal(responseBody, &data)
if err != nil {
fmt.Println("Error unmarshalling response:", err)
return
}
message := fmt.Sprintf("The link %s is not tested.", url)
var siteSafety string
if safety, ok := data["payload"]; ok {
if safetyList, ok := safety.([]interface{}); ok {
if len(safetyList) > 0 {
if safetyMap, ok := safetyList[0].(map[string]interface{}); ok {
if safetyValue, ok := safetyMap["siteSafety"].(string); ok {
siteSafety = safetyValue
}
}
}
}
}
if siteSafety != "" && siteSafety != "Safe" {
message = fmt.Sprintf("The link %s is classified as %s", url, siteSafety)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
_, err := Bot.Send(msg)
if err != nil {
fmt.Println("Error sending message:", err)
}
}
}
}
func Start() {
godotenv.Load(".env")
var err error
Bot, err = tgbotapi.NewBotAPI(os.Getenv("TelegramToken"))
if err != nil {
fmt.Println(err)
return
}
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := Bot.GetUpdatesChan(u)
if err != nil {
fmt.Println(err)
return
}
for update := range updates {
Validator(update)
}
}
func main() {
Start()
}