all repos — telegram-bot-api @ b728fa78fc94abdd39b0f24bf71dc1d245385059

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
 19Join [the development group](https://telegram.me/go_telegram_bot_api) if
 20you want to ask questions or discuss development.
 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
 33	"github.com/go-telegram-bot-api/telegram-bot-api"
 34)
 35
 36func main() {
 37	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
 38	if err != nil {
 39		log.Panic(err)
 40	}
 41
 42	bot.Debug = true
 43
 44	log.Printf("Authorized on account %s", bot.Self.UserName)
 45
 46	u := tgbotapi.NewUpdate(0)
 47	u.Timeout = 60
 48
 49	updates, err := bot.GetUpdatesChan(u)
 50
 51	for update := range updates {
 52		if update.Message == nil {
 53			continue
 54		}
 55
 56		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
 57
 58		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
 59		msg.ReplyToMessageID = update.Message.MessageID
 60
 61		bot.Send(msg)
 62	}
 63}
 64```
 65
 66If you need to use webhooks (if you wish to run on Google App Engine),
 67you may use a slightly different method.
 68
 69```go
 70package main
 71
 72import (
 73	"log"
 74	"net/http"
 75
 76	"github.com/go-telegram-bot-api/telegram-bot-api"
 77)
 78
 79func main() {
 80	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
 81	if err != nil {
 82		log.Fatal(err)
 83	}
 84
 85	bot.Debug = true
 86
 87	log.Printf("Authorized on account %s", bot.Self.UserName)
 88
 89	_, err = bot.Request(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
 90	if err != nil {
 91		log.Fatal(err)
 92	}
 93
 94	info, err := bot.GetWebhookInfo()
 95
 96	if err != nil {
 97		log.Fatal(err)
 98	}
 99
100	if info.LastErrorDate != 0 {
101		log.Printf("failed to set webhook: %s", info.LastErrorMessage)
102	}
103
104	updates := bot.ListenForWebhook("/" + bot.Token)
105	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
106
107	for update := range updates {
108		log.Printf("%+v\n", update)
109	}
110}
111```
112
113If you need, you may generate a self signed certficate, as this requires
114HTTPS / TLS. The above example tells Telegram that this is your
115certificate and that it should be trusted, even though it is not
116properly signed.
117
118    openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes
119
120Now that [Let's Encrypt](https://letsencrypt.org) is available, you may
121wish to generate your free TLS certificate there.