3
3
[ ![ Go Reference] ( https://pkg.go.dev/badge/github.com/go-telegram-bot-api/telegram-bot-api/v5.svg )] ( https://pkg.go.dev/github.com/go-telegram-bot-api/telegram-bot-api/v5 )
4
4
[ ![ Test] ( https://github.com/go-telegram-bot-api/telegram-bot-api/actions/workflows/test.yml/badge.svg )] ( https://github.com/go-telegram-bot-api/telegram-bot-api/actions/workflows/test.yml )
5
5
6
- All methods are fairly self-explanatory, and reading the [ godoc] ( http ://godoc.org /github.com/go-telegram-bot-api/telegram-bot-api) page should
6
+ All methods are fairly self-explanatory, and reading the [ godoc] ( https ://pkg.go.dev /github.com/go-telegram-bot-api/telegram-bot-api/v5 ) page should
7
7
explain everything. If something isn't clear, open an issue or submit
8
8
a pull request.
9
9
10
+ There are more tutorials and high-level information on the website, [ go-telegram-bot-api.dev] ( https://go-telegram-bot-api.dev ) .
11
+
10
12
The scope of this project is just to provide a wrapper around the API
11
13
without any additional features. There are other projects for creating
12
14
something with plugins and command handlers without having to design
@@ -45,28 +47,21 @@ func main() {
45
47
u := tgbotapi.NewUpdate (0 )
46
48
u.Timeout = 60
47
49
48
- updates , err := bot.GetUpdatesChan (u)
50
+ updates := bot.GetUpdatesChan (u)
49
51
50
52
for update := range updates {
51
- if update.Message == nil { // ignore any non-Message Updates
52
- continue
53
- }
53
+ if update.Message != nil { // If we got a message
54
+ log.Printf (" [%s ] %s " , update.Message .From .UserName , update.Message .Text )
54
55
55
- log.Printf (" [%s ] %s " , update.Message .From .UserName , update.Message .Text )
56
+ msg := tgbotapi.NewMessage (update.Message .Chat .ID , update.Message .Text )
57
+ msg.ReplyToMessageID = update.Message .MessageID
56
58
57
- msg := tgbotapi.NewMessage (update.Message .Chat .ID , update.Message .Text )
58
- msg.ReplyToMessageID = update.Message .MessageID
59
-
60
- bot.Send (msg)
59
+ bot.Send (msg)
60
+ }
61
61
}
62
62
}
63
63
```
64
64
65
- There are more examples on the [ site] ( https://go-telegram-bot-api.dev/ )
66
- with detailed information on how to do many kinds of things.
67
- It's a great place to get started on using keyboards, commands, or other
68
- kinds of reply markup.
69
-
70
65
If you need to use webhooks (if you wish to run on Google App Engine),
71
66
you may use a slightly different method.
72
67
@@ -77,7 +72,7 @@ import (
77
72
" log"
78
73
" net/http"
79
74
80
- " github.com/go-telegram-bot-api/telegram-bot-api"
75
+ " github.com/go-telegram-bot-api/telegram-bot-api/v5 "
81
76
)
82
77
83
78
func main () {
@@ -90,17 +85,22 @@ func main() {
90
85
91
86
log.Printf (" Authorized on account %s " , bot.Self .UserName )
92
87
93
- _, err = bot.SetWebhook (tgbotapi.NewWebhookWithCert (" https://www.google.com:8443/" +bot.Token , " cert.pem" ))
88
+ wh , _ := tgbotapi.NewWebhookWithCert (" https://www.google.com:8443/" +bot.Token , " cert.pem" )
89
+
90
+ _, err = bot.SetWebhook (wh)
94
91
if err != nil {
95
92
log.Fatal (err)
96
93
}
94
+
97
95
info , err := bot.GetWebhookInfo ()
98
96
if err != nil {
99
97
log.Fatal (err)
100
98
}
99
+
101
100
if info.LastErrorDate != 0 {
102
101
log.Printf (" Telegram callback failed: %s " , info.LastErrorMessage )
103
102
}
103
+
104
104
updates := bot.ListenForWebhook (" /" + bot.Token )
105
105
go http.ListenAndServeTLS (" 0.0.0.0:8443" , " cert.pem" , " key.pem" , nil )
106
106
@@ -110,74 +110,6 @@ func main() {
110
110
}
111
111
```
112
112
113
- If you need to publish your bot on AWS Lambda(or something like it) and AWS API Gateway,
114
- you can use such example:
115
-
116
- In this code used AWS Lambda Go net/http server adapter [ algnhsa] ( https://github.com/akrylysov/algnhsa )
117
-
118
- ``` go
119
- package main
120
-
121
- import (
122
- " github.com/akrylysov/algnhsa"
123
- " github.com/go-telegram-bot-api/telegram-bot-api"
124
- " log"
125
- " net/http"
126
- )
127
-
128
- func answer (w http .ResponseWriter , r *http .Request ) {
129
- bot , err := tgbotapi.NewBotAPI (" MyAwesomeBotToken" )
130
- if err != nil {
131
- log.Fatal (err)
132
- }
133
-
134
- bot.Debug = true
135
- updates := bot.ListenForWebhookRespReqFormat (w, r)
136
- for update := range updates {
137
- if update.Message == nil {
138
- continue
139
- }
140
- log.Printf (" [%s ] %s " , update.Message .From .UserName , update.Message .Text )
141
-
142
- msg := tgbotapi.NewMessage (update.Message .Chat .ID , update.Message .Text )
143
- msg.ReplyToMessageID = update.Message .MessageID
144
- _ , err := bot.Send (msg)
145
- if err != nil {
146
- log.Printf (" Error send message: %s | Error: %s " , msg.Text , err.Error ())
147
- }
148
- }
149
- }
150
-
151
- func setWebhook (_ http .ResponseWriter , _ *http .Request ) {
152
- bot , err := tgbotapi.NewBotAPI (" MyAwesomeBotToken" )
153
- if err != nil {
154
- log.Fatal (err)
155
- }
156
-
157
- bot.Debug = true
158
-
159
- log.Printf (" Authorized on account %s " , bot.Self .UserName )
160
-
161
- _, err = bot.SetWebhook (tgbotapi.NewWebhook (" https://your_api_gateway_address.com/" +bot.Token ))
162
- if err != nil {
163
- log.Fatal (err)
164
- }
165
- info , err := bot.GetWebhookInfo ()
166
- if err != nil {
167
- log.Fatal (err)
168
- }
169
- if info.LastErrorDate != 0 {
170
- log.Printf (" Telegram callback failed: %s " , info.LastErrorMessage )
171
- }
172
- }
173
-
174
- func main () {
175
- http.HandleFunc (" /set_webhook" , setWebhook)
176
- http.HandleFunc (" /MyAwesomeBotToken" , answer)
177
- algnhsa.ListenAndServe (http.DefaultServeMux , nil )
178
- }
179
- ```
180
-
181
113
If you need, you may generate a self-signed certificate, as this requires
182
114
HTTPS / TLS. The above example tells Telegram that this is your
183
115
certificate and that it should be trusted, even though it is not
0 commit comments