-
Notifications
You must be signed in to change notification settings - Fork 0
/
newPage.go
64 lines (60 loc) · 1.96 KB
/
newPage.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
package main
import (
"fmt"
"strings"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/StarkBotsIndustries/telegraph/v2"
)
func newPage(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) < 2 {
_, err := ctx.EffectiveMessage.Reply(b, "Wrong format. You need to pass account number and page title to create a new page. \n\nUse <code>/page account_number page_title <in reply to text message></code> instead.", &gotgbot.SendMessageOpts{ParseMode: "html"})
if err != nil {
return fmt.Errorf("failed to send newPage message : %w", err)
}
return nil
}
accountNumber := args[0]
pageTitle := strings.Join(args[1:], " ")
accountNumber, con, err := getToken(accountNumber, b, ctx)
if err != nil {
return fmt.Errorf("failed to send listPages tokens message : %w", err)
}
if con {
return nil
}
acc, _ := telegraph.GetAccountInfo(
telegraph.GetAccountInfoOpts{
AccessToken: accountNumber,
Fields: []string{"author_url", "author_name"},
})
opts := telegraph.CreatePageOpts{
AccessToken: accountNumber,
Title: pageTitle,
HTMLContent: content,
AuthorName: acc.AuthorName,
AuthorURL: acc.AuthorURL,
}
pg, err := telegraph.CreatePage(opts)
if err != nil {
_, err := ctx.EffectiveMessage.Reply(b, "An error occurred while creating page: "+err.Error(), &gotgbot.SendMessageOpts{ParseMode: "html"})
if err != nil {
return fmt.Errorf("failed to send newPage error message : %w", err)
}
return nil
}
aboutNewPage := fmt.Sprintf("<b>Created New Page</b> \n\n<b>URL</b> : %v \n\n<b>Path</b> : <code>%v</code> \n\n<b>Title</b> : %v", pg.URL, pg.Path, pg.Title)
_, err = ctx.EffectiveMessage.Reply(b, aboutNewPage, &gotgbot.SendMessageOpts{ParseMode: "html"})
if err != nil {
return fmt.Errorf("failed to send newPage message: %w", err)
}
return nil
}