-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhandler.go
185 lines (160 loc) Β· 4.78 KB
/
handler.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
// archiver - handler
// 2020-10-27 21:45
// Benny <[email protected]>
package main
import (
"errors"
"fmt"
"os"
"regexp"
"strconv"
"time"
)
import (
log "github.com/sirupsen/logrus"
"github.com/tgbot-collection/tgbot_ping"
tb "gopkg.in/telebot.v3"
)
func startHandler(c tb.Context) error {
_ = b.Notify(c.Chat(), tb.Typing)
_, _ = b.Send(c.Chat(), startText, &tb.SendOptions{ParseMode: tb.ModeMarkdown})
return nil
}
func aboutHandler(c tb.Context) error {
_ = b.Notify(c.Chat(), tb.Typing)
_, _ = b.Send(c.Chat(), aboutText)
return nil
}
func pingHandler(c tb.Context) error {
_ = b.Notify(c.Chat(), tb.Typing)
info := tgbot_ping.GetRuntime("botsrunner_archiver_1", "WaybackMachine Bot", "html")
ownerId, _ := strconv.ParseInt(os.Getenv("owner"), 10, 64)
if c.Chat().ID == ownerId {
info = fmt.Sprintf("%s\n Total URL archived %d", info, requestCount)
}
_, _ = b.Send(c.Chat(), info, &tb.SendOptions{ParseMode: tb.ModeHTML})
return nil
}
func mainEntrance(c tb.Context) error {
_ = b.Notify(c.Chat(), tb.Typing)
_ = b.Notify(c.Chat(), tb.Typing)
user := getUser(c.Sender().ID)
mode := user.Mode
if mode == "ai" {
addChat(c.Sender().ID, userRole, c.Message().Text)
aiResponse := askOpenAI(c.Sender().ID)
addChat(c.Sender().ID, assistantRole, aiResponse)
return c.Send(aiResponse, tb.NoPreview)
} else {
return urlHandler(c)
}
}
func stopAIHandler(c tb.Context) error {
_ = b.Notify(c.Chat(), tb.Typing)
disableAI(c.Sender().ID)
rows := deleteChat(c.Sender().ID)
return c.Send(fmt.Sprintf("AI mode disabled. %d chats deleted.", rows))
}
func buttonCallback(c tb.Context) error {
_ = c.Edit(&tb.ReplyMarkup{})
var q = &tb.CallbackResponse{Text: "Preparing AI mode...."}
_ = c.Respond(q)
doc := c.Message().Document.File
var file = &tb.File{
FileID: doc.FileID,
UniqueID: doc.UniqueID,
FileSize: doc.FileSize,
FilePath: doc.FilePath,
FileLocal: doc.FileLocal,
FileURL: doc.FileURL,
FileReader: doc.FileReader,
}
_ = b.Download(file, c.Message().Document.FileName)
defer os.Remove(c.Message().Document.FileName)
data := encodeImageToBase64(c.Message().Document.FileName)
enableAI(c.Sender().ID, c.Message().ReplyTo.Text, data)
_ = c.Send("AI mode enabled. Please send your question. **Your chats will be saved in database until you use /stop to exit AI mode**",
&tb.SendOptions{ParseMode: tb.ModeMarkdown})
return nil
}
func urlHandler(c tb.Context) error {
_ = b.Notify(c.Chat(), tb.Typing)
replied, _ := b.Reply(c.Message(), Receive)
providers := []archiveProvider{&archiveOrg{}}
for _, prov := range providers {
go archiveRunner(c.Message(), replied, prov)
}
go screenshotRunner(c)
return nil
}
func screenshotRunner(c tb.Context) {
urls, err := extractURL(c.Message().Text)
if err != nil {
return
}
for _, url := range urls {
filename := takeScreenshot(url)
_ = b.Notify(c.Chat(), tb.UploadingPhoto)
p := &tb.Document{File: tb.FromDisk(filename), FileName: filename}
selector.Inline(selector.Row(btnPrev))
_, _ = b.Reply(c.Message(), p, selector)
_ = os.Remove(filename)
}
}
func archiveRunner(m, replied *tb.Message, provider archiveProvider) {
urls, err := extractURL(m.Text)
if err != nil {
_, _ = b.Edit(replied, InvalidRequest)
return
}
for _, url := range urls {
requestCount += 1
log.Infof("ποΈ Archiving %s", url)
arc(m, replied, provider, url)
time.Sleep(sleep / 2)
}
_, _ = b.Edit(replied, Finish)
}
func arc(m, replied *tb.Message, provider archiveProvider, url string) {
_ = b.Notify(m.Chat, tb.UploadingDocument)
html, err := provider.submit(url)
if err != nil {
_, _ = b.Edit(replied, fmt.Sprintf(ArchiveRequestFailed, provider, err))
return
}
unique, err := provider.analysis(html)
if err != nil {
_, _ = b.Edit(replied, ArchiveNoResult+"\nError: "+err.Error())
return
}
_, _ = b.Edit(replied, Processing)
var result string
for i := 1; i <= attempt; i++ {
_ = b.Notify(m.Chat, tb.RecordingAudio)
time.Sleep(sleep)
result, err = provider.status(unique)
// three-way handle
if err != nil {
log.Errorf("β οΈ %s refresh archive failed %v", url, err)
} else if result != "" {
_ = b.Notify(m.Chat, tb.Typing)
_, _ = b.Send(m.Chat, result, &tb.SendOptions{ParseMode: tb.ModeMarkdown, DisableWebPagePreview: true})
break
} else if result == "" {
msg := fmt.Sprintf(Updating, i, attempt, m.Text)
_, _ = b.Edit(replied, msg, &tb.SendOptions{DisableWebPagePreview: true})
}
}
if result == "" {
log.Errorf(ArchiveStatusTimeout+" %s", url)
_, _ = b.Edit(replied, ArchiveStatusTimeout+" Please try here: https://web.archive.org/web/*/"+url)
}
}
func extractURL(text string) ([]string, error) {
re := regexp.MustCompile(`https?://.*`)
urls := re.FindAllString(text, -1)
if len(urls) == 0 {
return []string{}, errors.New("No URL found")
}
return urls, nil
}