Convert Figma logo to code with AI

go-telegram-bot-api logotelegram-bot-api

Golang bindings for the Telegram Bot API

5,703
879
5,703
151

Top Related Projects

3,921

Telebot is a Telegram bot framework in Go.

Golang bindings for the Telegram Bot API

Quick Overview

go-telegram-bot-api/telegram-bot-api is a Golang wrapper for the Telegram Bot API. It provides a simple and intuitive interface for developers to create Telegram bots using the Go programming language, offering full coverage of the Telegram Bot API methods and types.

Pros

  • Comprehensive coverage of Telegram Bot API features
  • Well-documented and easy to use for Go developers
  • Active community and regular updates
  • Supports both long polling and webhook methods for receiving updates

Cons

  • Limited built-in error handling, requiring manual error checks
  • Lack of advanced features like middleware or command routing
  • May require additional libraries for more complex bot functionalities
  • Documentation could be more extensive for advanced use cases

Code Examples

  1. Creating a new bot instance:
bot, err := tgbotapi.NewBotAPI("YOUR_BOT_TOKEN")
if err != nil {
    log.Panic(err)
}

bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
  1. Sending a simple message:
msg := tgbotapi.NewMessage(chatID, "Hello, I'm a Telegram bot!")
_, err := bot.Send(msg)
if err != nil {
    log.Println(err)
}
  1. Handling incoming messages:
u := tgbotapi.NewUpdate(0)
u.Timeout = 60

updates := bot.GetUpdatesChan(u)

for update := range updates {
    if update.Message != nil {
        log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)

        msg := tgbotapi.NewMessage(update.Message.Chat.ID, "I received your message!")
        bot.Send(msg)
    }
}

Getting Started

  1. Install the library:

    go get -u github.com/go-telegram-bot-api/telegram-bot-api/v5
    
  2. Import the library in your Go code:

    import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
    
  3. Create a new bot instance and start handling updates:

    bot, err := tgbotapi.NewBotAPI("YOUR_BOT_TOKEN")
    if err != nil {
        log.Panic(err)
    }
    
    u := tgbotapi.NewUpdate(0)
    u.Timeout = 60
    
    updates := bot.GetUpdatesChan(u)
    
    for update := range updates {
        if update.Message != nil {
            // Handle incoming messages
        }
    }
    

Competitor Comparisons

3,921

Telebot is a Telegram bot framework in Go.

Pros of telebot

  • More lightweight and focused on simplicity
  • Supports middleware for request handling
  • Offers a more idiomatic Go API design

Cons of telebot

  • Less comprehensive documentation compared to telegram-bot-api
  • Smaller community and fewer third-party resources
  • May lack some advanced features present in telegram-bot-api

Code Comparison

telegram-bot-api:

bot, err := tgbotapi.NewBotAPI("TOKEN")
u := tgbotapi.NewUpdate(0)
updates, err := bot.GetUpdatesChan(u)
for update := range updates {
    bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Hello!"))
}

telebot:

b, err := tb.NewBot(tb.Settings{Token: "TOKEN"})
b.Handle("/hello", func(m *tb.Message) {
    b.Send(m.Sender, "Hello!")
})
b.Start()

The telebot example demonstrates a more concise and idiomatic Go approach, utilizing a handler function for specific commands. The telegram-bot-api example shows a more traditional event loop style, which may be more familiar to some developers but requires more boilerplate code.

Both libraries offer similar core functionality, but telebot's API design tends to be more Go-like, while telegram-bot-api provides a more comprehensive set of features and documentation. The choice between them often depends on the specific requirements of the project and the developer's preferences.

Golang bindings for the Telegram Bot API

Pros of telegram-bot-api

  • More actively maintained with frequent updates
  • Better documentation and examples
  • Wider range of supported Telegram Bot API features

Cons of telegram-bot-api

  • Slightly more complex API, which may require a steeper learning curve
  • Larger codebase, potentially leading to increased compilation times

Code Comparison

telegram-bot-api:

bot, err := tgbotapi.NewBotAPI("YOUR_API_TOKEN")
if err != nil {
    log.Panic(err)
}
u := tgbotapi.NewUpdate(0)
u.Timeout = 60

telegram-bot-api:

bot, err := telebot.NewBot(telebot.Settings{
    Token:  "YOUR_API_TOKEN",
    Poller: &telebot.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
    log.Fatal(err)
}

Both libraries provide similar functionality for creating a bot instance and setting up updates. However, telegram-bot-api uses a more explicit approach with separate steps for creating the bot and configuring updates, while telegram-bot-api combines these steps in a single function call.

The main differences lie in the API design and available features. telegram-bot-api offers a more comprehensive set of functions and better documentation, making it easier to implement advanced bot features. On the other hand, telegram-bot-api has a simpler API that may be more approachable for beginners.

Ultimately, the choice between the two libraries depends on the specific requirements of your project and your familiarity with Go and Telegram bot development.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Golang bindings for the Telegram Bot API

Go Reference Test

All methods are fairly self-explanatory, and reading the godoc page should explain everything. If something isn't clear, open an issue or submit a pull request.

There are more tutorials and high-level information on the website, go-telegram-bot-api.dev.

The scope of this project is just to provide a wrapper around the API without any additional features. There are other projects for creating something with plugins and command handlers without having to design all that yourself.

Join the development group if you want to ask questions or discuss development.

Example

First, ensure the library is installed and up to date by running go get -u github.com/go-telegram-bot-api/telegram-bot-api/v5.

This is a very simple bot that just displays any gotten updates, then replies it to that chat.

package main

import (
	"log"

	tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

func main() {
	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
	if err != nil {
		log.Panic(err)
	}

	bot.Debug = true

	log.Printf("Authorized on account %s", bot.Self.UserName)

	u := tgbotapi.NewUpdate(0)
	u.Timeout = 60

	updates := bot.GetUpdatesChan(u)

	for update := range updates {
		if update.Message != nil { // If we got a message
			log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)

			msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
			msg.ReplyToMessageID = update.Message.MessageID

			bot.Send(msg)
		}
	}
}

If you need to use webhooks (if you wish to run on Google App Engine), you may use a slightly different method.

package main

import (
	"log"
	"net/http"

	"github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

func main() {
	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
	if err != nil {
		log.Fatal(err)
	}

	bot.Debug = true

	log.Printf("Authorized on account %s", bot.Self.UserName)

	wh, _ := tgbotapi.NewWebhookWithCert("https://www.example.com:8443/"+bot.Token, "cert.pem")

	_, err = bot.Request(wh)
	if err != nil {
		log.Fatal(err)
	}

	info, err := bot.GetWebhookInfo()
	if err != nil {
		log.Fatal(err)
	}

	if info.LastErrorDate != 0 {
		log.Printf("Telegram callback failed: %s", info.LastErrorMessage)
	}

	updates := bot.ListenForWebhook("/" + bot.Token)
	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)

	for update := range updates {
		log.Printf("%+v\n", update)
	}
}

If you need, you may generate a self-signed certificate, as this requires HTTPS / TLS. The above example tells Telegram that this is your certificate and that it should be trusted, even though it is not properly signed.

openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes

Now that Let's Encrypt is available, you may wish to generate your free TLS certificate there.