Skip to content

Commit

Permalink
Fixed possible panic in webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
mymmrac committed Jun 11, 2023
1 parent e7a71d3 commit 3cb851d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
42 changes: 33 additions & 9 deletions examples/ngrok/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
Expand All @@ -23,10 +25,15 @@ func main() {
os.Exit(1)
}

ctx := context.Background()
// Initialize signal handling
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

// Initialize done chan
done := make(chan struct{}, 1)

// Create a new Ngrok tunnel to connect local network with the Internet & have HTTPS domain for bot
tun, err := ngrok.Listen(ctx,
tun, err := ngrok.Listen(context.Background(),
// Forward connections to localhost:8080
config.HTTPEndpoint(config.WithForwardsTo(":8080")),
// Authenticate into Ngrok using NGROK_AUTHTOKEN env (optional)
Expand All @@ -50,6 +57,7 @@ func main() {
Router: router.New(),
},
// Override default start func to use Ngrok tunnel
// Note: When server is stopped, the Ngrok tunnel always returns an error, so it should be handled by user
StartFunc: func(_ string) error {
return srv.Serve(tun)
},
Expand All @@ -61,18 +69,34 @@ func main() {
}),
)

// Handle stop signal (Ctrl+C)
go func() {
// Wait for stop signal
<-sigs

fmt.Println("Stopping...")

// Stop reviving updates from update channel and shutdown webhook server
_ = bot.StopWebhook()
fmt.Println("Webhook done")

This comment has been minimized.

Copy link
@abakum

abakum Jun 11, 2023

Contributor

Please add this

		// Unset webhook on telegram server but keep updates for next start
		bot.DeleteWebhook(&telego.DeleteWebhookParams{DropPendingUpdates: false})
		fmt.Println("DeleteWebhook done")

Without this, the next time you run the examples, they will fail with an error

This comment has been minimized.

Copy link
@mymmrac

mymmrac Jun 11, 2023

Author Owner

Hmm, for me, it doesn't give an error, tried more than 10 time in a row

This comment has been minimized.

Copy link
@abakum

abakum Jun 18, 2023

Contributor

PS Y:\src\telego\examples\graceful_shutdown_long_polling>
Handling updates...
[Sun Jun 18 09:45:41 MSK 2023] DEBUG API call to: "https://api.telegram.org/botBOT_TOKEN/getUpdates", with data: {"timeout":8}
[Sun Jun 18 09:45:41 MSK 2023] DEBUG API response getUpdates: Ok: false, Err: [409 "Conflict: can't use getUpdates method while webhook is active; use deleteWebhook to delete the webhook first"]
[Sun Jun 18 09:45:41 MSK 2023] ERROR Getting updates: telego: getUpdates(): api: 409 "Conflict: can't use getUpdates method while webhook is active; use deleteWebhook to delete the webhook first"

// Notify that stop is done
done <- struct{}{}
}()

// Start server for receiving requests from the Telegram
go func() {
_ = bot.StartWebhook("")
}()

// Stop reviving updates from update channel and shutdown webhook server
defer func() {
_ = bot.StopWebhook()
// Loop through all updates when they came
go func() {
for update := range updates {
fmt.Printf("Update: %+v\n", update)
}
}()

// Loop through all updates when they came
for update := range updates {
fmt.Printf("Update: %+v\n", update)
}
// Wait for the stop process to be completed
<-done
fmt.Println("Done")
}
6 changes: 4 additions & 2 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@ func (b *Bot) StartWebhook(address string) error {

if err := ctx.server.Start(address); err != nil {
ctx.runningLock.Lock()
ctx.running = false
close(ctx.stop)
if ctx.running {
close(ctx.stop)
ctx.running = false
}
b.webhookContext = nil
ctx.runningLock.Unlock()

Expand Down

0 comments on commit 3cb851d

Please sign in to comment.