all repos — telegram-bot-api @ ab63d49cc925dfd42088efb03c558b99263c248c

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.v4` for the stable build.
 21
 22Join [the development group](https://telegram.me/go_telegram_bot_api) if
 23you want to ask questions or discuss development.
 24
 25## Example
 26
 27This is a very simple bot that just displays any gotten updates,
 28then replies it to that chat.
 29
 30```go
 31package main
 32
 33import (
 34	"log"
 35	"gopkg.in/telegram-bot-api.v4"
 36)
 37
 38func main() {
 39	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
 40	if err != nil {
 41		log.Panic(err)
 42	}
 43
 44	bot.Debug = true
 45
 46	log.Printf("Authorized on account %s", bot.Self.UserName)
 47
 48	u := tgbotapi.NewUpdate(0)
 49	u.Timeout = 60
 50
 51	updates, err := bot.GetUpdatesChan(u)
 52
 53	for update := range updates {
 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
 64If you need to use webhooks (if you wish to run on Google App Engine),
 65you may use a slightly different method.
 66
 67```go
 68package main
 69
 70import (
 71	"gopkg.in/telegram-bot-api.v4"
 72	"log"
 73	"net/http"
 74)
 75
 76func main() {
 77	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
 78	if err != nil {
 79		log.Fatal(err)
 80	}
 81
 82	bot.Debug = true
 83
 84	log.Printf("Authorized on account %s", bot.Self.UserName)
 85
 86	_, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
 87	if err != nil {
 88		log.Fatal(err)
 89	}
 90
 91	updates := bot.ListenForWebhook("/" + bot.Token)
 92	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
 93
 94	for update := range updates {
 95		log.Printf("%+v\n", update)
 96	}
 97}
 98```
 99
100If you need, you may generate a self signed certficate, as this requires
101HTTPS / TLS. The above example tells Telegram that this is your
102certificate and that it should be trusted, even though it is not
103properly signed.
104
105    openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes
106
107Now that [Let's Encrypt](https://letsencrypt.org) has entered public beta,
108you may wish to generate your free TLS certificate there.