all repos — telegram-bot-api @ 8b33fd5a507598ec18d38a51be13f7fe0300d6cd

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/Syfaro/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	if err != nil {
39		log.Panic(err)
40	}
41
42	for update := range bot.Updates {
43		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
44
45		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
46		msg.ReplyToMessageID = update.Message.MessageID
47
48		bot.SendMessage(msg)
49	}
50}
51```
52
53If you need to use webhooks for some reason (such as running on Google App Engine), you may use a slightly different method.
54
55```go
56package main
57
58import (
59	"github.com/Syfaro/telegram-bot-api"
60	"log"
61	"net/http"
62)
63
64func main() {
65	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
66	if err != nil {
67		log.Fatal(err)
68	}
69
70	bot.Debug = true
71
72	log.Printf("Authorized on account %s", bot.Self.UserName)
73
74	_, err := bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
75	if err != nil {
76		log.Fatal(err)
77	}
78
79	bot.ListenForWebhook()
80	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
81
82	for update := range bot.Updates {
83		log.Printf("%+v\n", update)
84	}
85}
86```
87
88If 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.
89
90    openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj -nodes