Code Monkey home page Code Monkey logo

telegraph's Introduction

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

  • 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/v2

Documentation

Docs can be found here : Go Reference

Example

A project based on this library can be found here : Telegraph Go Bot

Usage

This is pretty straightforward. First, import the library

import "github.com/StarkBotsIndustries/telegraph/v2"

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: []telegraph.Node{
			"Hi ",
			telegraph.NodeElement{
				Tag:      "b",
				Children: []telegraph.Node{"Brothers"},
			},
			" and ",
			telegraph.NodeElement{
				Tag:      "code",
				Children: []telegraph.Node{"Sisters"},
			},
		},
		AccessToken: "abcdefghijklmnopqrstuvwxyz",
	})

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

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

You can also directly use HTML using HTMLContent field

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

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"

Raw Get Request

opts := telegraph.CreateAccountOpts{ShortName: "Durov Uncle"}
acc, err := telegraph.Get("createAccount", opts)

acc.AccessToken
>>> "abcdefghijklmnopqrstuvwxyz"

Raw Post Request

opts := telegraph.CreateAccountOpts{ShortName: "Durov Uncle"}
acc, err := telegraph.Post("createAccount", opts)

acc.AccessToken
>>> "abcdefghijklmnopqrstuvwxyz"

Community and Support

Telegram Channel - StarkBots

Telegram Chat - StarkBotsChat

Copyright and License

:)

ForTheBadge made-with-go

ForTheBadge built-with-love

ForTheBadge makes-people-smile

telegraph's People

Contributors

shivangkakkar avatar thehellruler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.