README.md (view raw)
1# Golang bindings for the Telegram Bot API
2
3[![GoDoc](https://godoc.org/github.com/Syfaro/telegram-bot-api?status.svg)](http://godoc.org/github.com/Syfaro/telegram-bot-api)
4[![Travis](https://travis-ci.org/Syfaro/telegram-bot-api.svg)](https://travis-ci.org/Syfaro/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
19## Example
20
21This is a very simple bot that just displays any gotten updates,
22then replies it to that chat.
23
24```go
25package main
26
27import (
28 "log"
29 "github.com/Syfaro/telegram-bot-api"
30)
31
32func main() {
33 bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
34 if err != nil {
35 log.Panic(err)
36 }
37
38 bot.Debug = true
39
40 log.Printf("Authorized on account %s", bot.Self.UserName)
41
42 u := tgbotapi.NewUpdate(0)
43 u.Timeout = 60
44
45 updates, err := bot.GetUpdatesChan(u)
46
47 for update := range updates {
48 log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
49
50 msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
51 msg.ReplyToMessageID = update.Message.MessageID
52
53 bot.Send(msg)
54 }
55}
56```
57
58If you need to use webhooks (if you wish to run on Google App Engine),
59you may use a slightly different method.
60
61```go
62package main
63
64import (
65 "github.com/Syfaro/telegram-bot-api"
66 "log"
67 "net/http"
68)
69
70func main() {
71 bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
72 if err != nil {
73 log.Fatal(err)
74 }
75
76 bot.Debug = true
77
78 log.Printf("Authorized on account %s", bot.Self.UserName)
79
80 _, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
81 if err != nil {
82 log.Fatal(err)
83 }
84
85 updates, _ := bot.ListenForWebhook("/" + bot.Token)
86 go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
87
88 for update := range updates {
89 log.Printf("%+v\n", update)
90 }
91}
92```
93
94If you need, you may generate a self signed certficate, as this requires
95HTTPS / TLS. The above example tells Telegram that this is your
96certificate and that it should be trusted, even though it is not
97properly signed.
98
99 openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes
100
101Now that [Let's Encrypt](https://letsencrypt.org) has entered public beta,
102you may wish to generate your free TLS certificate there.