Channel methods
Everything you do with channels, supergroups, and broadcast lists. Reading history, walking members, promoting admins, creating new channels, minting invite links, managing forum topics.
Joining and leaving
_, err := client.JoinChannel("@durov")_, err := client.JoinChannel("@durov")JoinChanneltakes any peer form — username, id, t.me link, invite hash. For invite-only channels, pass the full invite URL.
err := client.LeaveChannel("@durov")
// also revoke the user's posted messages on leave:
err = client.LeaveChannel("@my_group", true)err := client.LeaveChannel("@durov")
// also revoke the user's posted messages on leave:
err = client.LeaveChannel("@my_group", true)LeaveChannel takes an optional revokeboolean — when true, the server deletes every message the leaving account posted in that channel.
Reading history
IterMessages walks the channel feed via a callback. Return an error to stop early; return nil to continue:
err := client.IterMessages("@durov", func(m *telegram.NewMessage) error {
fmt.Println(m.ID, m.Text())
return nil
}, &telegram.SearchOption{Limit: 500})err := client.IterMessages("@durov", func(m *telegram.NewMessage) error {
fmt.Println(m.ID, m.Text())
return nil
}, &telegram.SearchOption{Limit: 500})For one-shot fetches use GetMessages (returns a slice) or GetMessageByID(peer, id) for a single message.
Members
members, total, err := client.GetChatMembers("@my_group", &telegram.ParticipantOptions{
Filter: &telegram.ChannelParticipantsRecent{},
Limit: 200,
})
fmt.Printf("returning %d / %d\n", len(members), total)members, total, err := client.GetChatMembers("@my_group", &telegram.ParticipantOptions{
Filter: &telegram.ChannelParticipantsRecent{},
Limit: 200,
})
fmt.Printf("returning %d / %d\n", len(members), total)GetChatMembers returns a batch of participants plus the total count. Filters: ChannelParticipantsRecent, ChannelParticipantsAdmins, ChannelParticipantsBanned, ChannelParticipantsKicked, ChannelParticipantsBots, ChannelParticipantsSearch.
For large groups, IterChatMembers streams via channels:
out, errs := client.IterChatMembers("@my_group", &telegram.ParticipantOptions{
Filter: &telegram.ChannelParticipantsAdmins{},
})
for {
select {
case u, ok := <-out:
if !ok { return nil }
fmt.Println(u.User)
case err := <-errs:
if err != nil { return err }
}
}out, errs := client.IterChatMembers("@my_group", &telegram.ParticipantOptions{
Filter: &telegram.ChannelParticipantsAdmins{},
})
for {
select {
case u, ok := <-out:
if !ok { return nil }
fmt.Println(u.User)
case err := <-errs:
if err != nil { return err }
}
}Admins and bans
Promotion and demotion both go through EditAdmin. Pass the AdminOptions describing the rights you want granted; passing an empty Rights bitfield demotes the user.
// promote: pass AdminOptions describing the rights you want granted
ok, err := client.EditAdmin("@my_group", "@new_admin", &telegram.AdminOptions{
Rights: &telegram.ChatAdminRights{
PostMessages: true,
EditMessages: true,
DeleteMessages: true,
InviteUsers: true,
},
Rank: "Moderator",
})
// fluent alternative:
_, _ = client.EditAdminBuilder("@my_group", "@new_admin").
CanDeleteMessages().
CanInviteUsers().
Do()// promote: pass AdminOptions describing the rights you want granted
ok, err := client.EditAdmin("@my_group", "@new_admin", &telegram.AdminOptions{
Rights: &telegram.ChatAdminRights{
PostMessages: true,
EditMessages: true,
DeleteMessages: true,
InviteUsers: true,
},
Rank: "Moderator",
})
// fluent alternative:
_, _ = client.EditAdminBuilder("@my_group", "@new_admin").
CanDeleteMessages().
CanInviteUsers().
Do()Ban, mute, and restrict all go through EditBanned with a ChatBannedRights bitfield that says what the user is denied. ViewMessages: true is a full kick + ban; mixing with UntilDate turns it into a timed restriction.
// ban: pass BannedOptions with the rights you want denied
_, _ = client.EditBanned("@my_group", "@spammer", &telegram.BannedOptions{
Rights: &telegram.ChatBannedRights{
ViewMessages: true, // full ban
},
})
// mute for 1 hour without banning:
_, _ = client.EditBanned("@my_group", "@noisy", &telegram.BannedOptions{
Rights: &telegram.ChatBannedRights{
SendMessages: true,
UntilDate: int32(time.Now().Add(time.Hour).Unix()),
},
})
// kick (ban then unban so they can rejoin):
_, _ = client.KickParticipant("@my_group", "@quitter")// ban: pass BannedOptions with the rights you want denied
_, _ = client.EditBanned("@my_group", "@spammer", &telegram.BannedOptions{
Rights: &telegram.ChatBannedRights{
ViewMessages: true, // full ban
},
})
// mute for 1 hour without banning:
_, _ = client.EditBanned("@my_group", "@noisy", &telegram.BannedOptions{
Rights: &telegram.ChatBannedRights{
SendMessages: true,
UntilDate: int32(time.Now().Add(time.Hour).Unix()),
},
})
// kick (ban then unban so they can rejoin):
_, _ = client.KickParticipant("@my_group", "@quitter")Editing the channel
_, err := client.EditTitle("@my_group", "Updates v2")_, err := client.EditTitle("@my_group", "Updates v2")For the about text, photo, and slowmode the matching helpers live on the channels namespace; raw TL calls give you the rest.
Creating channels and groups
// broadcast channel (default behaviour)
channel, err := client.CreateChannel("Updates", &telegram.ChannelOptions{
About: "Daily build notifications",
})
// supergroup: opt-out of broadcast and turn on megagroup
group, err := client.CreateChannel("Team", &telegram.ChannelOptions{
About: "Internal chat",
NotBroadcast: true,
Megagroup: true,
})// broadcast channel (default behaviour)
channel, err := client.CreateChannel("Updates", &telegram.ChannelOptions{
About: "Daily build notifications",
})
// supergroup: opt-out of broadcast and turn on megagroup
group, err := client.CreateChannel("Team", &telegram.ChannelOptions{
About: "Internal chat",
NotBroadcast: true,
Megagroup: true,
})CreateChannel defaults to a broadcast channel. Set NotBroadcast: true and Megagroup: true to create a supergroup instead. Forum mode is enabled afterwards via EnableForum.
Invite links
link, err := client.ExportInvite("@my_group")
fmt.Println(link)
// to revoke later:
err = client.RevokeInvite("@my_group", "https://t.me/+abcdef...")link, err := client.ExportInvite("@my_group")
fmt.Println(link)
// to revoke later:
err = client.RevokeInvite("@my_group", "https://t.me/+abcdef...")ExportInvite returns a generic primary invite. For limited links with usage caps, expiry dates, or join-request approval, use GetChatInviteLink with InviteLinkOptions.
Forum topics
Forum mode must be enabled on the supergroup first:
_ = client.EnableForum("@my_forum", true)_ = client.EnableForum("@my_forum", true)Then create and manage topics:
topicID, err := client.CreateTopic("@my_forum", "Random", &telegram.CreateTopicOptions{
IconColor: 0x6FB9F0,
})
// post into it:
client.SendMessage("@my_forum", "first post", &telegram.SendOptions{
TopicID: topicID,
})topicID, err := client.CreateTopic("@my_forum", "Random", &telegram.CreateTopicOptions{
IconColor: 0x6FB9F0,
})
// post into it:
client.SendMessage("@my_forum", "first post", &telegram.SendOptions{
TopicID: topicID,
})_ = client.CloseTopic("@my_forum", topicID)
_ = client.ReopenTopic("@my_forum", topicID)
_ = client.PinTopic("@my_forum", topicID)
_ = client.HideTopic("@my_forum", topicID)
_ = client.DeleteTopic("@my_forum", topicID)_ = client.CloseTopic("@my_forum", topicID)
_ = client.ReopenTopic("@my_forum", topicID)
_ = client.PinTopic("@my_forum", topicID)
_ = client.HideTopic("@my_forum", topicID)
_ = client.DeleteTopic("@my_forum", topicID)