2016-12-21 19:32:28 +00:00
|
|
|
package telegraph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2017-01-10 10:28:23 +00:00
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
2016-12-21 19:32:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreatePage create a new Telegraph page. On success, returns a Page object.
|
|
|
|
func (account *Account) CreatePage(page *Page, returnContent bool) (*Page, error) {
|
|
|
|
var args fasthttp.Args
|
|
|
|
|
2016-12-25 03:10:55 +00:00
|
|
|
// Access token of the Telegraph account.
|
|
|
|
args.Add("access_token", account.AccessToken) // required
|
2016-12-21 19:32:28 +00:00
|
|
|
|
2016-12-25 03:10:55 +00:00
|
|
|
// Page title.
|
|
|
|
args.Add("title", page.Title) // required
|
2016-12-21 19:32:28 +00:00
|
|
|
|
2016-12-24 17:09:28 +00:00
|
|
|
if page.AuthorName != "" {
|
|
|
|
// Author name, displayed below the article's title.
|
|
|
|
args.Add("author_name", page.AuthorName)
|
|
|
|
}
|
2016-12-21 19:32:28 +00:00
|
|
|
|
2016-12-24 17:09:28 +00:00
|
|
|
if page.AuthorURL != "" {
|
2016-12-25 03:10:55 +00:00
|
|
|
// Profile link, opened when users click on the author's name below
|
|
|
|
// the title. Can be any link, not necessarily to a Telegram profile
|
|
|
|
// or channel.
|
2016-12-24 17:09:28 +00:00
|
|
|
args.Add("author_url", page.AuthorURL)
|
|
|
|
}
|
2016-12-21 19:32:28 +00:00
|
|
|
|
|
|
|
// If true, a content field will be returned in the Page object.
|
2016-12-24 17:09:28 +00:00
|
|
|
args.Add("return_content", strconv.FormatBool(returnContent))
|
|
|
|
|
|
|
|
content, err := json.Marshal(page.Content)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-25 03:10:55 +00:00
|
|
|
// Content of the page.
|
|
|
|
args.Add("content", string(content)) // required
|
2016-12-21 19:32:28 +00:00
|
|
|
|
|
|
|
url := fmt.Sprintf(APIEndpoint, "createPage")
|
2016-12-24 17:09:28 +00:00
|
|
|
body, err := request(url, &args)
|
2016-12-21 19:32:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp Page
|
|
|
|
if err := json.Unmarshal(body.Result, &resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp, nil
|
|
|
|
}
|
|
|
|
|
2016-12-25 03:10:55 +00:00
|
|
|
// EditPage edit an existing Telegraph page. On success, returns a Page
|
|
|
|
// object.
|
|
|
|
func (account *Account) EditPage(update *Page, returnContent bool) (*Page, error) {
|
2016-12-21 19:32:28 +00:00
|
|
|
var args fasthttp.Args
|
|
|
|
|
2016-12-25 03:10:55 +00:00
|
|
|
// Access token of the Telegraph account.
|
|
|
|
args.Add("access_token", account.AccessToken) // required
|
2016-12-21 19:32:28 +00:00
|
|
|
|
2016-12-25 03:10:55 +00:00
|
|
|
// Page title.
|
|
|
|
args.Add("title", update.Title) // required
|
2016-12-21 19:32:28 +00:00
|
|
|
|
2016-12-25 03:10:55 +00:00
|
|
|
if update.AuthorName != "" {
|
2016-12-24 17:09:28 +00:00
|
|
|
// Author name, displayed below the article's title.
|
2016-12-25 03:10:55 +00:00
|
|
|
args.Add("author_name", update.AuthorName)
|
2016-12-24 17:09:28 +00:00
|
|
|
}
|
2016-12-21 19:32:28 +00:00
|
|
|
|
2016-12-25 03:10:55 +00:00
|
|
|
if update.AuthorURL != "" {
|
|
|
|
// Profile link, opened when users click on the author's name below
|
|
|
|
// the title. Can be any link, not necessarily to a Telegram profile
|
|
|
|
// or channel.
|
|
|
|
args.Add("author_url", update.AuthorURL)
|
2016-12-24 17:09:28 +00:00
|
|
|
}
|
2016-12-21 19:32:28 +00:00
|
|
|
|
|
|
|
// If true, a content field will be returned in the Page object.
|
2016-12-24 17:09:28 +00:00
|
|
|
args.Add("return_content", strconv.FormatBool(returnContent))
|
|
|
|
|
2016-12-25 03:10:55 +00:00
|
|
|
content, err := json.Marshal(update.Content)
|
2016-12-24 17:09:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-21 19:32:28 +00:00
|
|
|
|
2016-12-25 03:10:55 +00:00
|
|
|
// Content of the page.
|
|
|
|
args.Add("content", string(content)) // required
|
2016-12-24 17:09:28 +00:00
|
|
|
|
2016-12-25 03:10:55 +00:00
|
|
|
url := fmt.Sprintf(PathEndpoint, "editPage", update.Path)
|
2016-12-24 17:09:28 +00:00
|
|
|
body, err := request(url, &args)
|
2016-12-21 19:32:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp Page
|
|
|
|
if err := json.Unmarshal(body.Result, &resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPage get a Telegraph page. Returns a Page object on success.
|
|
|
|
func GetPage(path string, returnContent bool) (*Page, error) {
|
|
|
|
var args fasthttp.Args
|
|
|
|
|
|
|
|
// If true, content field will be returned in Page object.
|
2016-12-24 17:09:28 +00:00
|
|
|
args.Add("return_content", strconv.FormatBool(returnContent))
|
2016-12-21 19:32:28 +00:00
|
|
|
|
|
|
|
url := fmt.Sprintf(PathEndpoint, "getPage", path)
|
2016-12-24 17:09:28 +00:00
|
|
|
body, err := request(url, &args)
|
2016-12-21 19:32:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp Page
|
|
|
|
if err := json.Unmarshal(body.Result, &resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp, nil
|
|
|
|
}
|
|
|
|
|
2016-12-25 03:10:55 +00:00
|
|
|
// GetPageList get a list of pages belonging to a Telegraph account. Returns
|
|
|
|
// a PageList object, sorted by most recently created pages first.
|
2016-12-21 19:32:28 +00:00
|
|
|
func (account *Account) GetPageList(offset int, limit int) (*PageList, error) {
|
|
|
|
var args fasthttp.Args
|
|
|
|
|
2016-12-25 03:10:55 +00:00
|
|
|
// Access token of the Telegraph account.
|
|
|
|
args.Add("access_token", account.AccessToken) // required
|
2016-12-21 19:32:28 +00:00
|
|
|
|
|
|
|
// Sequential number of the first page to be returned.
|
2016-12-24 17:09:28 +00:00
|
|
|
args.Add("offset", strconv.Itoa(offset))
|
2016-12-21 19:32:28 +00:00
|
|
|
|
|
|
|
// Limits the number of pages to be retrieved.
|
2016-12-24 17:09:28 +00:00
|
|
|
args.Add("limit", strconv.Itoa(limit))
|
2016-12-21 19:32:28 +00:00
|
|
|
|
|
|
|
url := fmt.Sprintf(APIEndpoint, "getPageList")
|
2016-12-24 17:09:28 +00:00
|
|
|
body, err := request(url, &args)
|
2016-12-21 19:32:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp PageList
|
|
|
|
if err := json.Unmarshal(body.Result, &resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp, nil
|
|
|
|
}
|
|
|
|
|
2016-12-25 03:10:55 +00:00
|
|
|
// GetViews get the number of views for a Telegraph article. By default, the
|
|
|
|
// total number of page views will be returned. Returns a PageViews object
|
|
|
|
// on success.
|
2016-12-24 19:26:36 +00:00
|
|
|
func GetViews(path string, hour int, day int, month int, year int) (*PageViews, error) {
|
2016-12-21 19:32:28 +00:00
|
|
|
var args fasthttp.Args
|
|
|
|
|
|
|
|
if hour > -1 {
|
2016-12-25 03:10:55 +00:00
|
|
|
// If passed, the number of page views for the requested hour will
|
|
|
|
// be returned.
|
2016-12-21 19:32:28 +00:00
|
|
|
args.Add("hour", strconv.Itoa(hour))
|
2016-12-24 19:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if day > 0 {
|
2016-12-25 03:10:55 +00:00
|
|
|
// Required if hour is passed. If passed, the number of page views
|
|
|
|
// for the requested day will be returned.
|
2016-12-24 19:26:36 +00:00
|
|
|
args.Add("day", strconv.Itoa(day))
|
|
|
|
}
|
|
|
|
|
|
|
|
if month > 0 {
|
2016-12-25 03:10:55 +00:00
|
|
|
// Required if day is passed. If passed, the number of page views
|
|
|
|
// for the requested month will be returned.
|
2016-12-24 19:26:36 +00:00
|
|
|
args.Add("month", strconv.Itoa(month))
|
|
|
|
}
|
2016-12-24 17:09:28 +00:00
|
|
|
|
2016-12-24 19:26:36 +00:00
|
|
|
if year > 0 {
|
2016-12-25 03:10:55 +00:00
|
|
|
// Required if month is passed. If passed, the number of page views
|
|
|
|
// for the requested year will be returned.
|
2016-12-24 19:26:36 +00:00
|
|
|
args.Add("year", strconv.Itoa(year))
|
2016-12-21 19:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
url := fmt.Sprintf(PathEndpoint, "getViews", path)
|
2016-12-24 17:09:28 +00:00
|
|
|
body, err := request(url, &args)
|
2016-12-21 19:32:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp PageViews
|
|
|
|
if err := json.Unmarshal(body.Result, &resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp, nil
|
|
|
|
}
|