all repos — telegram-bot-api @ d40f7f97034c8473b3fabe6e3034512fa1d655c9

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[![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, 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
13Note to previous users, there was just a large change that broke some methods. The main changes are that all the `Send*` functions have been replaced with a single `Send`, and `UpdatesChan` was renamed `GetUpdatesChan` and returns `(chan, err)` instead of storing the chan in `Updates`.
14
15## Example
16
17This is a very simple bot that just displays any gotten updates, then replies it to that chat.
18
19```go
20package main
21
22import (
23	"log"
24	"github.com/Syfaro/telegram-bot-api"
25)
26
27func main() {
28	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
29	if err != nil {
30		log.Panic(err)
31	}
32
33	bot.Debug = true
34
35	log.Printf("Authorized on account %s", bot.Self.UserName)
36
37	u := tgbotapi.NewUpdate(0)
38	u.Timeout = 60
39
40	updates, err := bot.GetUpdatesChan(u)
41
42	for update := range 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.Send(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	updates, _ := bot.ListenForWebhook("/" + bot.Token)
80	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
81
82	for update := range 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 "//O=Org\CN=Test" -nodes
91
92Now that [Let's Encrypt](https://letsencrypt.org) has entered public beta, you may wish to generate your free TLS certificate there.