Skip to content

Commit 18031c8

Browse files
committed
Fix bug with update channel closing, add ListenForWebhookRespReqFormat method, for using in serverless apps, add example to README.md, add exaple pf a published bot on AWS Lambda
1 parent 54104a0 commit 18031c8

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,74 @@ func main() {
110110
}
111111
```
112112

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+
113181
If you need, you may generate a self signed certficate, as this requires
114182
HTTPS / TLS. The above example tells Telegram that this is your
115183
certificate and that it should be trusted, even though it is not

bot.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,33 @@ func (bot *BotAPI) ListenForWebhook(pattern string) UpdatesChannel {
567567
}
568568

569569
ch <- *update
570+
close(ch)
570571
})
571572

572573
return ch
573574
}
574575

576+
// ListenForWebhookRespReqFormat registers a http handler for a webhook.
577+
func (bot *BotAPI) ListenForWebhookRespReqFormat(w http.ResponseWriter, r *http.Request) UpdatesChannel {
578+
ch := make(chan Update, bot.Buffer)
579+
580+
func(w http.ResponseWriter, r *http.Request) {
581+
update, err := bot.HandleUpdate(r)
582+
if err != nil {
583+
errMsg, _ := json.Marshal(map[string]string{"error": err.Error()})
584+
w.WriteHeader(http.StatusBadRequest)
585+
w.Header().Set("Content-Type", "application/json")
586+
_, _ = w.Write(errMsg)
587+
return
588+
}
589+
590+
ch <- *update
591+
close(ch)
592+
}(w, r)
593+
594+
return ch
595+
}
596+
575597
// HandleUpdate parses and returns update received via webhook
576598
func (bot *BotAPI) HandleUpdate(r *http.Request) (*Update, error) {
577599
if r.Method != http.MethodPost {

0 commit comments

Comments
 (0)