Handling Updates

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 messages
  • AddAlbumHandler - Register a handler for album messages
  • AddActionHandler - Register a handler for chat actions
  • AddInlineHandler - Register a handler for inline queries
  • AddCallbackHandler - Register a handler for callback queries
  • AddInlineCallbackHandler - Register a handler for inline callback queries
  • AddRawHandler - Register a handler for all updates
  • AddDeleteHandler - Register a handler for deleted messages
  • AddEditHandler - Register a handler for edited messages
  • AddParticipantHandler - 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.

main.go
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 received
  • telegram.OnInlineQuery - Fires on a new Inline query to the bot
  • telegram.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 ID
  • telegram.FilterUsers - Filter updates based on the user ID
  • telegram.FilterBlacklist - Only handle updates that are not in FilterChats or FilterUsers
  • telegram.FilterMention - Only handle updates that mention the bot
  • telegram.FilterPrivate - Only handle updates from private chats
  • telegram.FilterGroup - Only handle updates from group chats
  • telegram.FilterChannel - Only handle updates from channels
  • telegram.FilterCommand - Only handle updates that are commands
  • telegram.FilterForward - Only handle forwarded messages
  • telegram.FilterReply - Only handle replies
  • telegram.FilterFromBot - Only handle updates from bots
  • telegram.FilterMedia - Only handle media messages
  • telegram.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
})