all repos — telegram-bot-api @ 9b085ce6a62e50f2a7c657517f36c6d7f0cfd2a7

Golang bindings for the Telegram Bot API

README.md (view raw)

  1# Golang bindings for the Telegram Bot API
  2
  3[![Go Reference](https://pkg.go.dev/badge/github.com/go-telegram-bot-api/telegram-bot-api/v5.svg)](https://pkg.go.dev/github.com/go-telegram-bot-api/telegram-bot-api/v5)
  4[![Test](https://github.com/go-telegram-bot-api/telegram-bot-api/actions/workflows/test.yml/badge.svg)](https://github.com/go-telegram-bot-api/telegram-bot-api/actions/workflows/test.yml)
  5
  6All methods are fairly self-explanatory, and reading the [godoc](https://pkg.go.dev/github.com/go-telegram-bot-api/telegram-bot-api/v5) page should
  7explain everything. If something isn't clear, open an issue or submit
  8a pull request.
  9
 10There are more tutorials and high-level information on the website, [go-telegram-bot-api.dev](https://go-telegram-bot-api.dev).
 11
 12The scope of this project is just to provide a wrapper around the API
 13without any additional features. There are other projects for creating
 14something with plugins and command handlers without having to design
 15all that yourself.
 16
 17Join [the development group](https://telegram.me/go_telegram_bot_api) if
 18you want to ask questions or discuss development.
 19
 20## Example
 21
 22First, ensure the library is installed and up to date by running
 23`go get -u github.com/go-telegram-bot-api/telegram-bot-api/v5`.
 24
 25This is a very simple bot that just displays any gotten updates,
 26then replies it to that chat.
 27
 28```go
 29package main
 30
 31import (
 32	"log"
 33
 34	tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
 35)
 36
 37func main() {
 38	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
 39	if err != nil {
 40		log.Panic(err)
 41	}
 42
 43	bot.Debug = true
 44
 45	log.Printf("Authorized on account %s", bot.Self.UserName)
 46
 47	u := tgbotapi.NewUpdate(0)
 48	u.Timeout = 60
 49
 50	updates := bot.GetUpdatesChan(u)
 51
 52	for update := range updates {
 53		if update.Message != nil { // If we got a message
 54			log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
 55
 56			msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
 57			msg.ReplyToMessageID = update.Message.MessageID
 58
 59			bot.Send(msg)
 60		}
 61	}
 62}
 63```
 64
 65If you need to use webhooks (if you wish to run on Google App Engine),
 66you may use a slightly different method.
 67
 68```go
 69package main
 70
 71import (
 72	"log"
 73	"net/http"
 74
 75	"github.com/go-telegram-bot-api/telegram-bot-api/v5"
 76)
 77
 78func main() {
 79	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
 80	if err != nil {
 81		log.Fatal(err)
 82	}
 83
 84	bot.Debug = true
 85
 86	log.Printf("Authorized on account %s", bot.Self.UserName)
 87
 88	wh, _ := tgbotapi.NewWebhookWithCert("https://www.example.com:8443/"+bot.Token, "cert.pem")
 89
 90	_, err = bot.Request(wh)
 91	if err != nil {
 92		log.Fatal(err)
 93	}
 94
 95	info, err := bot.GetWebhookInfo()
 96	if err != nil {
 97		log.Fatal(err)
 98	}
 99
100	if info.LastErrorDate != 0 {
101		log.Printf("Telegram callback failed: %s", info.LastErrorMessage)
102	}
103
104	updates := bot.ListenForWebhook("/" + bot.Token)
105	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
106
107	for update := range updates {
108		log.Printf("%+v\n", update)
109	}
110}
111```
112
113If you need, you may generate a self-signed certificate, as this requires
114HTTPS / TLS. The above example tells Telegram that this is your
115certificate and that it should be trusted, even though it is not
116properly signed.
117
118    openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes
119
120Now that [Let's Encrypt](https://letsencrypt.org) is available,
121you may wish to generate your free TLS certificate there.