Sends a message using the Telegram API. The first parameter is the recipient (chat ID, username, or “me” for saved messages), and the second is the message text.
// Send to user by usernameclient.SendMessage("@username", "Hello!")// Send to chat by IDclient.SendMessage(123456789, "Hello!")// Send to yourself (Saved Messages)client.SendMessage("me", "Hello!")
// From fileclient.SendMedia("me", "photo.jpg", &telegram.MediaOptions{ Caption: "Check out this photo!",})// From URLclient.SendMedia("me", "https://example.com/photo.jpg")
// Get your own infome, err := client.GetMe()fmt.Printf("Username: @%s\n", me.Username)// Get another user's infouser, err := client.GetUser("@username")fmt.Printf("Name: %s %s\n", user.FirstName, user.LastName)
Gogram is goroutine-safe, meaning you can safely use the same client instance across multiple goroutines. This is perfect for handling concurrent operations.
package mainimport ( "fmt" "github.com/amarnathcjd/gogram/telegram")func main() { client, _ := telegram.NewClient(telegram.ClientConfig{ AppID: 6, AppHash: "app_hash", }) client.Conn() client.LoginBot("bot_token") // Each handler runs in its own goroutine automatically client.On("message", func(m *telegram.NewMessage) error { // Process message - runs concurrently for each message go processMessage(m) return nil }) client.On("callback", func(c *telegram.CallbackQuery) error { // Process callback - runs concurrently for each callback go processCallback(c) return nil }) client.Idle()}func processMessage(m *telegram.NewMessage) { // This runs in a separate goroutine // Safe to do blocking operations here fmt.Printf("Processing message from %d\n", m.SenderID) // Simulate heavy processing result := doHeavyWork(m.Text()) // Send response using the same client instance m.Reply(result)}func processCallback(c *telegram.CallbackQuery) { // Also runs in a separate goroutine fmt.Printf("Processing callback: %s\n", c.DataString()) c.Answer("Processing...")}
The entire raw Telegram API is available as functions on the client type for ease of use. Every method from the Telegram API documentation can be called directly.
package mainimport ( "fmt" "github.com/amarnathcjd/gogram/telegram")func main() { client, _ := telegram.NewClient(telegram.ClientConfig{ AppID: 6, AppHash: "app_hash", }) client.Conn() client.LoginBot("bot_token") // 1. messages.getDialogs - Get user's chats dialogs, err := client.MessagesGetDialogs(&telegram.MessagesGetDialogsParams{ Limit: 20, }) if err == nil { fmt.Printf("Found %d dialogs\n", len(dialogs.Dialogs)) } // 2. users.getFullUser - Get detailed user information fullUser, err := client.UsersGetFullUser(&telegram.InputUserSelf{}) if err == nil { fmt.Printf("User bio: %s\n", fullUser.FullUser.About) } // 3. channels.getParticipants - Get channel members participants, err := client.ChannelsGetParticipants(&telegram.ChannelsGetParticipantsParams{ Channel: &telegram.InputChannel{ ChannelID: 123456789, AccessHash: 0, // Get from channel object }, Filter: &telegram.ChannelParticipantsRecent{}, Limit: 100, }) if err == nil { fmt.Printf("Channel has %d participants\n", participants.Count) }}
All Telegram API methods follow the format Category.MethodName (e.g., messages.sendMessage, users.getFullUser, channels.getParticipants) and are available as client.CategoryMethodName() in Gogram.