-
Notifications
You must be signed in to change notification settings - Fork 20
/
editPage.go
67 lines (62 loc) · 1.98 KB
/
editPage.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
package main
import (
"fmt"
"strings"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/StarkBotsIndustries/telegraph/v2"
)
func editPage(b *gotgbot.Bot, ctx *ext.Context) error {
content, err := extractHTMLFromReply(b, ctx)
if err != nil {
return err
}
if content == "" {
return nil
}
args := getArgs(ctx)
if len(args) < 3 {
_, err := ctx.EffectiveMessage.Reply(b, "Wrong format. You need to pass account number, page title and page path to edit a page. \n\nUse <code>/editpage account_number page_path page_title <in reply to html message></code> instead.", &gotgbot.SendMessageOpts{ParseMode: "html"})
if err != nil {
return fmt.Errorf("failed to send editPage message : %w", err)
}
return nil
}
accountNumber := args[0]
pagePath := args[1]
pageTitle := strings.Join(args[2:], " ")
accountNumber, con, err := getToken(accountNumber, b, ctx)
if err != nil {
return fmt.Errorf("failed to get token in editPage func : %w", err)
}
if con {
return nil
}
acc, _ := telegraph.GetAccountInfo(
telegraph.GetAccountInfoOpts{
AccessToken: accountNumber,
Fields: []string{"author_url", "author_name"},
})
pg, err := telegraph.EditPage(
telegraph.EditPageOpts{
Path: pagePath,
AccessToken: accountNumber,
Title: pageTitle,
HTMLContent: content,
AuthorName: acc.AuthorName,
AuthorURL: acc.AuthorURL,
})
if err != nil {
_, err := ctx.EffectiveMessage.Reply(b, "failed to edit Page: "+err.Error(), &gotgbot.SendMessageOpts{ParseMode: "html"})
if err != nil {
return fmt.Errorf("failed to send editPage error message : %w", err)
}
return nil
}
abouteditPage := fmt.Sprintf("<b>Edited Page</b> \n\n<b>URL</b> : %v \n\n<b>Path</b> : %v \n\n<b>Title</b> : %v", pg.URL, pg.Path, pg.Title)
_, err = ctx.EffectiveMessage.Reply(b, abouteditPage, &gotgbot.SendMessageOpts{ParseMode: "html"})
if err != nil {
return fmt.Errorf("failed to send editPage message: %w", err)
}
return nil
}