Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
AshokShau committed Oct 18, 2024
1 parent 8a0710e commit 8d45c15
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
43 changes: 26 additions & 17 deletions Telegram/modules/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ type Field struct {
// isVercel checks if the application is running on Vercel.
func isVercel() bool {
log.Println(config.VERCEL)
return config.VERCEL == "1"
//return config.VERCEL == "1"
return true
}

// fetchAPI fetches the API documentation from a remote source and updates the apiCache.
func fetchAPI() error {
log.Println("Starting API fetch")
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Get(apiURL)
if err != nil {
Expand All @@ -76,8 +76,11 @@ func fetchAPI() error {
return fmt.Errorf("failed to decode API response: %w", err)
}

log.Println("Fetched API documentation:", apiDocs) // Log the fetched data
// Optionally, log the fetched methods and types
log.Println("Fetched API methods:", apiDocs.Methods)
log.Println("Fetched API types:", apiDocs.Types)

// Store the fetched data in cache for non-Vercel environments
apiCache.Lock()
defer apiCache.Unlock()
apiCache.Methods = apiDocs.Methods
Expand All @@ -86,6 +89,26 @@ func fetchAPI() error {
return nil
}

func getAPICache() (map[string]Method, map[string]Type, error) {
if isVercel() {
log.Println("Fetching API directly on Vercel")
if err := fetchAPI(); err != nil {
log.Println("Error fetching API on Vercel:", err)
return nil, nil, err
}

// Return the fetched data directly
apiCache.RLock()
defer apiCache.RUnlock()
return apiCache.Methods, apiCache.Types, nil
}

// Use the cached version if not on Vercel
apiCache.RLock()
defer apiCache.RUnlock()
return apiCache.Methods, apiCache.Types, nil
}

// StartAPICacheUpdater starts a goroutine that periodically updates the API cache.
func StartAPICacheUpdater(interval time.Duration) {
go func() {
Expand All @@ -100,20 +123,6 @@ func StartAPICacheUpdater(interval time.Duration) {
}()
}

// getAPICache returns a snapshot of the current API cache.
func getAPICache() (map[string]Method, map[string]Type, error) {
if isVercel() {
// Fetch directly if on Vercel
if err := fetchAPI(); err != nil {
return nil, nil, err
}
}

apiCache.RLock()
defer apiCache.RUnlock()
return apiCache.Methods, apiCache.Types, nil
}

// inlineQueryHandler handles inline queries from the bot.
func inlineQueryHandler(bot *gotgbot.Bot, ctx *ext.Context) error {
query := strings.TrimSpace(ctx.InlineQuery.Query)
Expand Down
2 changes: 1 addition & 1 deletion sample.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
TOKEN=
OWNER_ID=
VERCEL=0
VERCEL=1

0 comments on commit 8d45c15

Please sign in to comment.