go-telegram / bot

Telegram Bot API Go framework
MIT License
502 stars 46 forks source link

(feat): Add method to serve static HTML/JS files for integrate Telegram Mini Apps #96

Open koddr opened 1 week ago

koddr commented 1 week ago

First of all, thanks for supporting Telegram API at Go! 🔥 This is really helpful package to create TG bots.

IMHO, it would be great to add some methods to serve static HTML/JS files in one Go binary. For able to integrate Telegram Mini Apps, or simple embedded webviews.

For example, like this:

// ...

func main() {
    ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
    defer cancel()

    opts := []bot.Option{
        bot.WithDefaultHandler(handler),
    }

    b, err := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER", opts...)
    if err != nil {
        panic(err)
    }

    // New method FileServer() with defining ./static folder.
    fs := b.FileServer(http.Dir("./static"))

    // New method ServeStatic() with defining URL path.
    b.ServeStatic("/static/", http.StripPrefix("/static/", fs))

    b.Start(ctx)
}

// ...

Now, we can create buttons with links to our web pages.

Also, we can easily embed this static files to binary, like this:

// ...

//go:embed static
var staticFolder embed.FS

func main() {
    // ...

    sub, err := fs.Sub(staticFolder, "static")
    if err != nil {
        panic(err)
    }

    // New method FileServer() with defining ./static folder.
    fs := b.FileServer(http.FS(sub))

    // New method ServeStatic() with defining URL path.
    b.ServeStatic("/", http.StripPrefix("/", fs))

    b.Start(ctx)
}

// ...