all repos — telegram-bot-api @ f2b36838e6134c4db9b3f4d78d9cd2fe40e25724

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 have been added, and all features should be available.
  7If you want a feature that hasn't been added yet or something is broken,
  8open an issue and I'll see what I can do.
  9
 10All methods are fairly self explanatory, and reading the godoc page should
 11explain everything. If something isn't clear, open an issue or submit
 12a pull request.
 13
 14The scope of this project is just to provide a wrapper around the API
 15without any additional features. There are other projects for creating
 16something with plugins and command handlers without having to design
 17all that yourself.
 18
 19Use `github.com/go-telegram-bot-api/telegram-bot-api` for the latest
 20version, or use `gopkg.in/telegram-bot-api.v1` for the stable build.
 21
 22## Example
 23
 24This is a very simple bot that just displays any gotten updates,
 25then replies it to that chat.
 26
 27```go
 28package main
 29
 30import (
 31	"log"
 32	"gopkg.in/telegram-bot-api.v1"
 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		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
 52
 53		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
 54		msg.ReplyToMessageID = update.Message.MessageID
 55
 56		bot.Send(msg)
 57	}
 58}
 59```
 60
 61If you need to use webhooks (if you wish to run on Google App Engine),
 62you may use a slightly different method.
 63
 64```go
 65package main
 66
 67import (
 68	"gopkg.in/telegram-bot-api.v1"
 69	"log"
 70	"net/http"
 71)
 72
 73func main() {
 74	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
 75	if err != nil {
 76		log.Fatal(err)
 77	}
 78
 79	bot.Debug = true
 80
 81	log.Printf("Authorized on account %s", bot.Self.UserName)
 82
 83	_, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
 84	if err != nil {
 85		log.Fatal(err)
 86	}
 87
 88	updates, _ := bot.ListenForWebhook("/" + bot.Token)
 89	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
 90
 91	for update := range updates {
 92		log.Printf("%+v\n", update)
 93	}
 94}
 95```
 96
 97If you need, you may generate a self signed certficate, as this requires
 98HTTPS / TLS. The above example tells Telegram that this is your
 99certificate and that it should be trusted, even though it is not
100properly signed.
101
102    openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes
103
104Now that [Let's Encrypt](https://letsencrypt.org) has entered public beta,
105you may wish to generate your free TLS certificate there.