Handling Updates
Here we are going to see how to handle updates in Gogram. Updates are events that occur in the your telegram account, such as new messages, new members joining a group, etc. Gogram provides a simple way to handle these updates by registering Handlers
for each type of update.
Registering Handlers
There are diffrent functions to handle different types of updates. Here are some of the most common update types:
// Fires when a new message is received
client.AddMessageHandler(telegram.OnNewMessage, func(update *telegram.NewMessage) error {
fmt.Println("New message:", update.Message.Text)
})
// Fires on a new Inline query to the bot
client.AddInlineHandler(telegram.OnInlineQuery, func(update *telegram.InlineQuery) error {
fmt.Println("Inline query:", update.Query)
})
// Fires when an Inline button is clicked
client.AddCallbackHandler(telegram.OnCallbackQuery, func(update *telegram.CallbackQuery) error {
fmt.Println("Callback query:", update.Data)
})
// Fires when a message is deleted
client.AddDeleteHandler(func(update *telegram.DeleteMessage) error {
fmt.Println("Message deleted:", update.MessageID)
})
Supported custom handlers:
AddMessageHandler
- Register a handler for new messagesAddAlbumHandler
- Register a handler for album messagesAddActionHandler
- Register a handler for chat actionsAddInlineHandler
- Register a handler for inline queriesAddCallbackHandler
- Register a handler for callback queriesAddInlineCallbackHandler
- Register a handler for inline callback queriesAddRawHandler
- Register a handler for all updatesAddDeleteHandler
- Register a handler for deleted messagesAddEditHandler
- Register a handler for edited messagesAddParticipantHandler
- Register a handler for new participants in a chat
Handling Updates
Once you have registered the handlers, you can start the client and it will automatically handle the updates for you.
client.Start()
Full Example
Here is a full example that demonstrates how to handle updates in Gogram:
package main
import (
"fmt"
"github.com/amarnathcjd/gogram/telegram"
)
func main() {
client, _ := telegram.NewClient(telegram.ClientConfig{ // Create a new client
AppID: 6,
AppHash: "app_hash",
})
client.AddMessageHandler("/start", func(update *telegram.NewMessage) error {
update.Reply("Hello!")
return nil
})
client.Start()
client.Idle() // block the main goroutine, waiting for updates
}
In this example, we register a handler for the /start
command. When the bot receives a message with the /start
command, it will reply with "Hello!".
Parameters in Handlers
First parameter is the Command
or Query
that the handler is registered for. The second parameter is the Handler
function that will be called when the command is received. The handler function takes an Update
object as a parameter, which contains the details of the update.
General Handler Params
To handle for eg. all messages, you can use the telegram.OnNewMessage
constant as the first parameter in the AddMessageHandler
function. This will register the handler for all new messages.
telegram.OnNewMessage
- Fires when a new message is receivedtelegram.OnInlineQuery
- Fires on a new Inline query to the bottelegram.OnCallbackQuery
- Fires when an Inline button is clicked
Raw Handler
You can also register a raw handler that will be called for all updates. This is useful when you want to handle all updates in a single function. or when you want to handle updates that are not supported by custom handlers.
client.AddRawHandler(func(update *telegram.Update) error {
fmt.Println("Raw update:", update)
return nil
})
Filters
You can also filter updates based on certain conditions. For example, you can filter updates based on the sender's ID or the chat ID.
client.AddMessageHandler(telegram.OnNewMessage, func(m *telegram.NewMessage) error {
fmt.Println(m.Text())
return nil
}, telegram.FilterChats(1, 2, 3)) // only handle messages from chats with IDs 1, 2, and 3
Supported Filters
telegram.FilterChats
- Filter updates based on the chat IDtelegram.FilterUsers
- Filter updates based on the user IDtelegram.FilterBlacklist
- Only handle updates that are not in FilterChats or FilterUserstelegram.FilterMention
- Only handle updates that mention the bottelegram.FilterPrivate
- Only handle updates from private chatstelegram.FilterGroup
- Only handle updates from group chatstelegram.FilterChannel
- Only handle updates from channelstelegram.FilterCommand
- Only handle updates that are commandstelegram.FilterForward
- Only handle forwarded messagestelegram.FilterReply
- Only handle repliestelegram.FilterFromBot
- Only handle updates from botstelegram.FilterMedia
- Only handle media messagestelegram.FilterFunc
- Custom filter function
use multiple filters by chaining them together:
client.AddMessageHandler(telegram.OnNewMessage, func(m *telegram.NewMessage) error {
fmt.Println(m.Text())
return nil
}, telegram.FilterUsers(1, 2, 3), telegram.FilterPrivate, telegram.FilterMedia)
Error Handling
All handlers should return an error. If an error is returned, the client will log the error and continue processing other updates. If you want to stop the client when an error occurs, you can call the Stop
method on the client.
client.AddMessageHandler(telegram.OnNewMessage, func(m *telegram.NewMessage) error {
if m.Text() == "stop" {
client.Stop()
}
return nil
})