telegraph

package module
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2022 License: MIT Imports: 12 Imported by: 2

README

Telegraph API Go Package

Telegraph API This photo was originally uploaded at Telegraph

Telegra.ph is a minimalist publishing tool that allows you to create richly formatted posts to the Web. Telegraph posts also get beautiful Instant View pages on Telegram. So, this Go wrapper enables you to do all that easily.

Anyone can enjoy the simplicity of Telegraph publishing, not just Telegram users. For this reason, all developers are welcome to use this Telegraph API to create bots like @Telegraph for any other platform, or even standalone interfaces and this package is to make their work easier.

Getting started

Download the library with the standard go get command:

go get github.com/anonyindian/telegraph-go
Example

Examples can be found in the examples directory.

Documentation

GoDoc

Full go doc style documentation for the package can be viewed online without installing this package by using the GoDoc site here: https://pkg.go.dev/github.com/anonyindian/telegraph-go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InvokeRequest added in v1.2.1

func InvokeRequest(method string, params url.Values) (json.RawMessage, error)

func UploadFile added in v1.2.0

func UploadFile(filePath string) (string, error)

Use this method to upload a file to Telegraph. (You can upload some specific file formats like .jpg, .jpeg, .png, .gif, etc only) Returns a path to the uploaded file i.e. everything that comes after https://telegra.ph/ - filePath (type string): location of the file to upload to Telegraph. https://telegra.ph/upload

func UploadFileByBytes added in v1.2.1

func UploadFileByBytes(content []byte) (string, error)

Use this method to upload a file to Telegraph. (You can upload some specific file formats like .jpg, .jpeg, .png, .gif, etc only) Returns a path to the uploaded file i.e. everything that comes after https://telegra.ph/ - filePath (type string): location of the file to upload to Telegraph. https://telegra.ph/upload

Types

type Account

type Account struct {
	// Account name, helps users with several accounts remember which they are currently using. Displayed to the user above the "Edit/Publish" button on Telegra.ph, other users don't see this name.
	ShortName string `json:"short_name"`
	// Default author name used when creating new articles.
	AuthorName string `json:"author_name"`
	// 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.
	AuthorUrl string `json:"author_url"`
	// Optional. Only returned by the createAccount and revokeAccessToken method. Access token of the Telegraph account.
	AccessToken string `json:"access_token,omitempty"`
	// Optional. URL to authorize a browser on telegra.ph and connect it to a Telegraph account. This URL is valid for only one use and for 5 minutes only.
	AuthUrl string `json:"auth_url,omitempty"`
	// Optional. Number of pages belonging to the Telegraph account.
	PageCount int64 `json:"page_count,omitempty"`
}

This object represents a Telegraph account.

func CreateAccount

func CreateAccount(shortName string, opts *CreateAccountOpts) (*Account, error)

Use this method to create a new Telegraph account. Most users only need one account, but this can be useful for channel administrators who would like to keep individual author names and profile links for each of their channels. On success, returns an Account object with the regular fields and an additional access_token field. - shortName (type string): Account name, helps users with several accounts remember which they are currently using. Displayed to the user above the "Edit/Publish" button on Telegra.ph, other users don't see this name. - opts (type CreateAccountOpts): All optional parameters. https://telegra.ph/api#createAccount

func EditAccountInfo

func EditAccountInfo(accessToken string, opts *EditAccountInfoOpts) (*Account, error)

Use this method to update information about a Telegraph account. Pass only the parameters that you want to edit. On success, returns an Account object with the default fields. - accessToken (type string): Access token of the Telegraph account. - opts (type EditAccountInfoOpts): All optional parameters. https://telegra.ph/api#editAccountInfo

func GetAccountInfo

func GetAccountInfo(accessToken string) (*Account, error)

Use this method to get information about a Telegraph account. Returns an Account object on success. - accessToken (type string): Access token of the Telegraph account. https://telegra.ph/api#getAccountInfo

func RevokeAccessToken

func RevokeAccessToken(accessToken string) (*Account, error)

Use this method to revoke access_token and generate a new one, for example, if the user would like to reset all connected sessions, or you have reasons to believe the token was compromised. On success, returns an Account object with new access_token and auth_url fields. - accessToken (type string): Access token of the Telegraph account. https://telegra.ph/api#revokeAccessToken

func (*Account) CreatePage

func (a *Account) CreatePage(title string, content string, opts *PageOpts) (*Page, error)

Helper function to easily call CreatePage by an account.

func (*Account) EditInfo

func (a *Account) EditInfo(opts *EditAccountInfoOpts) (*Account, error)

Helper function to easily call EditAccountInfo by an account.

func (*Account) EditPage

func (a *Account) EditPage(path string, title string, content string, opts *PageOpts) (*Page, error)

Helper function to easily call EditPage by an account with previous author_name and author_url.

func (*Account) GetInfo

func (a *Account) GetInfo() (*Account, error)

Helper function to easily call GetAccountInfo by an account.

func (*Account) GetPageList

func (a *Account) GetPageList(opts *PageListOpts) (*PageList, error)

Helper function to easily call GetPageList by an account.

func (*Account) RevokeAccessToken

func (a *Account) RevokeAccessToken() (*Account, error)

Helper function to easily call RevokeAccessToken by an account.

type Body

type Body struct {
	// Ok: if true, request was successful, and result can be found in the Result field.
	// If false, error can be explained in Error field.
	Ok bool `json:"ok"`
	// Error: contains a human-readable description of the error result.
	Error string `json:"error"`
	// Result: result of requests (if Ok)
	Result json.RawMessage `json:"result"`
}

type CreateAccountOpts

type CreateAccountOpts struct {
	// Default author name used when creating new articles.
	AuthorName string `json:"author_name,omitempty"`
	// Optional. URL to authorize a browser on telegra.ph and connect it to a Telegraph account. This URL is valid for only one use and for 5 minutes only.
	AuthorUrl string `json:"author_url,omitempty"`
}

Optional parameters for createAccount.

type EditAccountInfoOpts

type EditAccountInfoOpts struct {
	// Account name, helps users with several accounts remember which they are currently using. Displayed to the user above the "Edit/Publish" button on Telegra.ph, other users don't see this name.
	ShortName string `json:"short_name,omitempty"`
	// Default author name used when creating new articles.
	AuthorName string `json:"author_name,omitempty"`
	// 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.
	AuthorUrl string `json:"author_url,omitempty"`
}

Optional parameters for editAccountInfo.

type Node

type Node interface{}

Node is abstract object represents a DOM Node. It can be a String which represents a DOM text node or a NodeElement object.

func ContentFormat

func ContentFormat(data interface{}) (n []Node, err error)

type NodeElement

type NodeElement struct {
	// Name of the DOM element. Available tags: a, aside, b, blockquote, br, code, em, figcaption, figure,
	// h3, h4, hr, i, iframe, img, li, ol, p, pre, s, strong, u, ul, video.
	Tag string `json:"tag"`

	// Attributes of the DOM element. Key of object represents name of attribute, value represents value
	// of attribute. Available attributes: href, src.
	Attrs map[string]string `json:"attrs,omitempty"`

	// List of child nodes for the DOM element.
	Children []Node `json:"children,omitempty"`
}

NodeElement represents a DOM element node.

type Page

type Page struct {
	// Path to the page.
	Path string `json:"path"`
	// URL of the page.
	Url string `json:"url"`
	// Title of the page.
	Title string `json:"title"`
	// Description of the page.
	Description string `json:"description"`
	// Optional. Name of the author, displayed below the title.
	AuthorName string `json:"author_name,omitempty"`
	// Optional. 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.
	AuthorUrl string `json:"author_url,omitempty"`
	// Optional. Image URL of the page.
	ImageUrl string `json:"image_url,omitempty"`
	// Optional. Content of the page.
	Content []Node `json:"content,omitempty"`
	// Number of page views for the page.
	Views int64 `json:"views"`
	// Optional. Only returned if access_token passed. True, if the target Telegraph account can edit the page.
	CanEdit bool `json:"can_edit,omitempty"`
}

This object represents a page on Telegraph.

func CreatePage

func CreatePage(accessToken string, title string, content string, opts *PageOpts) (*Page, error)

Use this method to create a new Telegraph page. On success, returns a Page object. - accessToken (type string): Access token of the Telegraph account. - title (type string): Page title. - content (type string): Content of the page (Array of Node, up to 64 KB converted into a json string). - opts (type PageOpts): All optional parameters. https://telegra.ph/api#createPage

func EditPage

func EditPage(accessToken string, path string, title string, content string, opts *PageOpts) (*Page, error)

Use this method to edit an existing Telegraph page. On success, returns a Page object. - accessToken (type string): Access token of the Telegraph account. - path (type string): Path to the page. - title (type string): Page title. - content (type string): Content of the page (Array of Node, up to 64 KB converted into a json string). - opts (type PageOpts): All optional parameters. https://telegra.ph/api#editPage

func GetPage

func GetPage(path string, returnContent bool) (*Page, error)

Use this method to get a Telegraph page. On success, returns a Page object. - path (type string): Path to the Telegraph page (in the format Title-12-31, i.e. everything that comes after http://telegra.ph/). - returnContent (type bool): If true, content field will be returned in Page object. https://telegra.ph/api#getPage

func (*Page) Get

func (p *Page) Get(returnContent bool) (*Page, error)

Helper function to easily get page.

func (*Page) GetViews

func (p *Page) GetViews(opts *PageViewsOpts) (*PageViews, error)

Helper function to easily call GetViews in a page.

type PageList

type PageList struct {
	// Total number of pages belonging to the target Telegraph account.
	TotalCount int64 `json:"total_count"`
	// Requested pages of the target Telegraph account.
	Pages []Page `json:"pages"`
}

This object represents a list of Telegraph articles belonging to an account. Most recently created articles first.

func GetPageList

func GetPageList(accessToken string, opts *PageListOpts) (*PageList, error)

Use this method to get a list of pages belonging to a Telegraph account. Returns a PageList object, sorted by most recently created pages first. - accessToken (type string): Access token of the Telegraph account. - opts https://telegra.ph/api#getPageList

type PageListOpts

type PageListOpts struct {
	// - offset (type int64): Sequential number of the first page to be returned. (default = 0)
	Offset int64 `json:"offset,omitempty"`
	// - limit (type int64): Limits the number of pages to be retrieved. (default = 50)
	Limit int64 `json:"limit,omitempty"`
}

Optional parameters for getPageList.

type PageOpts

type PageOpts struct {
	// Optional. Name of the author, displayed below the title.
	AuthorName string `json:"author_name,omitempty"`
	// Optional. 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.
	AuthorUrl string `json:"author_url,omitempty"`
	// If true, a content field will be returned in the Page object (see: Content format).
	ReturnContent bool `json:"return_content,omitempty"`
}

Optional parameters for getPage and editPage.

type PageViews

type PageViews struct {
	// Number of page views for the target page.
	Views int64 `json:"views"`
}

This object represents the number of page views for a Telegraph article.

func GetViews

func GetViews(path string, opts *PageViewsOpts) (*PageViews, error)

Use this method to get the number of views for a Telegraph article. Returns a PageViews object on success. By default, the total number of page views will be returned. - path (type string): Path to the Telegraph page (in the format Title-12-31, i.e. everything that comes after http://telegra.ph/). - opts (type PageViewsOpts): All optional parameters. https://telegra.ph/api#getViews

type PageViewsOpts

type PageViewsOpts struct {
	// Required if month is passed. If passed, the number of page views for the requested year will be returned.
	Year int64 `json:"year,omitempty"`
	// Required if day is passed. If passed, the number of page views for the requested month will be returned.
	Month int64 `json:"month,omitempty"`
	// Required if hour is passed. If passed, the number of page views for the requested day will be returned.
	Day int64 `json:"day,omitempty"`
	// If passed, the number of page views for the requested hour will be returned.
	Hour int64 `json:"hour,omitempty"`
}

Optional parameters for getViews.

type Upload added in v1.2.0

type Upload struct {
	// Path to the image.
	Path string `json:"src"`
}

This object represents a path of uploaded file.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL