all repos — telegram-bot-api @ 4d758f17d47eba8951d7810190e907db56e51801

Golang bindings for the Telegram Bot API

README.md (view raw)

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