Overview
This example demonstrates how to handle inline queries when users type @yourbot query in any chat.
Code
package examples
import (
"github.com/amarnathcjd/gogram/telegram"
)
const (
appID = 6
appHash = "YOUR_APP_HASH"
botToken = "YOUR_BOT_TOKEN"
)
func main() {
// Create a new client
client, _ := telegram.NewClient(telegram.ClientConfig{
AppID: appID,
AppHash: appHash,
LogLevel: telegram.LogInfo,
})
// Connect to the server
if err := client.Connect(); err != nil {
panic(err)
}
// Authenticate the client using the bot token
if err := client.LoginBot(botToken); err != nil {
panic(err)
}
// Add an inline query handler
client.AddInlineHandler(telegram.OnInlineQuery, HelloWorld)
// Start polling for updates
client.Idle()
}
// HelloWorld handles inline queries
func HelloWorld(i *telegram.InlineQuery) error {
// Create a result builder
builder := i.Builder()
// Add an article result
builder.Article("Hello World", "Hello World", "This is a test article")
// Answer the query with built results
_, err := i.Answer(builder.Results())
return err
}
How It Works
- Bot Setup: Creates client and authenticates with bot token
- Handler Registration: Adds inline query handler with
AddInlineHandler()
- Query Processing: Receives inline query when user types
@yourbot in any chat
- Build Results: Uses
Builder() to create result objects
- Send Response: Calls
Answer() to send results back to user
Result Types
Article
Photo
Document
Multiple Results
Search-Based
func HandleInline(i *telegram.InlineQuery) error {
builder := i.Builder()
// Simple article result
builder.Article(
"Title", // Title shown in results
"Description", // Description text
"Message content", // Content sent when selected
)
_, err := i.Answer(builder.Results())
return err
}
func PhotoResult(i *telegram.InlineQuery) error {
builder := i.Builder()
// Photo result from URL
builder.Photo(
"https://example.com/photo.jpg", // Photo URL
"Photo caption", // Caption
)
// Photo with thumbnail
builder.PhotoWithThumb(
"https://example.com/photo.jpg",
"https://example.com/thumb.jpg", // Thumbnail URL
"Photo caption",
)
_, err := i.Answer(builder.Results())
return err
}
func DocumentResult(i *telegram.InlineQuery) error {
builder := i.Builder()
// Document/file result
builder.Document(
"https://example.com/file.pdf", // File URL
"File.pdf", // File name
"PDF Document", // Description
)
_, err := i.Answer(builder.Results())
return err
}
func MultipleResults(i *telegram.InlineQuery) error {
builder := i.Builder()
// Add multiple results
builder.Article("Result 1", "First result", "Content 1")
builder.Article("Result 2", "Second result", "Content 2")
builder.Photo("https://example.com/photo1.jpg", "Photo 1")
builder.Photo("https://example.com/photo2.jpg", "Photo 2")
_, err := i.Answer(builder.Results())
return err
}
func SearchInline(i *telegram.InlineQuery) error {
query := i.Query // User's search query
builder := i.Builder()
// Return results based on query
if query == "" {
builder.Article("Help", "Search for something", "Type a query to search")
} else {
builder.Article(
"Search: "+query,
"You searched for: "+query,
"Results for: "+query,
)
}
_, err := i.Answer(builder.Results())
return err
}
Answer Options
Customize inline query responses with AnswerOptions:
func CustomAnswer(i *telegram.InlineQuery) error {
builder := i.Builder()
builder.Article("Result", "Description", "Content")
_, err := i.Answer(builder.Results(), &telegram.AnswerOptions{
CacheTime: 300, // Cache results for 5 minutes
IsPersonal: true, // Results are user-specific
NextOffset: "10", // Pagination offset for next results
SwitchPM: &telegram.InlineSwitchPM{ // "Switch to PM" button
Text: "Open bot in PM",
Param: "start",
},
})
return err
}
InlineQuery Properties
Access query information:
func DetailedHandler(i *telegram.InlineQuery) error {
// Query information
fmt.Println("Query:", i.Query) // User's search query
fmt.Println("User ID:", i.From.ID) // User who sent query
fmt.Println("Offset:", i.Offset) // Pagination offset
fmt.Println("Chat Type:", i.ChatType) // Where query was made
builder := i.Builder()
builder.Article("Info", "Query details", fmt.Sprintf("You searched: %s", i.Query))
_, err := i.Answer(builder.Results())
return err
}
Handle large result sets with pagination:
func PaginatedResults(i *telegram.InlineQuery) error {
offset := 0
if i.Offset != "" {
// Parse offset from string
fmt.Sscanf(i.Offset, "%d", &offset)
}
builder := i.Builder()
// Add 10 results starting from offset
for j := offset; j < offset+10; j++ {
builder.Article(
fmt.Sprintf("Result %d", j+1),
fmt.Sprintf("Description %d", j+1),
fmt.Sprintf("Content %d", j+1),
)
}
// Set next offset for pagination
nextOffset := fmt.Sprintf("%d", offset+10)
_, err := i.Answer(builder.Results(), &telegram.AnswerOptions{
NextOffset: nextOffset,
})
return err
}
Running the Example
- Replace
YOUR_APP_HASH and YOUR_BOT_TOKEN with your credentials
- Enable inline mode for your bot:
- Message @BotFather
- Send
/setinline
- Select your bot
- Provide a placeholder text (e.g., “Search…”)
- Optional: Enable inline feedback:
- Send
/setinlinefeedback to @BotFather
- Select your bot
- Choose “Enabled”
- Run the program
- In any chat, type
@yourbot hello to trigger inline query
Important: Your bot must have inline mode enabled via @BotFather. Without this, users cannot trigger inline queries.
Use inline feedback to track which results users select. This helps optimize your inline results based on user behavior.
Next Steps
Callback Queries
Handle button clicks in messages
Bot Keyboards
Create interactive keyboards