all repos — telegram-bot-api @ af029c366ce76c9e394ff18c43a5174314e0ce2e

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/zhulik/telegram-bot-api?status.svg)](http://godoc.org/github.com/zhulik/telegram-bot-api)
 4[![Travis](https://travis-ci.org/zhulik/telegram-bot-api.svg)](https://travis-ci.org/zhulik/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, open an issue and I'll see what I can do.
 8
 9All methods are fairly self explanatory, and reading the godoc page should explain everything. If something isn't clear, open an issue or submit a pull request.
10
11The scope of this project is just to provide a wrapper around the API without any additional features. There are other projects for creating something with plugins and command handlers without having to design all that yourself.
12
13## Example
14
15This is a very simple bot that just displays any gotten updates, then replies it to that chat.
16
17```go
18package main
19
20import (
21	"log"
22	"github.com/zhulik/telegram-bot-api"
23)
24
25func main() {
26	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
27    if err != nil {
28        log.Panic(err)
29    }
30
31    bot.Debug = true
32
33    log.Printf("Authorized on account %s", bot.Self.UserName)
34
35    u := tgbotapi.NewUpdate(0)
36    u.Timeout = 60
37
38    err = bot.UpdatesChan(u)
39
40    for update := range bot.Updates {
41        log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
42
43        msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
44        msg.ReplyToMessageID = update.Message.MessageID
45
46        bot.Send(msg)
47    }
48}
49```
50
51If you need to use webhooks for some reason (such as running on Google App Engine), you may use a slightly different method.
52
53```go
54package main
55
56import (
57	"github.com/zhulik/telegram-bot-api"
58	"log"
59	"net/http"
60)
61
62func main() {
63	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
64	if err != nil {
65		log.Fatal(err)
66	}
67
68	bot.Debug = true
69
70	log.Printf("Authorized on account %s", bot.Self.UserName)
71
72	_, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
73	if err != nil {
74		log.Fatal(err)
75	}
76
77	bot.ListenForWebhook("/"+bot.Token)
78	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
79
80	for update := range bot.Updates {
81		log.Printf("%+v\n", update)
82	}
83}
84```
85
86If you need, you may generate a self signed certficate, as this requires HTTPS / TLS. The above example tells Telegram that this is your certificate and that it should be trusted, even though it is not properly signed.
87
88    openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes