// Content in a string format (for this example).
// Be sure to wrap every media in a tag, okay? Be easy.
const data = `
Hello, my name is Page, look at me!
Yes, you can embed youtube, vimeo and twitter widgets too!
`
func Example_fastStart() {
var err error
// Create new Telegraph account.
requisites := telegraph.Account{
ShortName: "toby3d", // required
// Author name/link can be epmty. So secure. Much anonymously. Wow.
AuthorName: "Maxim Lebedev", // optional
AuthorURL: "https://t.me/toby3d", // optional
}
client := telegraph.NewClient()
account, err = client.CreateAccount(requisites)
if err != nil {
// Do error check
}
// Make sure that you have saved acc.AuthToken for create new pages or make
// other actions by this account in next time!
// Format content to []telegraph.Node array. Input data can be string, []byte
// or io.Reader.
content, err = client.ContentFormat(data)
if err != nil {
// Do error check
}
// Boom!.. And your text will be understandable for Telegraph. MAGIC.
// Create new Telegraph page
pageData := telegraph.Page{
Title: "My super-awesome page", // required
Content: content, // required
// Not necessarily, but, hey, it's just an example.
AuthorName: account.AuthorName, // optional
AuthorURL: account.AuthorURL, // optional
}
page, err = client.CreatePage(pageData, false)
if err != nil {
// Do error check
}
// Show link from response on created page.
log.Println("Kaboom! Page created, look what happened:", page.URL)
}
Package telegraph has functions and types used for interacting with the Telegraph API.
Telegra.ph is a minimalist publishing tool that allows you to create richly formatted posts and push them to the Web
in just a click. Telegraph posts also get beautiful Instant View pages on Telegram.
To maintain the purity of the basic interface, we launched the @Telegraph bot for those who require advanced features.
This bot can help you manage your articles across any number of devices and get page view statistics for any Telegraph
page.
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.
package main
import (
"log"
"github.com/TechMinerApps/telegraph"
)
// Content in a string format (for this example).
// Be sure to wrap every media in a tag, okay? Be easy.
const data = `
Hello, my name is Page, look at me!
Yes, you can embed youtube, vimeo and twitter widgets too!
`
var (
account *telegraph.Account
page *telegraph.Page
content []telegraph.Node
)
func errCheck(err error) {
if err != nil {
log.Fatalln(err.Error())
}
}
func main() {
var err error
// Create new Telegraph account.
requisites := telegraph.Account{
ShortName: "toby3d", // required
// Author name/link can be epmty. So secure. Much anonymously. Wow.
AuthorName: "Maxim Lebedev", // optional
AuthorURL: "https://t.me/toby3d", // optional
}
client := telegraph.NewClient()
account, err = client.CreateAccount(requisites)
errCheck(err)
// Make sure that you have saved acc.AuthToken for create new pages or make
// other actions by this account in next time!
// Format content to []telegraph.Node array. Input data can be string, []byte
// or io.Reader.
content, err = client.ContentFormat(data)
errCheck(err)
// Boom!.. And your text will be understandable for Telegraph. MAGIC.
// Create new Telegraph page
pageData := telegraph.Page{
Title: "My super-awesome page", // required
Content: content, // required
// Not necessarily, but, hey, it's just an example.
AuthorName: account.AuthorName, // optional
AuthorURL: account.AuthorURL, // optional
}
page, err = client.CreatePage(pageData, false)
errCheck(err)
// Show link from response on created page.
log.Println("Kaboom! Page created, look what happened:", page.URL)
}
const (
// FieldShortName used as GetAccountInfo argument for getting account name. FieldShortName string = "short_name"
// FieldAuthorName used as GetAccountInfo argument for getting author name. FieldAuthorName string = "author_name"
// FieldAuthorURL used as GetAccountInfo argument for getting profile link. FieldAuthorURL string = "author_url"
// FieldAuthURL used as GetAccountInfo argument for getting URL to authorize a browser on telegra.ph. FieldAuthURL string = "auth_url"
// FieldPageCount used as GetAccountInfo argument for getting number of pages belonging to the Telegraph// account.
FieldPageCount string = "page_count"
)
var (
// ErrInvalidDataType is returned when ContentFormat function are passed a data argument of invalid type. ErrInvalidDataType = errors.New("invalid data type")
// ErrNoInputData is returned when any method get nil argument. ErrNoInputData = errors.New("no input data")
// ErrFloodWait is returned when recieving a flood error ErrFloodWait = errors.New("reaching telegraph flood limit")
)
type Account struct {
// Only returned by the createAccount and revokeAccessToken method. Access token of the Telegraph// account.
AccessToken string `json:"access_token"`
// 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"`
// 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"`
// Number of pages belonging to the Telegraph account. PageCount int `json:"page_count,omitempty"`
}
type Client interface {
// Account returns the account object client is using Account() *Account// Client return the http client using, not recommend using Client() *http.Client// ContentFormat format the input of string or byte to Node object ContentFormat(data interface{}) (n []Node, err error)
// CreateAccount is used to create a new account using provided info// It alse set the account info stored in client
CreateAccount(account Account) (*Account, error)
// CreatePage use the account info stored within client to create a new page CreatePage(page Page, returnContent bool) (*Page, error)
// EditAccountInfo update the account info according to input// It ignores the Token field in the input account object and use the one stored within client
EditAccountInfo(update Account) (*Account, error)
// EditPage update the Page provided EditPage(update Page, returnContent bool) (*Page, error)
// GetAccountInfo get the account info from the token stored in client object// It does not update the account info in client object
GetAccountInfo(fields ...string) (*Account, error)
// GetPage get the page object from provided url GetPage(path string, returnContent bool) (*Page, error)
// GetPageList GetPageList(offset, limit int) (*PageList, error)
// GetViews get the views count of specific page GetViews(path string, date time.Time) (*PageViews, error)
// RevokeAccessToken is used when accesstoken is compromises RevokeAccessToken() (*Account, error)
// SetSocksProxy SetSocksProxy(proxy string)
// SetHTTPProxy SetHTTPProxy(proxy string)
}
Client is the interface to act with telegraph API
The 9 method is identical to the 9 method provided at https://telegra.ph/api
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"`
}
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"`
// Name of the author, displayed below the title. 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"`
// Image URL of the page. ImageURL string `json:"image_url,omitempty"`
// Content of the page. Content []Node `json:"content,omitempty"`
// Number of page views for the page. Views int `json:"views"`
// Only returned if access_token passed. True, if the target Telegraph account can edit the page. CanEdit bool `json:"can_edit,omitempty"`
}
type PageList struct {
// Total number of pages belonging to the target Telegraph account. TotalCount int `json:"total_count"`
// Requested pages of the target Telegraph account. Pages []Page `json:"pages"`
}
PageList represents a list of Telegraph articles belonging to an account. Most recently created articles
first.
type Response struct {
Ok bool `json:"ok"`
Error string `json:"error,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
}
Response contains a JSON object, which always has a Boolean field ok. If ok equals true, the request was
successful, and the result of the query can be found in the result field. In case of an unsuccessful request, ok
equals false, and the error is explained in the error field (e.g. SHORT_NAME_REQUIRED).