all repos — telegram-bot-api @ 0b68c9b7906ce2b3af7c9a42053f32c286b17801

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		if update.Message == nil {
 55			continue
 56		}
 57
 58		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
 59
 60		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
 61		msg.ReplyToMessageID = update.Message.MessageID
 62
 63		bot.Send(msg)
 64	}
 65}
 66```
 67
 68If you need to use webhooks (if you wish to run on Google App Engine),
 69you may use a slightly different method.
 70
 71```go
 72package main
 73
 74import (
 75	"gopkg.in/telegram-bot-api.v4"
 76	"log"
 77	"net/http"
 78)
 79
 80func main() {
 81	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
 82	if err != nil {
 83		log.Fatal(err)
 84	}
 85
 86	bot.Debug = true
 87
 88	log.Printf("Authorized on account %s", bot.Self.UserName)
 89
 90	_, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
 91	if err != nil {
 92		log.Fatal(err)
 93	}
 94	info, err := bot.GetWebhookInfo()
 95	if err != nil {
 96		log.Fatal(err)
 97	}
 98	if info.LastErrorDate != 0 {
 99		log.Printf("[Telegram callback failed]%s", info.LastErrorMessage)
100	}
101	updates := bot.ListenForWebhook("/" + bot.Token)
102	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
103
104	for update := range updates {
105		log.Printf("%+v\n", update)
106	}
107}
108```
109
110If you need, you may generate a self signed certficate, as this requires
111HTTPS / TLS. The above example tells Telegram that this is your
112certificate and that it should be trusted, even though it is not
113properly signed.
114
115    openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes
116
117Now that [Let's Encrypt](https://letsencrypt.org) has entered public beta,
118you may wish to generate your free TLS certificate there.