This example demonstrates how to broadcast messages to all users who have interacted with your bot using Telegram’s update history, without needing an external database.
package examplesimport ( "github.com/amarnathcjd/gogram/telegram")// Broadcasting to bot users/chats using updates.GetDifference// client.Broadcast returns peers in update history via channels// These peers can be used for broadcasting messages// No external database is required to store user/chat IDsconst ( appID = 6 appHash = "YOUR_APP_HASH" botToken = "YOUR_BOT_TOKEN")func main() { // Create a new client object client, _ := telegram.NewClient(telegram.ClientConfig{ AppID: appID, AppHash: appHash, LogLevel: telegram.LogInfo, }) // Authenticate as a bot client.LoginBot(botToken) // Get all users and chats from update history users, chats, err := client.Broadcast() if err != nil { panic(err) } // Broadcast to all users for user := range users { client.SendMessage(user, "Hello, This is a broadcast message") } // Broadcast to all chats (groups/channels) for chat := range chats { client.SendMessage(chat, "Hello, This is a broadcast message") }}
Replace YOUR_APP_HASH and YOUR_BOT_TOKEN with your credentials
Run the program
The broadcast will be sent to all users and chats in the bot’s update history
Rate Limits: Telegram has strict rate limits for broadcasting:
Max ~30 messages per second to different users
Slower for groups
Use time.Sleep() between messages to avoid flood errors
Update History Limit: Broadcast() only returns peers from recent update history. For comprehensive user tracking, consider maintaining your own database.