Skip to content

Commit 9453fd3

Browse files
author
Anonymous Indian
committed
telegraph-go v1.2
- added UploadFile method - fixed omitting empty json fields
1 parent 31fc417 commit 9453fd3

File tree

5 files changed

+96
-25
lines changed

5 files changed

+96
-25
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Telegraph API Go Package
22

3-
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.
3+
![Telegraph API](https://telegra.ph/file/a086583f5b7b25cd428fb.jpg)
4+
*This photo was originally uploaded at [Telegraph](https://telegra.ph/api)*
45

5-
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.
6+
Telegra.ph is a minimalist publishing tool that allows you to create richly formatted posts to the Web.
7+
Telegraph posts also get beautiful Instant View pages on Telegram. So, this Go wrapper enables you to do all that easily.
68

7-
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.
9+
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.
810

911
## Getting started
1012

examples/example.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,13 @@ func main() {
4242
// Get total pages count in this way
4343
pcount := plist.TotalCount
4444
fmt.Println(pcount)
45+
46+
// Let's upload a photo on telegraph using UploadFile function, your file's path will be it's parameter
47+
path, err := telegraph.UploadFile("telegraphAPI.jpg")
48+
if err != nil {
49+
fmt.Println(err.Error())
50+
return
51+
}
52+
// 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'
53+
fmt.Println("Uploaded photo can be viewed at:", "https://telegra.ph"+path)
4554
}

examples/telegraphAPI.jpg

213 KB
Loading

methods.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package telegraph
22

33
import (
4+
"bytes"
45
"encoding/json"
6+
"fmt"
7+
"io"
8+
"io/ioutil"
9+
"mime/multipart"
10+
"net/http"
511
"net/url"
12+
"os"
613
"strconv"
714
)
815

@@ -245,3 +252,50 @@ func GetViews(path string, opts *PageViewsOpts) (*PageViews, error) {
245252
}
246253
return &a, json.Unmarshal(r, &a)
247254
}
255+
256+
// Use this method to upload a file to Telegraph.
257+
// (You can upload some specific file formats like .jpg, .jpeg, .png, .gif, etc only)
258+
// Returns a path to the uploaded file i.e. everything that comes after https://telegra.ph/
259+
// - filePath (type string): location of the file to upload to Telegraph.
260+
// https://telegra.ph/upload
261+
func UploadFile(filePath string) (path string, err error) {
262+
body := &bytes.Buffer{}
263+
writer := multipart.NewWriter(body)
264+
file, err := os.Open(filePath)
265+
if err != nil {
266+
return "", err
267+
}
268+
part, err := writer.CreateFormFile("file", filePath)
269+
if err != nil {
270+
return "", err
271+
}
272+
if _, err = io.Copy(part, file); err != nil {
273+
return "", err
274+
}
275+
if err = writer.Close(); err != nil {
276+
return "", err
277+
}
278+
request, err := http.NewRequest(http.MethodPost, "https://telegra.ph/upload", body)
279+
if err != nil {
280+
return "", err
281+
}
282+
request.Header.Set("Content-Type", writer.FormDataContentType())
283+
var client http.Client
284+
httpResponse, err := client.Do(request)
285+
if err != nil {
286+
return "", err
287+
}
288+
b, err := ioutil.ReadAll(httpResponse.Body)
289+
if err != nil {
290+
return "", err
291+
}
292+
rUpload := make([]Upload, 0)
293+
if err := json.Unmarshal(b, &rUpload); err != nil {
294+
m := map[string]string{}
295+
if err := json.Unmarshal(b, &m); err != nil {
296+
return "", err
297+
}
298+
return "", fmt.Errorf("failed to upload: %s", m["error"])
299+
}
300+
return rUpload[0].Path, nil
301+
}

types.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,29 @@ type Account struct {
99
// 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.
1010
AuthorUrl string `json:"author_url"`
1111
// Optional. Only returned by the createAccount and revokeAccessToken method. Access token of the Telegraph account.
12-
AccessToken string `json:"access_token"`
12+
AccessToken string `json:"access_token,omitempty"`
1313
// 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.
14-
AuthUrl string `json:"auth_url"`
14+
AuthUrl string `json:"auth_url,omitempty"`
1515
// Optional. Number of pages belonging to the Telegraph account.
16-
PageCount int64 `json:"page_count"`
16+
PageCount int64 `json:"page_count,omitempty"`
1717
}
1818

1919
// Optional parameters for createAccount.
2020
type CreateAccountOpts struct {
2121
// Default author name used when creating new articles.
22-
AuthorName string `json:"author_name"`
22+
AuthorName string `json:"author_name,omitempty"`
2323
// 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.
24-
AuthorUrl string `json:"author_url"`
24+
AuthorUrl string `json:"author_url,omitempty"`
2525
}
2626

2727
// Optional parameters for editAccountInfo.
2828
type EditAccountInfoOpts struct {
2929
// 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.
30-
ShortName string `json:"short_name"`
30+
ShortName string `json:"short_name,omitempty"`
3131
// Default author name used when creating new articles.
32-
AuthorName string `json:"author_name"`
32+
AuthorName string `json:"author_name,omitempty"`
3333
// 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.
34-
AuthorUrl string `json:"author_url"`
34+
AuthorUrl string `json:"author_url,omitempty"`
3535
}
3636

3737
// This object represents a page on Telegraph.
@@ -45,27 +45,27 @@ type Page struct {
4545
// Description of the page.
4646
Description string `json:"description"`
4747
// Optional. Name of the author, displayed below the title.
48-
AuthorName string `json:"author_name"`
48+
AuthorName string `json:"author_name,omitempty"`
4949
// 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.
50-
AuthorUrl string `json:"author_url"`
50+
AuthorUrl string `json:"author_url,omitempty"`
5151
// Optional. Image URL of the page.
52-
ImageUrl string `json:"image_url"`
52+
ImageUrl string `json:"image_url,omitempty"`
5353
// Optional. Content of the page.
54-
Content []Node `json:"content"`
54+
Content []Node `json:"content,omitempty"`
5555
// Number of page views for the page.
5656
Views int64 `json:"views"`
5757
// Optional. Only returned if access_token passed. True, if the target Telegraph account can edit the page.
58-
CanEdit bool `json:"can_edit"`
58+
CanEdit bool `json:"can_edit,omitempty"`
5959
}
6060

6161
// Optional parameters for getPage and editPage.
6262
type PageOpts struct {
6363
// Optional. Name of the author, displayed below the title.
64-
AuthorName string `json:"author_name"`
64+
AuthorName string `json:"author_name,omitempty"`
6565
// 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.
66-
AuthorUrl string `json:"author_url"`
66+
AuthorUrl string `json:"author_url,omitempty"`
6767
// If true, a content field will be returned in the Page object (see: Content format).
68-
ReturnContent bool `json:"return_content"`
68+
ReturnContent bool `json:"return_content,omitempty"`
6969
}
7070

7171
// This object represents a list of Telegraph articles belonging to an account. Most recently created articles first.
@@ -79,9 +79,9 @@ type PageList struct {
7979
// Optional parameters for getPageList.
8080
type PageListOpts struct {
8181
// - offset (type int64): Sequential number of the first page to be returned. (default = 0)
82-
Offset int64 `json:"offset"`
82+
Offset int64 `json:"offset,omitempty"`
8383
// - limit (type int64): Limits the number of pages to be retrieved. (default = 50)
84-
Limit int64 `json:"limit"`
84+
Limit int64 `json:"limit,omitempty"`
8585
}
8686

8787
// This object represents the number of page views for a Telegraph article.
@@ -93,13 +93,13 @@ type PageViews struct {
9393
// Optional parameters for getViews.
9494
type PageViewsOpts struct {
9595
// Required if month is passed. If passed, the number of page views for the requested year will be returned.
96-
Year int64 `json:"year"`
96+
Year int64 `json:"year,omitempty"`
9797
// Required if day is passed. If passed, the number of page views for the requested month will be returned.
98-
Month int64 `json:"month"`
98+
Month int64 `json:"month,omitempty"`
9999
// Required if hour is passed. If passed, the number of page views for the requested day will be returned.
100-
Day int64 `json:"day"`
100+
Day int64 `json:"day,omitempty"`
101101
// If passed, the number of page views for the requested hour will be returned.
102-
Hour int64 `json:"hour"`
102+
Hour int64 `json:"hour,omitempty"`
103103
}
104104

105105
// 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 {
119119
// List of child nodes for the DOM element.
120120
Children []Node `json:"children,omitempty"`
121121
}
122+
123+
// This object represents a path of uploaded file.
124+
type Upload struct {
125+
// Path to the image.
126+
Path string `json:"src"`
127+
}

0 commit comments

Comments
 (0)