🤓 More docs fixes
This commit is contained in:
parent
8add535378
commit
7823d67637
58
account.go
58
account.go
|
@ -7,23 +7,25 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreateAccount create a new Telegraph account. Most users only need one account, but this can be
|
// CreateAccount create a new Telegraph account. Most users only need one
|
||||||
// useful for channel administrators who would like to keep individual author names and profile
|
// account, but this can be useful for channel administrators who would like
|
||||||
// links for each of their channels. On success, returns an Account object with the regular fields
|
// to keep individual author names and profile links for each of their
|
||||||
// and an additional access_token field.
|
// channels. On success, returns an Account object with the regular fields and
|
||||||
|
// an additional access_token field.
|
||||||
func CreateAccount(shortName string, authorName string, authorURL string) (*Account, error) {
|
func CreateAccount(shortName string, authorName string, authorURL string) (*Account, error) {
|
||||||
var args fasthttp.Args
|
var args fasthttp.Args
|
||||||
|
|
||||||
// Required. Account name, helps users with several accounts remember which they are currently
|
// Account name, helps users with several accounts remember which they are
|
||||||
// using. Displayed to the user above the "Edit/Publish" button on Telegra.ph, other users
|
// currently using. Displayed to the user above the "Edit/Publish" button
|
||||||
// don't see this name.
|
// on Telegra.ph, other users don't see this name.
|
||||||
args.Add("short_name", shortName)
|
args.Add("short_name", shortName) // required
|
||||||
|
|
||||||
// Default author name used when creating new articles.
|
// Default author name used when creating new articles.
|
||||||
args.Add("author_name", authorName)
|
args.Add("author_name", authorName)
|
||||||
|
|
||||||
// Default profile link, opened when users click on the author's name below the title. Can be
|
// Default profile link, opened when users click on the author's name
|
||||||
// any link, not necessarily to a Telegram profile or channel.
|
// below the title. Can be any link, not necessarily to a Telegram profile
|
||||||
|
// or channel.
|
||||||
args.Add("author_url", authorURL)
|
args.Add("author_url", authorURL)
|
||||||
|
|
||||||
url := fmt.Sprintf(APIEndpoint, "createAccount")
|
url := fmt.Sprintf(APIEndpoint, "createAccount")
|
||||||
|
@ -40,13 +42,14 @@ func CreateAccount(shortName string, authorName string, authorURL string) (*Acco
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// EditAccountInfo update information about a Telegraph account. Pass only the parameters that you
|
// EditAccountInfo update information about a Telegraph account. Pass only the
|
||||||
// want to edit. On success, returns an Account object with the default fields.
|
// parameters that you want to edit. On success, returns an Account object
|
||||||
|
// with the default fields.
|
||||||
func (account *Account) EditAccountInfo(update *Account) (*Account, error) {
|
func (account *Account) EditAccountInfo(update *Account) (*Account, error) {
|
||||||
var args fasthttp.Args
|
var args fasthttp.Args
|
||||||
|
|
||||||
// Required. Access token of the Telegraph account.
|
// Access token of the Telegraph account.
|
||||||
args.Add("access_token", account.AccessToken)
|
args.Add("access_token", account.AccessToken) // required
|
||||||
|
|
||||||
// New account name.
|
// New account name.
|
||||||
args.Add("short_name", update.ShortName)
|
args.Add("short_name", update.ShortName)
|
||||||
|
@ -54,8 +57,9 @@ func (account *Account) EditAccountInfo(update *Account) (*Account, error) {
|
||||||
// New default author name used when creating new articles.
|
// New default author name used when creating new articles.
|
||||||
args.Add("author_name", update.AuthorName)
|
args.Add("author_name", update.AuthorName)
|
||||||
|
|
||||||
// New default profile link, opened when users click on the author's name below the title. Can
|
// New default profile link, opened when users click on the author's name
|
||||||
// be any link, not necessarily to a Telegram profile or channel.
|
// below the title. Can be any link, not necessarily to a Telegram profile
|
||||||
|
// or channel.
|
||||||
args.Add("author_url", update.AuthorURL)
|
args.Add("author_url", update.AuthorURL)
|
||||||
|
|
||||||
url := fmt.Sprintf(APIEndpoint, "editAccountInfo")
|
url := fmt.Sprintf(APIEndpoint, "editAccountInfo")
|
||||||
|
@ -72,15 +76,16 @@ func (account *Account) EditAccountInfo(update *Account) (*Account, error) {
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAccountInfo get information about a Telegraph account. Returns an Account object on success.
|
// GetAccountInfo get information about a Telegraph account. Returns an
|
||||||
|
// Account object on success.
|
||||||
func (account *Account) GetAccountInfo(fields []string) (*Account, error) {
|
func (account *Account) GetAccountInfo(fields []string) (*Account, error) {
|
||||||
var args fasthttp.Args
|
var args fasthttp.Args
|
||||||
|
|
||||||
// Required. Access token of the Telegraph account.
|
// Access token of the Telegraph account.
|
||||||
args.Add("access_token", account.AccessToken)
|
args.Add("access_token", account.AccessToken) // required
|
||||||
|
|
||||||
// List of account fields to return. Available fields: short_name, author_name, author_url,
|
// List of account fields to return. Available fields: short_name,
|
||||||
// auth_url, page_count.
|
// author_name, author_url, auth_url, page_count.
|
||||||
args.Add("fields", fmt.Sprintf(`["%s"]`, strings.Join(fields, `","`)))
|
args.Add("fields", fmt.Sprintf(`["%s"]`, strings.Join(fields, `","`)))
|
||||||
|
|
||||||
url := fmt.Sprintf(APIEndpoint, "getAccountInfo")
|
url := fmt.Sprintf(APIEndpoint, "getAccountInfo")
|
||||||
|
@ -97,14 +102,15 @@ func (account *Account) GetAccountInfo(fields []string) (*Account, error) {
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RevokeAccessToken revoke access_token and generate a new one, for example, if the user would
|
// RevokeAccessToken revoke access_token and generate a new one, for example,
|
||||||
// like to reset all connected sessions, or you have reasons to believe the token was compromised.
|
// if the user would like to reset all connected sessions, or you have reasons
|
||||||
// On success, returns an Account object with new access_token and auth_url fields.
|
// to believe the token was compromised. On success, returns an Account object
|
||||||
|
// with new access_token and auth_url fields.
|
||||||
func (account *Account) RevokeAccessToken() (*Account, error) {
|
func (account *Account) RevokeAccessToken() (*Account, error) {
|
||||||
var args fasthttp.Args
|
var args fasthttp.Args
|
||||||
|
|
||||||
// Required. Access token of the Telegraph account.
|
// Access token of the Telegraph account.
|
||||||
args.Add("access_token", account.AccessToken)
|
args.Add("access_token", account.AccessToken) // required
|
||||||
|
|
||||||
url := fmt.Sprintf(APIEndpoint, "revokeAccessToken")
|
url := fmt.Sprintf(APIEndpoint, "revokeAccessToken")
|
||||||
body, err := request(url, &args)
|
body, err := request(url, &args)
|
||||||
|
|
|
@ -39,7 +39,8 @@ var availableAttributes = map[string]bool{
|
||||||
"src": true,
|
"src": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContentFormat transforms data to a DOM-based format to represent the content of the page.
|
// ContentFormat transforms data to a DOM-based format to represent the
|
||||||
|
// content of the page.
|
||||||
func ContentFormat(data interface{}) ([]Node, error) {
|
func ContentFormat(data interface{}) ([]Node, error) {
|
||||||
var doc html.Node
|
var doc html.Node
|
||||||
switch dst := data.(type) {
|
switch dst := data.(type) {
|
||||||
|
|
79
page.go
79
page.go
|
@ -11,11 +11,11 @@ import (
|
||||||
func (account *Account) CreatePage(page *Page, returnContent bool) (*Page, error) {
|
func (account *Account) CreatePage(page *Page, returnContent bool) (*Page, error) {
|
||||||
var args fasthttp.Args
|
var args fasthttp.Args
|
||||||
|
|
||||||
// Required. Access token of the Telegraph account.
|
// Access token of the Telegraph account.
|
||||||
args.Add("access_token", account.AccessToken)
|
args.Add("access_token", account.AccessToken) // required
|
||||||
|
|
||||||
// Required. Page title.
|
// Page title.
|
||||||
args.Add("title", page.Title)
|
args.Add("title", page.Title) // required
|
||||||
|
|
||||||
if page.AuthorName != "" {
|
if page.AuthorName != "" {
|
||||||
// Author name, displayed below the article's title.
|
// Author name, displayed below the article's title.
|
||||||
|
@ -23,8 +23,9 @@ func (account *Account) CreatePage(page *Page, returnContent bool) (*Page, error
|
||||||
}
|
}
|
||||||
|
|
||||||
if page.AuthorURL != "" {
|
if page.AuthorURL != "" {
|
||||||
// Profile link, opened when users click on the author's name below the title. Can be any
|
// Profile link, opened when users click on the author's name below
|
||||||
// link, not necessarily to a Telegram profile or channel.
|
// the title. Can be any link, not necessarily to a Telegram profile
|
||||||
|
// or channel.
|
||||||
args.Add("author_url", page.AuthorURL)
|
args.Add("author_url", page.AuthorURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,8 +37,8 @@ func (account *Account) CreatePage(page *Page, returnContent bool) (*Page, error
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Required. Content of the page.
|
// Content of the page.
|
||||||
args.Add("content", string(content))
|
args.Add("content", string(content)) // required
|
||||||
|
|
||||||
url := fmt.Sprintf(APIEndpoint, "createPage")
|
url := fmt.Sprintf(APIEndpoint, "createPage")
|
||||||
body, err := request(url, &args)
|
body, err := request(url, &args)
|
||||||
|
@ -53,39 +54,41 @@ func (account *Account) CreatePage(page *Page, returnContent bool) (*Page, error
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// EditPage edit an existing Telegraph page. On success, returns a Page object.
|
// EditPage edit an existing Telegraph page. On success, returns a Page
|
||||||
func (account *Account) EditPage(page *Page, returnContent bool) (*Page, error) {
|
// object.
|
||||||
|
func (account *Account) EditPage(update *Page, returnContent bool) (*Page, error) {
|
||||||
var args fasthttp.Args
|
var args fasthttp.Args
|
||||||
|
|
||||||
// Required. Access token of the Telegraph account.
|
// Access token of the Telegraph account.
|
||||||
args.Add("access_token", account.AccessToken)
|
args.Add("access_token", account.AccessToken) // required
|
||||||
|
|
||||||
// Required. Page title.
|
// Page title.
|
||||||
args.Add("title", page.Title)
|
args.Add("title", update.Title) // required
|
||||||
|
|
||||||
if page.AuthorName != "" {
|
if update.AuthorName != "" {
|
||||||
// Author name, displayed below the article's title.
|
// Author name, displayed below the article's title.
|
||||||
args.Add("author_name", page.AuthorName)
|
args.Add("author_name", update.AuthorName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if page.AuthorURL != "" {
|
if update.AuthorURL != "" {
|
||||||
// Profile link, opened when users click on the author's name below the title. Can be any
|
// Profile link, opened when users click on the author's name below
|
||||||
// link, not necessarily to a Telegram profile or channel.
|
// the title. Can be any link, not necessarily to a Telegram profile
|
||||||
args.Add("author_url", page.AuthorURL)
|
// or channel.
|
||||||
|
args.Add("author_url", update.AuthorURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If true, a content field will be returned in the Page object.
|
// If true, a content field will be returned in the Page object.
|
||||||
args.Add("return_content", strconv.FormatBool(returnContent))
|
args.Add("return_content", strconv.FormatBool(returnContent))
|
||||||
|
|
||||||
content, err := json.Marshal(page.Content)
|
content, err := json.Marshal(update.Content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Required. Content of the page.
|
// Content of the page.
|
||||||
args.Add("content", string(content))
|
args.Add("content", string(content)) // required
|
||||||
|
|
||||||
url := fmt.Sprintf(PathEndpoint, "editPage", page.Path)
|
url := fmt.Sprintf(PathEndpoint, "editPage", update.Path)
|
||||||
body, err := request(url, &args)
|
body, err := request(url, &args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -120,13 +123,13 @@ func GetPage(path string, returnContent bool) (*Page, error) {
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPageList get a list of pages belonging to a Telegraph account. Returns a PageList object,
|
// GetPageList get a list of pages belonging to a Telegraph account. Returns
|
||||||
// sorted by most recently created pages first.
|
// a PageList object, sorted by most recently created pages first.
|
||||||
func (account *Account) GetPageList(offset int, limit int) (*PageList, error) {
|
func (account *Account) GetPageList(offset int, limit int) (*PageList, error) {
|
||||||
var args fasthttp.Args
|
var args fasthttp.Args
|
||||||
|
|
||||||
// Required. Access token of the Telegraph account.
|
// Access token of the Telegraph account.
|
||||||
args.Add("access_token", account.AccessToken)
|
args.Add("access_token", account.AccessToken) // required
|
||||||
|
|
||||||
// Sequential number of the first page to be returned.
|
// Sequential number of the first page to be returned.
|
||||||
args.Add("offset", strconv.Itoa(offset))
|
args.Add("offset", strconv.Itoa(offset))
|
||||||
|
@ -148,31 +151,33 @@ func (account *Account) GetPageList(offset int, limit int) (*PageList, error) {
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetViews get the number of views for a Telegraph article. By default, the total number of page
|
// GetViews get the number of views for a Telegraph article. By default, the
|
||||||
// views will be returned. Returns a PageViews object on success.
|
// total number of page views will be returned. Returns a PageViews object
|
||||||
|
// on success.
|
||||||
func GetViews(path string, hour int, day int, month int, year int) (*PageViews, error) {
|
func GetViews(path string, hour int, day int, month int, year int) (*PageViews, error) {
|
||||||
var args fasthttp.Args
|
var args fasthttp.Args
|
||||||
|
|
||||||
if hour > -1 {
|
if hour > -1 {
|
||||||
// If passed, the number of page views for the requested hour will be returned.
|
// If passed, the number of page views for the requested hour will
|
||||||
|
// be returned.
|
||||||
args.Add("hour", strconv.Itoa(hour))
|
args.Add("hour", strconv.Itoa(hour))
|
||||||
}
|
}
|
||||||
|
|
||||||
if day > 0 {
|
if day > 0 {
|
||||||
// Required if hour is passed. If passed, the number of page views for the requested
|
// Required if hour is passed. If passed, the number of page views
|
||||||
// day will be returned.
|
// for the requested day will be returned.
|
||||||
args.Add("day", strconv.Itoa(day))
|
args.Add("day", strconv.Itoa(day))
|
||||||
}
|
}
|
||||||
|
|
||||||
if month > 0 {
|
if month > 0 {
|
||||||
// Required if day is passed. If passed, the number of page views for the
|
// Required if day is passed. If passed, the number of page views
|
||||||
// requested month will be returned.
|
// for the requested month will be returned.
|
||||||
args.Add("month", strconv.Itoa(month))
|
args.Add("month", strconv.Itoa(month))
|
||||||
}
|
}
|
||||||
|
|
||||||
if year > 0 {
|
if year > 0 {
|
||||||
// Required if month is passed. If passed, the number of page views for the
|
// Required if month is passed. If passed, the number of page views
|
||||||
// requested year will be returned.
|
// for the requested year will be returned.
|
||||||
args.Add("year", strconv.Itoa(year))
|
args.Add("year", strconv.Itoa(year))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
72
telegraph.go
72
telegraph.go
|
@ -1,3 +1,5 @@
|
||||||
|
// Package telegraph has functions and types used for interacting with the
|
||||||
|
// Telegraph API.
|
||||||
package telegraph
|
package telegraph
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -19,14 +21,14 @@ const (
|
||||||
type (
|
type (
|
||||||
// Account represents a Telegraph account.
|
// Account represents a Telegraph account.
|
||||||
Account struct {
|
Account struct {
|
||||||
// Optional. Only returned by the createAccount and revokeAccessToken
|
// Only returned by the createAccount and revokeAccessToken method.
|
||||||
// method. Access token of the Telegraph account.
|
// Access token of the Telegraph account.
|
||||||
AccessToken string `json:"access_token"`
|
AccessToken string `json:"access_token"` // optional
|
||||||
|
|
||||||
// Optional. URL to authorize a browser on telegra.ph and connect it to
|
// URL to authorize a browser on telegra.ph and connect it to a
|
||||||
// a Telegraph account. This URL is valid for only one use and for 5
|
// Telegraph account. This URL is valid for only one use and for 5
|
||||||
// minutes only.
|
// minutes only.
|
||||||
AuthURL string `json:"auth_url"`
|
AuthURL string `json:"auth_url"` // optional
|
||||||
|
|
||||||
// Account name, helps users with several accounts remember which they
|
// Account name, helps users with several accounts remember which they
|
||||||
// are currently using. Displayed to the user above the "Edit/Publish"
|
// are currently using. Displayed to the user above the "Edit/Publish"
|
||||||
|
@ -41,12 +43,12 @@ type (
|
||||||
// channel.
|
// channel.
|
||||||
AuthorURL string `json:"author_url"`
|
AuthorURL string `json:"author_url"`
|
||||||
|
|
||||||
// Optional. Number of pages belonging to the Telegraph account.
|
// Number of pages belonging to the Telegraph account.
|
||||||
PageCount int `json:"page_count"`
|
PageCount int `json:"page_count"` // optional
|
||||||
}
|
}
|
||||||
|
|
||||||
// PageList represents a list of Telegraph articles belonging to an account.
|
// PageList represents a list of Telegraph articles belonging to an
|
||||||
// Most recently created articles first.
|
// account. Most recently created articles first.
|
||||||
PageList struct {
|
PageList struct {
|
||||||
// Total number of pages belonging to the target Telegraph account.
|
// Total number of pages belonging to the target Telegraph account.
|
||||||
TotalCount int `json:"total_count"`
|
TotalCount int `json:"total_count"`
|
||||||
|
@ -69,26 +71,26 @@ type (
|
||||||
// Description of the page.
|
// Description of the page.
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
|
|
||||||
// Optional. Name of the author, displayed below the title.
|
// Name of the author, displayed below the title.
|
||||||
AuthorName string `json:"author_name"`
|
AuthorName string `json:"author_name"` // optional
|
||||||
|
|
||||||
// Optional. Profile link, opened when users click on the author's name
|
// Profile link, opened when users click on the author's name below
|
||||||
// below the title. Can be any link, not necessarily to a Telegram
|
// the title. Can be any link, not necessarily to a Telegram profile
|
||||||
// profile or channel.
|
// or channel.
|
||||||
AuthorURL string `json:"author_url"`
|
AuthorURL string `json:"author_url"` // optional
|
||||||
|
|
||||||
// Optional. Image URL of the page.
|
// Image URL of the page.
|
||||||
ImageURL string `json:"image_url"`
|
ImageURL string `json:"image_url"` // optional
|
||||||
|
|
||||||
// Optional. Content of the page.
|
// Content of the page.
|
||||||
Content []Node `json:"content"`
|
Content []Node `json:"content"` // optional
|
||||||
|
|
||||||
// Number of page views for the page.
|
// Number of page views for the page.
|
||||||
Views int `json:"views"`
|
Views int `json:"views"`
|
||||||
|
|
||||||
// Optional. Only returned if access_token passed. True, if the target
|
// Only returned if access_token passed. True, if the target Telegraph
|
||||||
// Telegraph account can edit the page.
|
// account can edit the page.
|
||||||
CanEdit bool `json:"can_edit"`
|
CanEdit bool `json:"can_edit"` // optional
|
||||||
}
|
}
|
||||||
|
|
||||||
// PageViews represents the number of page views for a Telegraph article.
|
// PageViews represents the number of page views for a Telegraph article.
|
||||||
|
@ -103,25 +105,25 @@ type (
|
||||||
|
|
||||||
// NodeElement represents a DOM element node.
|
// NodeElement represents a DOM element node.
|
||||||
NodeElement struct {
|
NodeElement struct {
|
||||||
// Name of the DOM element. Available tags: a, aside, b, blockquote, br,
|
// Name of the DOM element. Available tags: a, aside, b, blockquote,
|
||||||
// code, em, figcaption, figure, h3, h4, hr, i, iframe, img, li, ol, p,
|
// br, code, em, figcaption, figure, h3, h4, hr, i, iframe, img, li,
|
||||||
// pre, s, strong, u, ul, video.
|
// ol, p, pre, s, strong, u, ul, video.
|
||||||
Tag string `json:"tag"`
|
Tag string `json:"tag"`
|
||||||
|
|
||||||
// Optional. Attributes of the DOM element. Key of object represents
|
// Attributes of the DOM element. Key of object represents name of
|
||||||
// name of attribute, value represents value of attribute. Available
|
// attribute, value represents value of attribute. Available
|
||||||
// attributes: href, src.
|
// attributes: href, src.
|
||||||
Attrs map[string]string `json:"attrs"`
|
Attrs map[string]string `json:"attrs"` // optional
|
||||||
|
|
||||||
// Optional. List of child nodes for the DOM element.
|
// List of child nodes for the DOM element.
|
||||||
Children []Node `json:"children"`
|
Children []Node `json:"children"` // optional
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response represents a response from the Telegram API with the result
|
// Response represents a response from the Telegram API with the result
|
||||||
// stored raw. If ok equals true, the request was successful, and the result
|
// stored raw. If ok equals true, the request was successful, and the
|
||||||
// of the query can be found in the result field. In case of an unsuccessful
|
// result of the query can be found in the result field. In case of an
|
||||||
// request, ok equals false, and the error is explained in the error field
|
// unsuccessful request, ok equals false, and the error is explained in
|
||||||
// (e.g. SHORT_NAME_REQUIRED).
|
// the error field (e.g. SHORT_NAME_REQUIRED).
|
||||||
Response struct {
|
Response struct {
|
||||||
Ok bool `json:"ok"`
|
Ok bool `json:"ok"`
|
||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
|
|
Loading…
Reference in New Issue