mirror of
https://github.com/lukaszraczylo/go-telegram.git
synced 2026-06-13 02:51:55 +00:00
Initial release of go-telegram
A fully-generated, strongly-typed Go client for the Telegram Bot API. * 176 methods + 301 types generated from Bot API v10.0 * 1408 auto-generated tests (8 scenarios per method) * Typed unions throughout — no 'any' in the public surface * Pluggable HTTP transport and JSON codec (default goccy/go-json) * Built-in retry middleware honouring Telegram's retry_after * Generic dispatcher with filters and conversation handlers * Self-verifying codegen pipeline (regen → audit → emit → run tests) * 14 example bots covering common patterns
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/lukaszraczylo/go-telegram/client"
|
||||
)
|
||||
|
||||
// MeCache caches the result of GetMe across calls. Construct one per
|
||||
// Bot and call Get to retrieve the cached User on subsequent invocations.
|
||||
//
|
||||
// var meCache api.MeCache
|
||||
// me, err := meCache.Get(ctx, bot)
|
||||
//
|
||||
// MeCache is safe for concurrent use.
|
||||
type MeCache struct {
|
||||
mu sync.Mutex
|
||||
cached *User
|
||||
}
|
||||
|
||||
// Get returns the User from a cached GetMe call. If the cache is empty,
|
||||
// it calls GetMe and populates the cache on success.
|
||||
func (c *MeCache) Get(ctx context.Context, b *client.Bot) (*User, error) {
|
||||
c.mu.Lock()
|
||||
if c.cached != nil {
|
||||
u := c.cached
|
||||
c.mu.Unlock()
|
||||
return u, nil
|
||||
}
|
||||
c.mu.Unlock()
|
||||
|
||||
u, err := GetMe(ctx, b, &GetMeParams{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.mu.Lock()
|
||||
c.cached = u
|
||||
c.mu.Unlock()
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// Reset clears the cache. Useful in tests or after the bot's identity
|
||||
// is known to have changed (very rare).
|
||||
func (c *MeCache) Reset() {
|
||||
c.mu.Lock()
|
||||
c.cached = nil
|
||||
c.mu.Unlock()
|
||||
}
|
||||
Reference in New Issue
Block a user