all repos — telegram-bot-api @ 4ec899a62e8ea13f540cdc27b943642958836d4e

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	updates := bot.ListenForWebhook("/" + bot.Token)
 95	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
 96
 97	for update := range updates {
 98		log.Printf("%+v\n", update)
 99	}
100}
101```
102
103If you need, you may generate a self signed certficate, as this requires
104HTTPS / TLS. The above example tells Telegram that this is your
105certificate and that it should be trusted, even though it is not
106properly signed.
107
108    openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes
109
110Now that [Let's Encrypt](https://letsencrypt.org) is available, you may
111wish to generate your free TLS certificate there.