|
1 | | -# Golang bindings for the Telegram Bot API |
| 1 | +# Go bindings for the Telegram Bot API |
2 | 2 |
|
3 | 3 | [](https://github.com/bots-go-framework/bots-api-telegram/actions/workflows/ci.yml) |
4 | 4 | [](https://goreportcard.com/report/github.com/bots-go-framework/bots-api-telegram) |
5 | | -[](http://godoc.org/github.com/bots-go-framework/bots-api-telegram) |
| 5 | +[](https://pkg.go.dev/github.com/bots-go-framework/bots-api-telegram) |
6 | 6 |
|
7 | | -All methods have been added, and all features should be available. |
8 | | -If you want a feature that hasn't been added yet or something is broken, |
9 | | -open an issue and I'll see what I can do. |
| 7 | +Go bindings for the [Telegram Bot API](https://core.telegram.org/bots/api), tracking **Bot API 9.5**. |
10 | 8 |
|
11 | | -All methods are fairly self explanatory, and reading the godoc page should |
12 | | -explain everything. If something isn't clear, open an issue or submit |
13 | | -a pull request. |
| 9 | +This module also includes sub-packages for [Telegram Login Widget](#tglogin) and [Telegram Web Apps](#tgwebapp). |
14 | 10 |
|
15 | | -The scope of this project is just to provide a wrapper around the API |
16 | | -without any additional features. There are other projects for creating |
17 | | -something with plugins and command handlers without having to design |
18 | | -all that yourself. |
| 11 | +## Packages |
19 | 12 |
|
20 | | -Use `github.com/go-telegram-bot-api/telegram-bot-api` for the latest |
21 | | -version, or use `gopkg.in/telegram-bot-api.v1` for the stable build. |
| 13 | +| Package | Import path | Description | |
| 14 | +|---|---|---| |
| 15 | +| `tgbotapi` | `github.com/bots-go-framework/bots-api-telegram/tgbotapi` | Core Bot API types and HTTP client | |
| 16 | +| `tglogin` | `github.com/bots-go-framework/bots-api-telegram/tglogin` | Telegram Login Widget authentication | |
| 17 | +| `tgwebapp` | `github.com/bots-go-framework/bots-api-telegram/tgwebapp` | Telegram Web App (Mini App) init-data validation | |
22 | 18 |
|
23 | | -## Example |
| 19 | +## Installation |
24 | 20 |
|
25 | | -This is a very simple bot that just displays any gotten updates, |
26 | | -then replies it to that chat. |
| 21 | +```sh |
| 22 | +go get github.com/bots-go-framework/bots-api-telegram |
| 23 | +``` |
| 24 | + |
| 25 | +## Examples |
| 26 | + |
| 27 | +### Long-polling |
| 28 | + |
| 29 | +A minimal bot that echoes every message back to the sender: |
27 | 30 |
|
28 | 31 | ```go |
29 | 32 | package main |
30 | 33 |
|
31 | 34 | import ( |
32 | 35 | "log" |
33 | | - "gopkg.in/telegram-bot-api.v1" |
| 36 | + |
| 37 | + "github.com/bots-go-framework/bots-api-telegram/tgbotapi" |
34 | 38 | ) |
35 | 39 |
|
36 | 40 | func main() { |
37 | | - bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") |
38 | | - if err != nil { |
39 | | - logus.Panic(err) |
40 | | - } |
| 41 | + bot := tgbotapi.NewBotAPI("MyAwesomeBotToken") |
41 | 42 |
|
42 | | - bot.Debug = true |
43 | | - |
44 | | - logus.Printf("Authorized on account %s", bot.Self.UserName) |
| 43 | + log.Printf("Authorized on account %s", bot.Self.UserName) |
45 | 44 |
|
46 | 45 | u := tgbotapi.NewUpdate(0) |
47 | 46 | u.Timeout = 60 |
48 | 47 |
|
49 | 48 | updates, err := bot.GetUpdatesChan(u) |
| 49 | + if err != nil { |
| 50 | + log.Fatal(err) |
| 51 | + } |
50 | 52 |
|
51 | 53 | for update := range updates { |
52 | | - logus.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) |
| 54 | + if update.Message == nil { |
| 55 | + continue |
| 56 | + } |
| 57 | + log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) |
53 | 58 |
|
54 | 59 | msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text) |
55 | 60 | msg.ReplyToMessageID = update.Message.MessageID |
56 | 61 |
|
57 | | - bot.Send(msg) |
| 62 | + if _, err := bot.Send(msg); err != nil { |
| 63 | + log.Println(err) |
| 64 | + } |
58 | 65 | } |
59 | 66 | } |
60 | 67 | ``` |
61 | 68 |
|
62 | | -If you need to use webhooks (if you wish to run on Google App Engine), |
63 | | -you may use a slightly different method. |
| 69 | +### Webhook |
| 70 | + |
| 71 | +To receive updates via webhook your server must be reachable over HTTPS. |
| 72 | +Telegram supports ports **443, 80, 88, and 8443**. |
64 | 73 |
|
65 | 74 | ```go |
66 | 75 | package main |
67 | 76 |
|
68 | 77 | import ( |
69 | | - "gopkg.in/telegram-bot-api.v1" |
70 | 78 | "log" |
71 | 79 | "net/http" |
| 80 | + |
| 81 | + "github.com/bots-go-framework/bots-api-telegram/tgbotapi" |
72 | 82 | ) |
73 | 83 |
|
74 | 84 | func main() { |
75 | | - bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") |
76 | | - if err != nil { |
77 | | - logus.Fatal(err) |
78 | | - } |
79 | | - |
80 | | - bot.Debug = true |
| 85 | + bot := tgbotapi.NewBotAPI("MyAwesomeBotToken") |
81 | 86 |
|
82 | | - logus.Printf("Authorized on account %s", bot.Self.UserName) |
| 87 | + log.Printf("Authorized on account %s", bot.Self.UserName) |
83 | 88 |
|
84 | | - _, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem")) |
| 89 | + _, err := bot.SetWebhook(tgbotapi.NewWebhookWithCert( |
| 90 | + "https://example.com:8443/"+bot.Token, "cert.pem", |
| 91 | + )) |
85 | 92 | if err != nil { |
86 | | - logus.Fatal(err) |
| 93 | + log.Fatal(err) |
87 | 94 | } |
88 | 95 |
|
89 | | - updates, _ := bot.ListenForWebhook("/" + bot.Token) |
90 | | - go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil) |
| 96 | + updates := bot.ListenForWebhook("/" + bot.Token) |
| 97 | + go func() { |
| 98 | + if err := http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil); err != nil { |
| 99 | + log.Fatal(err) |
| 100 | + } |
| 101 | + }() |
91 | 102 |
|
92 | 103 | for update := range updates { |
93 | | - logus.Printf("%+v\n", update) |
| 104 | + log.Printf("%+v\n", update) |
94 | 105 | } |
95 | 106 | } |
96 | 107 | ``` |
97 | 108 |
|
98 | | -If you need, you may generate a self signed certficate, as this requires |
99 | | -HTTPS / TLS. The above example tells Telegram that this is your |
100 | | -certificate and that it should be trusted, even though it is not |
101 | | -properly signed. |
| 109 | +#### Self-signed TLS certificate |
| 110 | + |
| 111 | +If you don't have a certificate from a trusted CA (e.g. [Let's Encrypt](https://letsencrypt.org)), |
| 112 | +you can generate a self-signed one. Pass the public certificate to `NewWebhookWithCert` so |
| 113 | +Telegram knows to trust it. |
| 114 | + |
| 115 | +```sh |
| 116 | +openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3650 \ |
| 117 | + -subj "/O=Org/CN=example.com" -nodes |
| 118 | +``` |
| 119 | + |
| 120 | +## tglogin |
| 121 | + |
| 122 | +The `tglogin` sub-package validates data received from the |
| 123 | +[Telegram Login Widget](https://core.telegram.org/widgets/login). |
| 124 | + |
| 125 | +```go |
| 126 | +import "github.com/bots-go-framework/bots-api-telegram/tglogin" |
| 127 | + |
| 128 | +user := tglogin.LoginUser{ /* fields from the widget callback */ } |
| 129 | +if user.IsFromTelegram(botToken) { |
| 130 | + // user data is authentic |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +See [`tglogin/README.md`](tglogin/README.md) for full details. |
102 | 135 |
|
103 | | - openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3560 -subj "//O=Org\CN=Test" -nodes |
| 136 | +## tgwebapp |
104 | 137 |
|
105 | | -Now that [Let's Encrypt](https://letsencrypt.org) has entered public beta, |
106 | | -you may wish to generate your free TLS certificate there. |
| 138 | +The `tgwebapp` sub-package validates init data received from a |
| 139 | +[Telegram Web App (Mini App)](https://core.telegram.org/bots/webapps). |
| 140 | + |
| 141 | +```go |
| 142 | +import "github.com/bots-go-framework/bots-api-telegram/tgwebapp" |
| 143 | + |
| 144 | +tgwebapp.AuthenticateTelegramWebApp(w, r, |
| 145 | + func(bot string) string { return myBotToken }, |
| 146 | + func(initData *tgwebapp.InitData) { |
| 147 | + // initData is verified and ready to use |
| 148 | + }, |
| 149 | +) |
| 150 | +``` |
| 151 | + |
| 152 | +See [`tgwebapp/README.md`](tgwebapp/README.md) for full details. |
107 | 153 |
|
108 | 154 | ## Used by |
109 | | -This package is used in production by: |
110 | | -* https://debtstracker.io/ - an app and [Telegram bot](https://t.me/DebtsTrackerBot) to track your personal debts |
111 | 155 |
|
112 | | -## Frameworks that utilise this `strongo/db` package |
113 | | -* [`bots-go-framework`](https://github.com/bots-go-framework) - a framework to build chat bots in Go language. |
| 156 | +- [debtstracker.io](https://debtstracker.io/) — personal debt tracking app with a [Telegram bot](https://t.me/DebtsTrackerBot) |
| 157 | + |
| 158 | +## Related |
| 159 | + |
| 160 | +- [`bots-go-framework`](https://github.com/bots-go-framework) — framework for building Telegram bots in Go |
0 commit comments