telegraph

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2022 License: GPL-3.0 Imports: 12 Imported by: 2

README

Golang Bindings for Telegraph API

⭐️ A star from you, means a lot!

This library represents a convenient wrapper around the Telegra.ph API. This is my first project in golang, so I would love to know my mistakes.

Features

Here are the most important aspects, shown as pros and cons:

Pros
  • Auto-generated from API, leaving less space for mistakes.
  • Very easy to use.
  • Upload media to Telegraph, the method which isn't included in the official API.
  • Helpful methods like Prettify() to pretty-print any object or convert object to JSON.
  • In-built HTML to NodeElement conversion so you can directly use strings with embedded HTML
  • Highly documented (doc strings) for IDEs like VSCode.
Cons
  • Types Node and NodeElement are not implemented.
  • No custom error handling. Not much panicking. Though official errors will be returned as Error object. But I guess that's conventional, and maybe convenient?
  • The bugs you are going to find.

Installing

Just install it using the standard go get command.

go get github.com/StarkBotsIndustries/telegraph@v1.0.0

Documentation

Docs can be found here : Go Reference

Usage

This is pretty straightforward. First, import the library

import "github.com/StarkBotsIndustries/telegraph"

Now you can call any methods. Let's say, CreateAccount?

acc, err := telegraph.CreateAccount(&telegraph.CreateAccountOpts{ShortName: "Go is Love"})

acc.AccessToken
>>> abcdefghijklmnopqrstuvwxyz

acc.ShortName
>>> Go is Love

Or CreatePage

page, err := telegraph.CreatePage(&telegraph.CreatePageOpts{
    Title: "My First Page", 
    Content: "Hi <b>Brothers</b> and <code>Sisters</code>", 
    AccessToken: "abcdefghijklmnopqrstuvwxyz",
})

page.URL
>>> https://telegra.ph/My-First-Page-02-20

page.Path
>>> My-First-Page-02-20

Pretty Print an Object / Convert to JSON

acc, err := telegraph.CreateAccount(&telegraph.CreateAccountOpts{ShortName: "Go is Love"})
prettifiedObject := telegraph.Prettify(acc)

prettifiedObject
>>> {
>>>     "short_name": "Go is Love",
>>>     "author_name": "",
>>>     "author_url": "",
>>>     "access_token": "abcdefghijklmnopqrstuvwxyz",
>>>     "auth_url": "https://edit.telegra.ph/auth/lmnopqrstuvwxyzabcdefghijk",
>>>     "page_count": 0
>>> }

Upload Media to Telegraph

file, _ := os.Open("file.jpg")
// os.File is io.Reader so just pass it.
link, _ := telegraph.Upload(file, "photo")

link
>>> https://telegra.ph/file/abcdefghijklmnopqrstuvwxyz.jpg

Community and Support

Telegram Channel - StarkBots

Telegram Chat - StarkBotsChat

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTMLToNodeString

func HTMLToNodeString(html string) string

func Prettify

func Prettify(t interface{}) string

func Upload

func Upload(f io.Reader, mediaType string) (string, error)

Upload photo/video to Telegra.ph on the '/upload' endpoint. Media type should either be "video" or "photo". "Animation" is considered "video" here.

Types

type Account

type Account struct {
	ShortName   string `json:"short_name"`
	AuthorName  string `json:"author_name"`
	AuthorURL   string `json:"author_url"`
	AccessToken string `json:"access_token"`
	AuthURL     string `json:"auth_url"`
	PageCount   int64  `json:"page_count"`
}

Account implements the Account type of Telegraph API

func CreateAccount

func CreateAccount(opts *CreateAccountOpts) (acc *Account, err error)

CreateAccount using Telegraph API. Returns Account object

func EditAccountInfo

func EditAccountInfo(opts *EditAccountInfoOpts) (acc *Account, err error)

EditAccountInfo using Telegraph API. Returns Account object

func GetAccountInfo

func GetAccountInfo(opts *GetAccountInfoOpts) (acc *Account, err error)

GetAccountInfo using Telegraph API. Returns Account object

func RevokeAccessToken

func RevokeAccessToken(opts *RevokeAccessTokenOpts) (acc *Account, err error)

RevokeAccessToken using Telegraph API. Returns Account object

type AllValueTypes

type AllValueTypes struct {
	ShortName   string `json:"short_name"`
	AuthorName  string `json:"author_name"`
	AuthorURL   string `json:"author_url"`
	AccessToken string `json:"access_token"`
	AuthURL     string `json:"auth_url"`
	PageCount   int64  `json:"page_count"`
	TotalCount  int64  `json:"total_count"`
	Pages       []Page `json:"pages"`
	Path        string `json:"path"`
	URL         string `json:"url"`
	Title       string `json:"title"`
	Description string `json:"description"`
	ImageURL    string `json:"image_url"`
	Content     string `json:"content"`
	Views       int64  `json:"views"`
	CanEdit     bool   `json:"can_edit"`
}

A way to set defaults to their zero types instead of nil Know a better way to do this?

type CreateAccountOpts

type CreateAccountOpts struct {
	// Required. 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
	// Optional. Default author name used when creating new articles.
	AuthorName string
	// Optional. Default 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
}

CreateAccountOpts is the set of fields for 'Telegraph.CreateAccount'

type CreatePageOpts

type CreatePageOpts struct {
	// Required. Access token of the Telegraph account.
	AccessToken string
	// Required. Page title.
	Title string
	// Optional. Author name, displayed below the article's title.
	AuthorName string
	// 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
	// Required. Content of the page.
	Content string
	// Optional. If true, a content field will be returned in the Page object (see: Content format).
	ReturnContent bool
}

CreatePageOpts is the set of fields for 'Telegraph.CreatePage'

type EditAccountInfoOpts

type EditAccountInfoOpts struct {
	// Required. Access token of the Telegraph account.
	AccessToken string
	// Optional. New account name.
	ShortName string
	// Optional. New default author name used when creating new articles.
	AuthorName string
	// Optional. New default 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
}

EditAccountInfoOpts is the set of fields for 'Telegraph.EditAccountInfo'

type EditPageOpts

type EditPageOpts struct {
	// Required. Access token of the Telegraph account.
	AccessToken string
	// Required. Path to the page.
	Path string
	// Required. Page title.
	Title string
	// Required. Content of the page.
	Content string
	// Optional. Author name, displayed below the article's title.
	AuthorName string
	// 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
	// Optional. If�true, a content field will be returned in the Page object.
	ReturnContent bool
}

EditPageOpts is the set of fields for 'Telegraph.EditPage'

type Error

type Error struct {
	Error string `json:"error"`
}

type GetAccountInfoOpts

type GetAccountInfoOpts struct {
	// Required. Access token of the Telegraph account.
	AccessToken string
	// Optional. List of account fields to return. Available fields: short_name, author_name, author_url, auth_url, page_count.
	Fields []string
}

GetAccountInfoOpts is the set of fields for 'Telegraph.GetAccountInfo'

type GetPageListOpts

type GetPageListOpts struct {
	// Required. Access token of the Telegraph account.
	AccessToken string
	// Optional. Sequential number of the first page to be returned.
	Offset int64
	// Optional. Limits the number of pages to be retrieved.
	Limit int64
}

GetPageListOpts is the set of fields for 'Telegraph.GetPageList'

type GetPageOpts

type GetPageOpts struct {
	// Required. Path to the Telegraph page (in the format Title-12-31, i.e. everything that comes after http://telegra.ph/).
	Path string
	// Optional. If�true, content field will be returned in Page object.
	ReturnContent bool
}

GetPageOpts is the set of fields for 'Telegraph.GetPage'

type GetViewsOpts

type GetViewsOpts struct {
	// Required. Path to the Telegraph page (in the format Title-12-31, where 12 is the month and 31 the day the article was first published).
	Path string
	// Required if month is passed. If passed, the number of page views for the requested year will be returned.
	Year int64
	// Required if day is passed. If passed, the number of page views for the requested month will be returned.
	Month int64
	// Required if hour is passed. If passed, the number of page views for the requested day will be returned.
	Day int64
	// Optional. If passed, the number of page views for the requested hour will be returned.
	Hour int64
}

GetViewsOpts is the set of fields for 'Telegraph.GetViews'

type Page

type Page struct {
	Path        string `json:"path"`
	URL         string `json:"url"`
	Title       string `json:"title"`
	Description string `json:"description"`
	AuthorName  string `json:"author_name"`
	AuthorURL   string `json:"author_url"`
	ImageURL    string `json:"image_url"`
	Content     string `json:"content"`
	Views       int64  `json:"views"`
	CanEdit     bool   `json:"can_edit"`
}

Page implements the Page type of Telegraph API

func CreatePage

func CreatePage(opts *CreatePageOpts) (page *Page, err error)

CreatePage using Telegraph API. Returns Page object

func EditPage

func EditPage(opts *EditPageOpts) (page *Page, err error)

EditPage using Telegraph API. Returns Page object

func GetPage

func GetPage(opts *GetPageOpts) (page *Page, err error)

GetPage using Telegraph API. Returns Page object

type PageList

type PageList struct {
	TotalCount int64  `json:"total_count"`
	Pages      []Page `json:"pages"`
}

PageList implements the PageList type of Telegraph API

func GetPageList

func GetPageList(opts *GetPageListOpts) (pl *PageList, err error)

GetPageList using Telegraph API. Returns PageList object

type PageViews

type PageViews struct {
	Views int64 `json:"views"`
}

PageViews implements the PageViews type of Telegraph API

func GetViews

func GetViews(opts *GetViewsOpts) (views *PageViews, err error)

GetViews using Telegraph API. Returns PageViews object

type ResultStruct

type ResultStruct struct {
	Ok     bool          `json:"ok"`
	Result AllValueTypes `json:"result,omitempty"`
	Error  string        `json:"error,omitempty"`
}

type RevokeAccessTokenOpts

type RevokeAccessTokenOpts struct {
	// Required. Access token of the Telegraph account.
	AccessToken string
}

RevokeAccessTokenOpts is the set of fields for 'Telegraph.RevokeAccessToken'

type Source

type Source struct {
	Src string `json:"src"`
}

type Telegraph

type Telegraph struct {
	Account   `json:"account"`
	PageList  `json:"pageList"`
	Page      `json:"page"`
	PageViews `json:"pageViews"`
}

type UploadResult

type UploadResult struct {
	Source []Source
}

Jump to

Keyboard shortcuts

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