all repos — telegram-bot-api @ b8bc7b993903c879736560a3f8e05869503cc6ca

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/Syfaro/telegram-bot-api?status.svg)](http://godoc.org/github.com/Syfaro/telegram-bot-api)
 4
 5All methods have been added, and all features should be available.
 6If 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.
 7
 8All 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.
 9
10The 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.
11
12## Example
13
14This is a very simple bot that just displays any gotten updates, then replies it to that chat.
15
16```go
17package main
18
19import (
20	"log"
21	"github.com/zhulik/telegram-bot-api"
22)
23
24func main() {
25	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
26    if err != nil {
27        log.Panic(err)
28    }
29
30    bot.Debug = true
31
32    log.Printf("Authorized on account %s", bot.Self.UserName)
33
34    u := tgbotapi.NewUpdate(0)
35    u.Timeout = 60
36
37    err = bot.UpdatesChan(u)
38
39    for update := range bot.Updates {
40        log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
41
42        msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
43        msg.ReplyToMessageID = update.Message.MessageID
44
45        bot.Send(msg)
46    }
47}
48```
49
50If you need to use webhooks for some reason (such as running on Google App Engine), you may use a slightly different method.
51
52```go
53package main
54
55import (
56	"github.com/zhulik/telegram-bot-api"
57	"log"
58	"net/http"
59)
60
61func main() {
62	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
63	if err != nil {
64		log.Fatal(err)
65	}
66
67	bot.Debug = true
68
69	log.Printf("Authorized on account %s", bot.Self.UserName)
70
71	_, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
72	if err != nil {
73		log.Fatal(err)
74	}
75
76	bot.ListenForWebhook("/"+bot.Token)
77	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
78
79	for update := range bot.Updates {
80		log.Printf("%+v\n", update)
81	}
82}
83```
84
85If 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.
86
87    openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes