From 18031c85e5bbe0781261fcbb2f20e11ca07583e6 Mon Sep 17 00:00:00 2001 From: farit2000 Date: Fri, 27 Nov 2020 11:24:41 +0300 Subject: [PATCH] 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 --- README.md | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ bot.go | 22 ++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/README.md b/README.md index 43b33ed..c6692ac 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,74 @@ func main() { } ``` +If you need to publish your bot on AWS Lambda(or something like it) and AWS API Gateway, +you can use such example: + +In this code used AWS Lambda Go net/http server adapter [algnhsa](https://github.com/akrylysov/algnhsa) + +```go +package main + +import ( + "github.com/akrylysov/algnhsa" + "github.com/go-telegram-bot-api/telegram-bot-api" + "log" + "net/http" +) + +func answer(w http.ResponseWriter, r *http.Request) { + bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") + if err != nil { + log.Fatal(err) + } + + bot.Debug = true + updates := bot.ListenForWebhookRespReqFormat(w, r) + for update := range updates { + if update.Message == nil { + continue + } + log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) + + msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text) + msg.ReplyToMessageID = update.Message.MessageID + _, err := bot.Send(msg) + if err != nil { + log.Printf("Error send message: %s | Error: %s", msg.Text, err.Error()) + } + } +} + +func setWebhook(_ http.ResponseWriter, _ *http.Request) { + bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") + if err != nil { + log.Fatal(err) + } + + bot.Debug = true + + log.Printf("Authorized on account %s", bot.Self.UserName) + + _, err = bot.SetWebhook(tgbotapi.NewWebhook("https://your_api_gateway_address.com/"+bot.Token)) + if err != nil { + log.Fatal(err) + } + info, err := bot.GetWebhookInfo() + if err != nil { + log.Fatal(err) + } + if info.LastErrorDate != 0 { + log.Printf("Telegram callback failed: %s", info.LastErrorMessage) + } +} + +func main() { + http.HandleFunc("/set_webhook", setWebhook) + http.HandleFunc("/MyAwesomeBotToken", answer) + algnhsa.ListenAndServe(http.DefaultServeMux, nil) +} +``` + If 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 diff --git a/bot.go b/bot.go index 626024e..7ec2efb 100644 --- a/bot.go +++ b/bot.go @@ -567,11 +567,33 @@ func (bot *BotAPI) ListenForWebhook(pattern string) UpdatesChannel { } ch <- *update + close(ch) }) return ch } +// ListenForWebhookRespReqFormat registers a http handler for a webhook. +func (bot *BotAPI) ListenForWebhookRespReqFormat(w http.ResponseWriter, r *http.Request) UpdatesChannel { + ch := make(chan Update, bot.Buffer) + + func(w http.ResponseWriter, r *http.Request) { + update, err := bot.HandleUpdate(r) + if err != nil { + errMsg, _ := json.Marshal(map[string]string{"error": err.Error()}) + w.WriteHeader(http.StatusBadRequest) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write(errMsg) + return + } + + ch <- *update + close(ch) + }(w, r) + + return ch +} + // HandleUpdate parses and returns update received via webhook func (bot *BotAPI) HandleUpdate(r *http.Request) (*Update, error) { if r.Method != http.MethodPost {