all repos — telegram-bot-api @ 1ad852c7652bb1c0226da1214678f9dc70dcf3d5

Golang bindings for the Telegram Bot API

README.md (view raw)

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