package examplesimport ( "fmt" "github.com/amarnathcjd/gogram/telegram")const ( appID = 6 appHash = "YOUR_APP_HASH" phoneNum = "YOUR_PHONE_NUMBER")func main() { // Create client client, _ := telegram.NewClient(telegram.ClientConfig{ AppID: appID, AppHash: appHash, }) // Connect to Telegram client.Conn() // Authenticate (prompts for phone/code/password) client.AuthPrompt() // Get all your unique gifts and send the first one to a user gifts, _ := client.GetMyGifts(true) // true = unique gifts only for _, gift := range gifts { gft := (*gift).(*telegram.StarGiftUnique) // Transfer the gift to the user client.SendGift("roseloverx", gft.ID, "Here is a gift for you!") break } // Buying new gifts and sending them to users availGifts, _ := client.PaymentsGetStarGifts(0) // 0 = get all available gifts for _, gift := range availGifts.(*telegram.PaymentsStarGiftsObj).Gifts { gft := gift.(*telegram.StarGiftObj) // Buy and send the gift client.SendNewGift("roseloverx", gft.ID, "Here is a gift for you!") break } // Add a handler for receiving gifts client.AddActionHandler(func(m *telegram.NewMessage) error { // Check for regular star gift if action, ok := m.Action.(*telegram.MessageActionStarGift); ok { fmt.Println("Gift received", action.Gift) } else if action, ok := m.Action.(*telegram.MessageActionStarGiftUnique); ok { // Check for unique star gift fmt.Println("Unique gift received", action.Gift) } return nil })}
// Get only unique (limited edition) giftsuniqueGifts, _ := client.GetMyGifts(true)for _, gift := range uniqueGifts { gft := (*gift).(*telegram.StarGiftUnique) fmt.Printf("Unique Gift ID: %d\n", gft.ID) fmt.Printf("Title: %s\n", gft.Title) fmt.Printf("Number: %d\n", gft.Number) // Edition number // Send unique gift client.SendGift("username", gft.ID, "Special gift for you!")}
// Get all gifts (including regular ones)allGifts, _ := client.GetMyGifts(false)for _, gift := range allGifts { // Check gift type if gft, ok := (*gift).(*telegram.StarGiftObj); ok { fmt.Printf("Regular Gift ID: %d\n", gft.ID) fmt.Printf("Stars: %d\n", gft.Stars) // Cost in stars client.SendGift("username", gft.ID, "Gift for you!") }}
// Get gifts available for purchaseavailable, _ := client.PaymentsGetStarGifts(0)gifts := available.(*telegram.PaymentsStarGiftsObj).Giftsfor _, gift := range gifts { gft := gift.(*telegram.StarGiftObj) fmt.Printf("Gift: %s\n", gft.ID) fmt.Printf("Price: %d stars\n", gft.Stars) fmt.Printf("Available: %t\n", gft.Available) // Buy and send if gft.Available { client.SendNewGift("username", gft.ID, "New gift!") }}
// Get limited number of available giftslimited, _ := client.PaymentsGetStarGifts(10) // Get first 10 giftsgifts := limited.(*telegram.PaymentsStarGiftsObj).Giftsfmt.Printf("Showing %d available gifts\n", len(gifts))for _, gift := range gifts { gft := gift.(*telegram.StarGiftObj) fmt.Printf("- %s (%d stars)\n", gft.ID, gft.Stars)}
// Transfer a gift from your inventoryfunc SendOwnedGift(client *telegram.Client, recipient string) { gifts, _ := client.GetMyGifts(false) if len(gifts) == 0 { fmt.Println("No gifts to send!") return } // Send first available gift gft := (*gifts[0]).(*telegram.StarGiftObj) _, err := client.SendGift(recipient, gft.ID, "A gift from me to you! π") if err != nil { fmt.Println("Error sending gift:", err) } else { fmt.Println("Gift sent successfully!") }}
// Purchase and send a new gift in one stepfunc BuyAndSendGift(client *telegram.Client, recipient string, giftID int64) { _, err := client.SendNewGift(recipient, giftID, "Here's a special gift! β") if err != nil { fmt.Println("Error buying/sending gift:", err) } else { fmt.Println("Gift purchased and sent!") }}
// Send gifts to multiple recipientsfunc SendGiftToMany(client *telegram.Client, recipients []string, giftID int64) { for _, recipient := range recipients { _, err := client.SendNewGift(recipient, giftID, "You're awesome! π") if err != nil { fmt.Printf("Failed to send to %s: %v\n", recipient, err) } else { fmt.Printf("Sent to %s β\n", recipient) } // Rate limiting time.Sleep(1 * time.Second) }}
// Find and send the cheapest available giftfunc SendCheapestGift(client *telegram.Client, recipient string) { available, _ := client.PaymentsGetStarGifts(0) gifts := available.(*telegram.PaymentsStarGiftsObj).Gifts var cheapest *telegram.StarGiftObj minStars := int64(999999) for _, gift := range gifts { gft := gift.(*telegram.StarGiftObj) if gft.Available && gft.Stars < minStars { minStars = gft.Stars cheapest = gft } } if cheapest != nil { client.SendNewGift(recipient, cheapest.ID, "A small gift! π") fmt.Printf("Sent gift costing %d stars\n", cheapest.Stars) }}
client.AddActionHandler(func(m *telegram.NewMessage) error { // Handle regular star gift if action, ok := m.Action.(*telegram.MessageActionStarGift); ok { gft := action.Gift.(*telegram.StarGiftObj) fmt.Printf("Received gift from %s\n", m.Sender.FirstName) fmt.Printf("Gift ID: %d\n", gft.ID) fmt.Printf("Stars: %d\n", gft.Stars) fmt.Printf("Message: %s\n", action.Message) // Thank the sender m.Reply("Thank you for the gift! π") // Or send a gift back client.SendGift(m.SenderID(), gft.ID, "A gift for a gift! π") } // Handle unique star gift if action, ok := m.Action.(*telegram.MessageActionStarGiftUnique); ok { gft := action.Gift.(*telegram.StarGiftUnique) fmt.Printf("Received UNIQUE gift #%d\n", gft.Number) fmt.Printf("Title: %s\n", gft.Title) m.Reply("Wow, a unique gift! Thank you! β") } return nil})