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 { // ignore any non-Message Updates
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
68There are more examples on the [wiki](https://github.com/go-telegram-bot-api/telegram-bot-api/wiki)
69with detailed information on how to do many differen kinds of things.
70It's a great place to get started on using keyboards, commands, or other
71kinds of reply markup.
72
73If you need to use webhooks (if you wish to run on Google App Engine),
74you may use a slightly different method.
75
76```go
77package main
78
79import (
80 "gopkg.in/telegram-bot-api.v4"
81 "log"
82 "net/http"
83)
84
85func main() {
86 bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
87 if err != nil {
88 log.Fatal(err)
89 }
90
91 bot.Debug = true
92
93 log.Printf("Authorized on account %s", bot.Self.UserName)
94
95 _, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
96 if err != nil {
97 log.Fatal(err)
98 }
99 info, err := bot.GetWebhookInfo()
100 if err != nil {
101 log.Fatal(err)
102 }
103 if info.LastErrorDate != 0 {
104 log.Printf("Telegram callback failed: %s", info.LastErrorMessage)
105 }
106 updates := bot.ListenForWebhook("/" + bot.Token)
107 go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
108
109 for update := range updates {
110 log.Printf("%+v\n", update)
111 }
112}
113```
114
115If you need, you may generate a self signed certficate, as this requires
116HTTPS / TLS. The above example tells Telegram that this is your
117certificate and that it should be trusted, even though it is not
118properly signed.
119
120 openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes
121
122Now that [Let's Encrypt](https://letsencrypt.org) is available,
123you may wish to generate your free TLS certificate there.