All Projects → olebedev → Go Tgbot

olebedev / Go Tgbot

Licence: apache-2.0
Golang telegram bot API wrapper, session-based router and middleware

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Go Tgbot

Novagram
An Object-Oriented PHP library for Telegram Bots
Stars: ✭ 112 (+24.44%)
Mutual labels:  api, bot, telegram
Clevergo
👅 CleverGo is a lightweight, feature rich and high performance HTTP router for Go.
Stars: ✭ 246 (+173.33%)
Mutual labels:  api, middleware, router
Foxify
The fast, easy to use & typescript ready web framework for Node.js
Stars: ✭ 138 (+53.33%)
Mutual labels:  api, middleware, router
Chi
lightweight, idiomatic and composable router for building Go HTTP services
Stars: ✭ 10,581 (+11656.67%)
Mutual labels:  api, middleware, router
Vk To Telegram
Utility to forward posts from VK through callback API to telegram channel or chat
Stars: ✭ 24 (-73.33%)
Mutual labels:  api, bot, telegram
Ex gram
Telegram Bot API low level API and framework
Stars: ✭ 103 (+14.44%)
Mutual labels:  api, bot, telegram
Mellow
Mellow can communicate with several APIs like Ombi, Sonarr, Radarr and Tautulli which are related to home streaming to use those services directly in your Discord client.
Stars: ✭ 193 (+114.44%)
Mutual labels:  api, bot, telegram
Telebot.nim
Async client for Telegram Bot API in pure Nim [Bot API 5.1]
Stars: ✭ 93 (+3.33%)
Mutual labels:  api, bot, telegram
Node Telegram Bot Api
Telegram Bot API for NodeJS
Stars: ✭ 5,782 (+6324.44%)
Mutual labels:  api, bot, telegram
Telegraf
Modern Telegram Bot Framework for Node.js
Stars: ✭ 5,178 (+5653.33%)
Mutual labels:  bot, middleware, telegram
Http Router
🎉 Release 2.0 is released! Very fast HTTP router for PHP 7.1+ (incl. PHP8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger)
Stars: ✭ 124 (+37.78%)
Mutual labels:  swagger, middleware, router
Copper
Copper is a set of Go packages that help you build backend APIs quickly and with less boilerplate.
Stars: ✭ 35 (-61.11%)
Mutual labels:  api, middleware, router
Pure Http
✨ The simple web framework for Node.js with zero dependencies.
Stars: ✭ 139 (+54.44%)
Mutual labels:  api, middleware, router
Diet
A tiny, fast and modular node.js web framework. Good for making fast & scalable apps and apis.
Stars: ✭ 394 (+337.78%)
Mutual labels:  api, middleware, router
Altair
Lightweight and Robust API Gateway written in Go
Stars: ✭ 34 (-62.22%)
Mutual labels:  api, middleware, router
Telegram Test Api
Simple implimentation of telegram API which can be used for testing telegram bots
Stars: ✭ 42 (-53.33%)
Mutual labels:  api, bot, telegram
Dorado
基于Netty4开发的简单、轻量级、高性能的的Http restful api server
Stars: ✭ 65 (-27.78%)
Mutual labels:  swagger, router
Slacko
A neat interface for Slack
Stars: ✭ 64 (-28.89%)
Mutual labels:  api, bot
Node Sdk
An official module for interacting with the top.gg API
Stars: ✭ 90 (+0%)
Mutual labels:  api, bot
Flowa
🔥Service level control flow for Node.js
Stars: ✭ 66 (-26.67%)
Mutual labels:  api, router

go-tgbot godoc

Pure Golang telegram bot API wrapper generated from swagger definition, session-based routing and middlewares.

Usage benefits

  1. No need to learn any other library API. You will use methods with payload exactly like it presented on telegram bot API description page. With only couple trade-offs, b/c of telegram bot API is generics a bit.
  2. All models and methods are being supported. The models and methods were generated from swagger.yaml description file. So, new entities/methods could be added by describing in the YAML swagger file. This approach allows validating the description, avoid typos and develop fast.
  3. easyjson is plugged. So, it's fast.
  4. context.Context based HTTP client
  5. Session-based routing, not only message text based.

Client

Client package could be used as regular go-swagger client library without using Router. There are the only two additional features over go-swagger, a possibility to setup token by default(It solved as an interface checking) and API throttling for 30 calls per second(see more info).

Example:

package main

import (
	"context"
	"flag"
	"log"
	"time"

	tgbot "github.com/olebedev/go-tgbot"
	"github.com/olebedev/go-tgbot/client/users"
)

var token *string

func main() {
	token = flag.String("token", "", "telegram bot token")
	flag.Parse()

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	api := tgbot.NewClient(ctx, *token)

	log.Println(api.Users.GetMe(nil))

	// Also, every calls could be done with given context
	ctx, cancel = context.WithTimeout(ctx, 10*time.Second)
	defer cancel()

	_, err := api.Users.GetMe(
		users.NewGetMeParams().
			WithContext(ctx),
	)

	if err != nil {
		// users.NewGetMeBadRequest()
		if e, ok := err.(*users.GetMeBadRequest); ok {
			log.Println(e.Payload.ErrorCode, e.Payload.Description)
		}
	}
}

Since swagger covers many other platforms/technologies the same libraries could be generated for them too. See the source here - swagger.yaml.
Also, have a look at Swagger UI for telegram API(master branch).

Router

The Router allows binding between kinds of updates and handlers, which are being checked via regexp. Router includes client API library as embedded struct. Example:

package main

import (
	"context"
	"flag"
	"log"
	"os"

	tgbot "github.com/olebedev/go-tgbot"
	"github.com/olebedev/go-tgbot/client/messages"
	"github.com/olebedev/go-tgbot/models"
)

var token *string

func main() {
	token = flag.String("token", "", "telegram bot token")
	flag.Parse()

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	r := tgbot.New(ctx, *token)

	// setup global middleware
	r.Use(tgbot.Recover)
	r.Use(tgbot.Logger(os.Stdout))

	// modify path to be able to match user's commands via router
	r.Use(func(c *tgbot.Context) error {
		c.Path = c.Path + c.Text
		return nil
	})

	// bind handler
	r.Bind(`^/message/(?:.*)/text/start(?:\s(.*))?$`, func(c *tgbot.Context) error {
		log.Println(c.Capture)             // - ^ from path
		log.Println(c.Update.Message.Text) // or c.Text

		// send greeting message back
		message := "hi there what's up"
		resp, err := r.Messages.SendMessage(
			messages.NewSendMessageParams().WithBody(&models.SendMessageBody{
				Text:   &message,
				ChatID: c.Update.Message.Chat.ID,
			}),
		)
		if err != nil {
			return err
		}
		if resp != nil {
			log.Println(resp.Payload.Result.MessageID)
		}
		return nil
	})

	if err := r.Poll(ctx, models.AllowedUpdateMessage); err != nil {
		log.Fatal(err)
	}
}

Default string representation of any kind of an update could be found here - router.go.

Router implements http.Handler interface to be able to serve HTTP as well. But, it's not recommended because webhooks are much much slower than polling.

More examples can be found at godoc.


See the documentation for more details.

LICENSE

http://www.apache.org/licenses/LICENSE-2.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].