diff --git a/README.md b/README.md index c172820..f92bb62 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,12 @@ # Telegraph API Go Package -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. So, this Go wrapper enables you to do all that easily. +![Telegraph API](https://telegra.ph/file/a086583f5b7b25cd428fb.jpg) +*This photo was originally uploaded at [Telegraph](https://telegra.ph/api)* -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. +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. +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 diff --git a/examples/example.go b/examples/example.go index 5baf3fe..efbba18 100644 --- a/examples/example.go +++ b/examples/example.go @@ -42,4 +42,13 @@ func main() { // Get total pages count in this way pcount := plist.TotalCount fmt.Println(pcount) + + // Let's upload a photo on telegraph using UploadFile function, your file's path will be it's parameter + path, err := telegraph.UploadFile("telegraphAPI.jpg") + if err != nil { + fmt.Println(err.Error()) + return + } + // returned path is everything that comes after 'https://telegra.ph' in the url, for example in our case, returned path was '/file/a086583f5b7b25cd428fb.jpg' which can be viewed at 'https://telegra.ph/file/a086583f5b7b25cd428fb.jpg' + fmt.Println("Uploaded photo can be viewed at:", "https://telegra.ph"+path) } diff --git a/examples/telegraphAPI.jpg b/examples/telegraphAPI.jpg new file mode 100644 index 0000000..e45ffc1 Binary files /dev/null and b/examples/telegraphAPI.jpg differ diff --git a/methods.go b/methods.go index 1286e8b..35107e0 100644 --- a/methods.go +++ b/methods.go @@ -1,8 +1,15 @@ package telegraph import ( + "bytes" "encoding/json" + "fmt" + "io" + "io/ioutil" + "mime/multipart" + "net/http" "net/url" + "os" "strconv" ) @@ -245,3 +252,50 @@ func GetViews(path string, opts *PageViewsOpts) (*PageViews, error) { } return &a, json.Unmarshal(r, &a) } + +// 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 UploadFile(filePath string) (path string, err error) { + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + file, err := os.Open(filePath) + if err != nil { + return "", err + } + part, err := writer.CreateFormFile("file", filePath) + if err != nil { + return "", err + } + if _, err = io.Copy(part, file); err != nil { + return "", err + } + if err = writer.Close(); err != nil { + return "", err + } + request, err := http.NewRequest(http.MethodPost, "https://telegra.ph/upload", body) + if err != nil { + return "", err + } + request.Header.Set("Content-Type", writer.FormDataContentType()) + var client http.Client + httpResponse, err := client.Do(request) + if err != nil { + return "", err + } + b, err := ioutil.ReadAll(httpResponse.Body) + if err != nil { + return "", err + } + rUpload := make([]Upload, 0) + if err := json.Unmarshal(b, &rUpload); err != nil { + m := map[string]string{} + if err := json.Unmarshal(b, &m); err != nil { + return "", err + } + return "", fmt.Errorf("failed to upload: %s", m["error"]) + } + return rUpload[0].Path, nil +} diff --git a/types.go b/types.go index 7ea9780..50e1c6d 100644 --- a/types.go +++ b/types.go @@ -9,29 +9,29 @@ type Account struct { // 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"` + 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"` + AuthUrl string `json:"auth_url,omitempty"` // Optional. Number of pages belonging to the Telegraph account. - PageCount int64 `json:"page_count"` + PageCount int64 `json:"page_count,omitempty"` } // Optional parameters for createAccount. type CreateAccountOpts struct { // Default author name used when creating new articles. - AuthorName string `json:"author_name"` + 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"` + AuthorUrl string `json:"author_url,omitempty"` } // Optional parameters for editAccountInfo. 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"` + ShortName string `json:"short_name,omitempty"` // Default author name used when creating new articles. - AuthorName string `json:"author_name"` + 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"` + AuthorUrl string `json:"author_url,omitempty"` } // This object represents a page on Telegraph. @@ -45,27 +45,27 @@ type Page struct { // Description of the page. Description string `json:"description"` // Optional. Name of the author, displayed below the title. - AuthorName string `json:"author_name"` + 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"` + AuthorUrl string `json:"author_url,omitempty"` // Optional. Image URL of the page. - ImageUrl string `json:"image_url"` + ImageUrl string `json:"image_url,omitempty"` // Optional. Content of the page. - Content []Node `json:"content"` + 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"` + CanEdit bool `json:"can_edit,omitempty"` } // Optional parameters for getPage and editPage. type PageOpts struct { // Optional. Name of the author, displayed below the title. - AuthorName string `json:"author_name"` + 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"` + 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"` + ReturnContent bool `json:"return_content,omitempty"` } // This object represents a list of Telegraph articles belonging to an account. Most recently created articles first. @@ -79,9 +79,9 @@ type PageList struct { // Optional parameters for getPageList. type PageListOpts struct { // - offset (type int64): Sequential number of the first page to be returned. (default = 0) - Offset int64 `json:"offset"` + Offset int64 `json:"offset,omitempty"` // - limit (type int64): Limits the number of pages to be retrieved. (default = 50) - Limit int64 `json:"limit"` + Limit int64 `json:"limit,omitempty"` } // This object represents the number of page views for a Telegraph article. @@ -93,13 +93,13 @@ type PageViews struct { // Optional parameters for getViews. 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"` + 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"` + 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"` + Day int64 `json:"day,omitempty"` // If passed, the number of page views for the requested hour will be returned. - Hour int64 `json:"hour"` + Hour int64 `json:"hour,omitempty"` } // Node is abstract object represents a DOM Node. It can be a String which represents a DOM text node or a @@ -119,3 +119,9 @@ type NodeElement struct { // List of child nodes for the DOM element. Children []Node `json:"children,omitempty"` } + +// This object represents a path of uploaded file. +type Upload struct { + // Path to the image. + Path string `json:"src"` +}