telegraphist

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2019 License: MIT Imports: 11 Imported by: 0

README

Telegraphist

CI status

Bindings for Telegram Bot API on Go. API v4.4 (29 July 2019) is supported. All methods and types represent their alter ego from Telegram Bot API so you would expect what you see in the official documentation. And you don't need to study new API to use this library.

Documentation

Usage

package main

import (
	"encoding/json"
	"fmt"
	"os"

	Telegraphist "github.com/xamut/telegraphist"
	Telegram "github.com/xamut/telegraphist/telegram"
)

func printResp(resp []byte, err error) {
	if err != nil {
		fmt.Println(err)
	}
	respAsJSON, _ := json.MarshalIndent(resp, "", "\t")
	fmt.Println(string(respAsJSON))
}

func main() {
	telegraphist, err = Telegraphist.NewClient(&Telegraphist.ClientConfig{
		BotToken: "<YOUR_BOT_TOKEN>",
	})

	if err != nil {
		fmt.Println(err)
	}

	printResp(telegraphist.GetMe()) // Simple method to obtain basic bot info
	                                // https://core.telegram.org/bots/api#getme
	                                // Returns an User struct

	printResp(telegraphist.SendMessage(&Telegram.SendMessageParams{ // Send a text message to a chat
		ChatID:  int64(<CHAT_ID>), // https://core.telegram.org/bots/api#sendmessage
		Message: "Hello there!",   // Returns a Message struct
	})) //

	photo, _ := os.Open("<PATH_TO_LOCAL_PHOTO>")                //
	printResp(telegraphist.SendPhoto(&Telegram.SendPhotoParams{ // Send a photo to a chat
		ChatID:  int64(<CHAT_ID>),    // https://core.telegram.org/bots/api#sendphoto
		Photo:   photo,               // Returns a Message struct
		Caption: "Just a nice photo", //
	})) //
}

Getting updates

Webhook
  • Find your IP
curl ifconfig.co
  • Use nip.io to pretend you have a domain, for example, "app.X.X.X.X.nip.io"

  • Generate a certificate, replace X.X.X.X with your IP:

openssl req -newkey rsa:2048 \
  -new -nodes -x509 \
  -days 3650 \
  -out cert.pem \
  -keyout key.pem \
  -subj "/O=Organization/CN=app.X.X.X.X.nip.io""
  • Run server
package main

import (
	"encoding/json"
	"fmt"
	"os"

	Telegraphist "github.com/xamut/telegraphist"
	Telegram "github.com/xamut/telegraphist/telegram"
)

func main() {
	telegraphist, err := Telegraphist.NewClient(&Telegraphist.ClientConfig{
		BotToken: "<YOUR_BOT_TOKEN>",
	})

	if err != nil {
		fmt.Println(err)
	}

	host := "https://app.X.X.X.X.nip.io" // For example: "https://example.com"
	webhookPath := "<YOUR_WEBHOOK_PATH>" // For example: "/telegram_webhook"

	cert, _ := os.Open("<PATH_TO_CERT.PEM>")
	ok, err = telegraphist.SetWebhook(&Telegram.SetWebhookParams{
		URL:         host + webhookPath,
		Certificate: cert,
	})

	if err != nil {
		fmt.Println(err)
	}

	if !ok {
		fmt.Println("Something went wrong and the webhook wasn't set up")
	}

	handlers := map[string]func(update telegram.Update) error{
		// "message":              func(update Telegram.Update) error {},
		// "edited_message":       func(update Telegram.Update) error {},
		// "channel_post":         func(update Telegram.Update) error {},
		// "edited_channel_post":  func(update Telegram.Update) error {},
		// "inline_query":         func(update Telegram.Update) error {},
		// "chosen_inline_result": func(update Telegram.Update) error {},
		// "callback_query":       func(update Telegram.Update) error {},
		// "shipping_query":       func(update Telegram.Update) error {},
		// "pre_checkout_query":   func(update Telegram.Update) error {},
		// "poll":                 func(update Telegram.Update) error {},
		"always": func(update Telegram.Update) error {
			js, _ := json.MarshalIndent(update, "", "\t")
			fmt.Println(string(js))
			return nil
		},
	}

	err = Telegraphist.NewServer(&Telegraphist.ServerConfig{
		EnableHTTPS: true,
		Webhook:     webhookPath,
	}, &handlers)

	if err != nil {
		fmt.Println(err)
	}
}

NOTE: Use this example only for testing purposes, for production use something like nginx or traefik to serve HTTPS (and/or to (re)generate Let's Encrypt certificate).

License

Telegraphist is released under the MIT License.

Documentation

Index

Constants

View Source
const (
	// DefaultBaseURL is endpoint URL to Telegram Bot API
	DefaultBaseURL string = "https://api.telegram.org/bot"
	// DefaultPort is port for serving the webhook
	DefaultPort int = 443
	// DefaultTimeout is period of time that will be allowed to wait a response
	DefaultTimeout time.Duration = 5 * time.Second
)

Variables

This section is empty.

Functions

func NewClient added in v0.3.0

func NewClient(config *ClientConfig) (*telegram.Client, error)

NewClient creates a new instance of API client with ClientConfig

func NewServer added in v0.3.0

func NewServer(config *ServerConfig, handlers *map[string]func(update telegram.Update) error) error

NewServer create a new instance of the web server to process Telegram API webhook

Types

type ClientConfig added in v0.3.0

type ClientConfig struct {
	BaseURL  string
	BotToken string
	Timeout  time.Duration
}

ClientConfig represents settings of API client

type Logger added in v0.3.0

type Logger interface {
	Printf(format string, v ...interface{})
}

type ServerConfig added in v0.3.0

type ServerConfig struct {
	Debug       bool
	EnableHTTPS bool
	Logger      Logger
	Port        int
	Timeout     time.Duration
	Webhook     string
}

ServerConfig represents settings of the webhook server

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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