Documentation
¶
Overview ¶
Package peers contains helpers to work with Telegram peers.
NB: this package is completely experimental and still WIP. API and behavior may be changed dramatically, so use it with caution.
Index ¶
- type AdminLog
- func (l *AdminLog) Admins(admins ...tg.InputUserClass) *AdminLog
- func (l *AdminLog) Fetch(ctx context.Context, maxID int64, limit int) ([]tg.ChannelAdminLogEvent, error)
- func (l *AdminLog) Filter(filter tg.ChannelAdminLogEventsFilter) *AdminLog
- func (l *AdminLog) ForEach(ctx context.Context, cb AdminLogCallback) error
- func (l *AdminLog) Search(q string) *AdminLog
- type AdminLogCallback
- type Bot
- type Broadcast
- type Cache
- type Channel
- func (c Channel) AdminLog() *AdminLog
- func (c Channel) AdminRights() (tg.ChatAdminRights, bool)
- func (c Channel) BannedRights() (tg.ChatBannedRights, bool)
- func (c Channel) CallActive() bool
- func (c Channel) CallNotEmpty() bool
- func (c Channel) CheckUsername(ctx context.Context, username string) (bool, error)
- func (c Channel) Creator() bool
- func (c Channel) DeactivateAllUsernames(ctx context.Context) error
- func (c Channel) DefaultBannedRights() (tg.ChatBannedRights, bool)
- func (c Channel) Delete(ctx context.Context) error
- func (c Channel) DeletePhoto(ctx context.Context) error
- func (c Channel) DisableReactions(ctx context.Context) error
- func (c Channel) Fake() bool
- func (c Channel) FullRaw(ctx context.Context) (*tg.ChannelFull, error)
- func (c Channel) HasGeo() bool
- func (c Channel) HasLink() bool
- func (c Channel) ID() int64
- func (c Channel) InputChannel() tg.InputChannelClass
- func (c Channel) InputPeer() tg.InputPeerClass
- func (c Channel) InviteLinks() InviteLinks
- func (c Channel) IsBroadcast() bool
- func (c Channel) IsSupergroup() bool
- func (c Channel) Join(ctx context.Context) error
- func (c Channel) Leave(ctx context.Context) error
- func (c Channel) Left() bool
- func (c Channel) Manager() *Manager
- func (c Channel) NoForwards() bool
- func (c Channel) ParticipantsCount() int
- func (c Channel) Photo(ctx context.Context) (*tg.Photo, bool, error)
- func (c Channel) Raw() *tg.Channel
- func (c Channel) RecommendedChannels(ctx context.Context) (RecommendedChannels, error)
- func (c Channel) Report(ctx context.Context, reason tg.ReportReasonClass, message string) error
- func (c Channel) Restricted() ([]tg.RestrictionReason, bool)
- func (c Channel) Scam() bool
- func (c Channel) SetDescription(ctx context.Context, about string) error
- func (c Channel) SetPhoto(ctx context.Context, photo tg.InputChatPhotoClass) error
- func (c Channel) SetReactions(ctx context.Context, reactions ...tg.ReactionClass) error
- func (c Channel) SetTitle(ctx context.Context, title string) error
- func (c Channel) SetUsername(ctx context.Context, username string) error
- func (c Channel) Sync(ctx context.Context) error
- func (c Channel) TDLibPeerID() constant.TDLibPeerID
- func (c Channel) ToBroadcast() (Broadcast, bool)
- func (c Channel) ToSupergroup() (Supergroup, bool)
- func (c Channel) Username() (string, bool)
- func (c Channel) Verified() bool
- func (c Channel) VisibleName() string
- type Chat
- func (c Chat) ActualChat(ctx context.Context) (Channel, bool, error)
- func (c Chat) AdminRights() (tg.ChatAdminRights, bool)
- func (c Chat) CallActive() bool
- func (c Chat) CallNotEmpty() bool
- func (c Chat) Creator() bool
- func (c Chat) Deactivated() bool
- func (c Chat) DefaultBannedRights() (tg.ChatBannedRights, bool)
- func (c Chat) DisableReactions(ctx context.Context) error
- func (c Chat) Fake() bool
- func (c Chat) FullRaw(ctx context.Context) (*tg.ChatFull, error)
- func (c Chat) ID() int64
- func (c Chat) InputPeer() tg.InputPeerClass
- func (c Chat) InviteLinks() InviteLinks
- func (c Chat) IsBroadcast() bool
- func (c Chat) IsSupergroup() bool
- func (c Chat) Leave(ctx context.Context) error
- func (c Chat) LeaveAndDelete(ctx context.Context) error
- func (c Chat) Left() bool
- func (c Chat) Manager() *Manager
- func (c Chat) MigratedTo() (tg.InputChannelClass, bool)
- func (c Chat) NoForwards() bool
- func (c Chat) ParticipantsCount() int
- func (c Chat) Photo(ctx context.Context) (*tg.Photo, bool, error)
- func (c Chat) Raw() *tg.Chat
- func (c Chat) Report(ctx context.Context, reason tg.ReportReasonClass, message string) error
- func (c Chat) Restricted() ([]tg.RestrictionReason, bool)
- func (c Chat) Scam() bool
- func (c Chat) SetDescription(ctx context.Context, about string) error
- func (c Chat) SetReactions(ctx context.Context, reactions ...tg.ReactionClass) error
- func (c Chat) SetTitle(ctx context.Context, title string) error
- func (c Chat) Sync(ctx context.Context) error
- func (c Chat) TDLibPeerID() constant.TDLibPeerID
- func (c Chat) ToBroadcast() (Broadcast, bool)
- func (c Chat) ToSupergroup() (Supergroup, bool)
- func (c Chat) Username() (string, bool)
- func (c Chat) Verified() bool
- func (c Chat) VisibleName() string
- type ExportLinkOptions
- type InmemoryCache
- func (f *InmemoryCache) FindChannel(ctx context.Context, id int64) (*tg.Channel, bool, error)
- func (f *InmemoryCache) FindChannelFull(ctx context.Context, id int64) (*tg.ChannelFull, bool, error)
- func (f *InmemoryCache) FindChat(ctx context.Context, id int64) (*tg.Chat, bool, error)
- func (f *InmemoryCache) FindChatFull(ctx context.Context, id int64) (*tg.ChatFull, bool, error)
- func (f *InmemoryCache) FindUser(ctx context.Context, id int64) (*tg.User, bool, error)
- func (f *InmemoryCache) FindUserFull(ctx context.Context, id int64) (*tg.UserFull, bool, error)
- func (f *InmemoryCache) SaveChannelFulls(ctx context.Context, channels ...*tg.ChannelFull) error
- func (f *InmemoryCache) SaveChannels(ctx context.Context, channels ...*tg.Channel) error
- func (f *InmemoryCache) SaveChatFulls(ctx context.Context, chats ...*tg.ChatFull) error
- func (f *InmemoryCache) SaveChats(ctx context.Context, chats ...*tg.Chat) error
- func (f *InmemoryCache) SaveUserFulls(ctx context.Context, users ...*tg.UserFull) error
- func (f *InmemoryCache) SaveUsers(ctx context.Context, users ...*tg.User) error
- type InmemoryStorage
- func (f *InmemoryStorage) Find(ctx context.Context, key Key) (value Value, found bool, _ error)
- func (f *InmemoryStorage) FindPhone(ctx context.Context, phone string) (key Key, value Value, found bool, err error)
- func (f *InmemoryStorage) GetContactsHash(ctx context.Context) (int64, error)
- func (f *InmemoryStorage) Save(ctx context.Context, key Key, value Value) error
- func (f *InmemoryStorage) SaveContactsHash(ctx context.Context, hash int64) error
- func (f *InmemoryStorage) SavePhone(ctx context.Context, phone string, key Key) error
- type InviteLink
- func (l InviteLink) CreatedAt() time.Time
- func (l InviteLink) Creator(ctx context.Context) (User, error)
- func (l InviteLink) ExpireDate() (time.Time, bool)
- func (l InviteLink) Link() string
- func (l InviteLink) Permanent() bool
- func (l InviteLink) Raw() *tg.ChatInviteExported
- func (l InviteLink) ReplacedWith() (InviteLink, bool)
- func (l InviteLink) RequestNeeded() bool
- func (l InviteLink) Requested() (int, bool)
- func (l InviteLink) Revoked() bool
- func (l InviteLink) StartDate() (time.Time, bool)
- func (l InviteLink) Title() (string, bool)
- func (l InviteLink) Usage() (int, bool)
- func (l InviteLink) UsageLimit() (int, bool)
- type InviteLinks
- func (e InviteLinks) AddNew(ctx context.Context, opts ExportLinkOptions) (InviteLink, error)
- func (e InviteLinks) ApproveJoin(ctx context.Context, user tg.InputUserClass) error
- func (e InviteLinks) DeclineJoin(ctx context.Context, user tg.InputUserClass) error
- func (e InviteLinks) Delete(ctx context.Context, link string) error
- func (e InviteLinks) Edit(ctx context.Context, link string, opts ExportLinkOptions) (InviteLink, error)
- func (e InviteLinks) ExportNew(ctx context.Context, opts ExportLinkOptions) (InviteLink, error)
- func (e InviteLinks) Get(ctx context.Context, link string) (InviteLink, error)
- func (e InviteLinks) Revoke(ctx context.Context, link string) (InviteLink, error)
- type Key
- type Manager
- func (m *Manager) API() *tg.Client
- func (m *Manager) Apply(ctx context.Context, users []tg.UserClass, chats []tg.ChatClass) error
- func (m *Manager) Channel(u *tg.Channel) Channel
- func (m *Manager) Chat(u *tg.Chat) Chat
- func (m *Manager) FromInputPeer(ctx context.Context, p tg.InputPeerClass) (Peer, error)
- func (m *Manager) GetChannel(ctx context.Context, p tg.InputChannelClass) (Channel, error)
- func (m *Manager) GetChannelAccessHash(ctx context.Context, userID, channelID int64) (accessHash int64, found bool, err error)
- func (m *Manager) GetChat(ctx context.Context, id int64) (Chat, error)
- func (m *Manager) GetUser(ctx context.Context, p tg.InputUserClass) (User, error)
- func (m *Manager) ImportInvite(ctx context.Context, hash string) (Peer, error)
- func (m *Manager) Init(ctx context.Context) error
- func (m *Manager) JoinLink(ctx context.Context, link string) (Peer, error)
- func (m *Manager) RecommendedChannels(ctx context.Context) (RecommendedChannels, error)
- func (m *Manager) Resolve(ctx context.Context, from string) (Peer, error)
- func (m *Manager) ResolveBusinessChat(ctx context.Context, u string) (p Peer, me MsgAndEntities, err error)
- func (m *Manager) ResolveChannelID(ctx context.Context, id int64) (Channel, error)
- func (m *Manager) ResolveChatID(ctx context.Context, id int64) (Chat, error)
- func (m *Manager) ResolveDeeplink(ctx context.Context, u string) (Peer, error)
- func (m *Manager) ResolveDeeplinkJoin(ctx context.Context, u string) (tg.ChatInviteClass, error)
- func (m *Manager) ResolveDomain(ctx context.Context, domain string) (Peer, error)
- func (m *Manager) ResolvePeer(ctx context.Context, p tg.PeerClass) (Peer, error)
- func (m *Manager) ResolvePhone(ctx context.Context, phone string) (User, error)
- func (m *Manager) ResolveTDLibID(ctx context.Context, peerID constant.TDLibPeerID) (p Peer, err error)
- func (m *Manager) ResolveUserID(ctx context.Context, id int64) (User, error)
- func (m *Manager) Search(ctx context.Context, q string) (SearchResult, error)
- func (m *Manager) Self(ctx context.Context) (User, error)
- func (m *Manager) SetChannelAccessHash(ctx context.Context, userID, channelID, accessHash int64) error
- func (m *Manager) UpdateHook(next telegram.UpdateHandler) telegram.UpdateHandler
- func (m *Manager) User(u *tg.User) User
- func (m *Manager) UserResolveHook(ctx context.Context) entity.UserResolver
- type MsgAndEntities
- type NoopCache
- func (n NoopCache) FindChannel(ctx context.Context, id int64) (*tg.Channel, bool, error)
- func (n NoopCache) FindChannelFull(ctx context.Context, id int64) (*tg.ChannelFull, bool, error)
- func (n NoopCache) FindChat(ctx context.Context, id int64) (*tg.Chat, bool, error)
- func (n NoopCache) FindChatFull(ctx context.Context, id int64) (*tg.ChatFull, bool, error)
- func (n NoopCache) FindUser(ctx context.Context, id int64) (*tg.User, bool, error)
- func (n NoopCache) FindUserFull(ctx context.Context, id int64) (*tg.UserFull, bool, error)
- func (n NoopCache) SaveChannelFulls(ctx context.Context, channels ...*tg.ChannelFull) error
- func (n NoopCache) SaveChannels(ctx context.Context, channels ...*tg.Channel) error
- func (n NoopCache) SaveChatFulls(ctx context.Context, chats ...*tg.ChatFull) error
- func (n NoopCache) SaveChats(ctx context.Context, chats ...*tg.Chat) error
- func (n NoopCache) SaveUserFulls(ctx context.Context, users ...*tg.UserFull) error
- func (n NoopCache) SaveUsers(ctx context.Context, users ...*tg.User) error
- type Options
- type Peer
- type PeerNotFoundError
- type PhoneNotFoundError
- type RecommendedChannels
- type SearchResult
- type Storage
- type Supergroup
- func (c Supergroup) DisableSlowMode(ctx context.Context) error
- func (c Supergroup) ResetStickerSet(ctx context.Context) error
- func (c Supergroup) SetStickerSet(ctx context.Context, set tg.InputStickerSetClass) error
- func (c Supergroup) SlowmodeEnabled() bool
- func (c Supergroup) ToggleJoinRequest(ctx context.Context, enabled bool) error
- func (c Supergroup) ToggleJoinToSend(ctx context.Context, enabled bool) error
- func (c Supergroup) TogglePreHistoryHidden(ctx context.Context, enabled bool) error
- func (c Supergroup) ToggleSlowMode(ctx context.Context, seconds int) error
- type User
- func (u User) Block(ctx context.Context) error
- func (u User) Contact() bool
- func (u User) Deleted() bool
- func (u User) Fake() bool
- func (u User) FirstName() (string, bool)
- func (u User) FullRaw(ctx context.Context) (*tg.UserFull, error)
- func (u User) ID() int64
- func (u User) InputPeer() tg.InputPeerClass
- func (u User) InputUser() tg.InputUserClass
- func (u User) InviteTo(ctx context.Context, ch tg.InputChannelClass) error
- func (u User) LangCode() (string, bool)
- func (u User) LastName() (string, bool)
- func (u User) Manager() *Manager
- func (u User) MutualContact() bool
- func (u User) Phone() (string, bool)
- func (u User) Photo(ctx context.Context) (*tg.Photo, bool, error)
- func (u User) Raw() *tg.User
- func (u User) Report(ctx context.Context, reason tg.ReportReasonClass, message string) error
- func (u User) ReportSpam(ctx context.Context) error
- func (u User) Restricted() ([]tg.RestrictionReason, bool)
- func (u User) Scam() bool
- func (u User) Self() bool
- func (u User) Status() (tg.UserStatusClass, bool)
- func (u User) Support() bool
- func (u User) Sync(ctx context.Context) error
- func (u User) TDLibPeerID() constant.TDLibPeerID
- func (u User) ToBot() (Bot, bool)
- func (u User) Unblock(ctx context.Context) error
- func (u User) Username() (string, bool)
- func (u User) Verified() bool
- func (u User) VisibleName() string
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AdminLog ¶ added in v0.152.0
type AdminLog struct {
// contains filtered or unexported fields
}
AdminLog is a query for the recent actions log (admin log) of a channel/supergroup.
See https://core.telegram.org/method/channels.getAdminLog.
func (*AdminLog) Admins ¶ added in v0.152.0
func (l *AdminLog) Admins(admins ...tg.InputUserClass) *AdminLog
Admins filters events by the given admins.
If not set, events from all admins are returned.
func (*AdminLog) Fetch ¶ added in v0.152.0
func (l *AdminLog) Fetch(ctx context.Context, maxID int64, limit int) ([]tg.ChannelAdminLogEvent, error)
Fetch fetches a single batch of admin log events older than maxID (pass zero to start from the newest event).
Use the returned events' IDs as the next maxID to paginate.
func (*AdminLog) Filter ¶ added in v0.152.0
func (l *AdminLog) Filter(filter tg.ChannelAdminLogEventsFilter) *AdminLog
Filter sets the filter of event types to fetch.
If not set, all event types are returned.
type AdminLogCallback ¶ added in v0.152.0
type AdminLogCallback = func(event tg.ChannelAdminLogEvent) error
AdminLogCallback is the callback called for every admin log event.
type Bot ¶
type Bot struct {
User
}
Bot is a bot User.
func (Bot) CanBeAdded ¶
CanBeAdded whether can the bot be added to group.
func (Bot) ChatHistory ¶
ChatHistory whether can the bot see all messages in groups.
func (Bot) InlinePlaceholder ¶
InlinePlaceholder returns inline placeholder for this inline bot.
func (Bot) SupportsInline ¶
SupportsInline whether the bot supports inline queries.
type Broadcast ¶
type Broadcast struct {
Channel
}
Broadcast is a broadcast Channel.
func (Broadcast) DiscussionGroup ¶
DiscussionGroup returns linked chat, if any.
func (Broadcast) SetDiscussionGroup ¶
SetDiscussionGroup associates a group to a channel as discussion group for that channel.
func (Broadcast) Signatures ¶
Signatures whether signatures are enabled (channels).
type Cache ¶
type Cache interface {
SaveUsers(ctx context.Context, users ...*tg.User) error
SaveUserFulls(ctx context.Context, users ...*tg.UserFull) error
FindUser(ctx context.Context, id int64) (*tg.User, bool, error)
FindUserFull(ctx context.Context, id int64) (*tg.UserFull, bool, error)
SaveChats(ctx context.Context, chats ...*tg.Chat) error
SaveChatFulls(ctx context.Context, chats ...*tg.ChatFull) error
FindChat(ctx context.Context, id int64) (*tg.Chat, bool, error)
FindChatFull(ctx context.Context, id int64) (*tg.ChatFull, bool, error)
SaveChannels(ctx context.Context, channels ...*tg.Channel) error
SaveChannelFulls(ctx context.Context, channels ...*tg.ChannelFull) error
FindChannel(ctx context.Context, id int64) (*tg.Channel, bool, error)
FindChannelFull(ctx context.Context, id int64) (*tg.ChannelFull, bool, error)
}
Cache is peer entities cache.
type Channel ¶
type Channel struct {
// contains filtered or unexported fields
}
Channel is channel peer.
func (Channel) AdminLog ¶ added in v0.152.0
AdminLog returns recent actions log query for this channel/supergroup.
Only available to administrators.
Example ¶
package main
import (
"context"
"fmt"
"os"
"os/signal"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/peers"
"github.com/gotd/td/telegram/peers/members"
"github.com/gotd/td/tg"
)
func administrate(ctx context.Context) error {
client, err := telegram.ClientFromEnvironment(telegram.Options{})
if err != nil {
return err
}
return client.Run(ctx, func(ctx context.Context) error {
m := peers.Options{}.Build(client.API())
// Resolve a supergroup to administrate.
p, err := m.Resolve(ctx, "gotd_test")
if err != nil {
return err
}
ch, ok := p.(peers.Channel)
if !ok {
return fmt.Errorf("%q is not a channel", "gotd_test")
}
sg, ok := ch.ToSupergroup()
if !ok {
return fmt.Errorf("%q is not a supergroup", "gotd_test")
}
// Set a public username.
if err := sg.SetUsername(ctx, "gotd_example"); err != nil {
return err
}
// Require admin approval for new members and hide message
// history from them.
if err := sg.ToggleJoinRequest(ctx, true); err != nil {
return err
}
if err := sg.TogglePreHistoryHidden(ctx, true); err != nil {
return err
}
// Promote a user to an admin allowed to ban and pin.
admin, err := m.Resolve(ctx, "gotd_admin")
if err != nil {
return err
}
user, ok := admin.(peers.User)
if !ok {
return fmt.Errorf("%q is not a user", "gotd_admin")
}
if err := members.Channel(sg.Channel).Promote(ctx, user.InputUser(), members.AdminRights{
Rank: "moderator",
BanUsers: true,
PinMessages: true,
}); err != nil {
return err
}
// Walk the recent admin actions log.
return sg.AdminLog().ForEach(ctx, func(event tg.ChannelAdminLogEvent) error {
fmt.Println("admin action", event.ID, "by", event.UserID)
return nil
})
})
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
if err := administrate(ctx); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(2)
}
}
Output:
func (Channel) AdminRights ¶
func (c Channel) AdminRights() (tg.ChatAdminRights, bool)
AdminRights returns admin rights of the user in this channel.
func (Channel) BannedRights ¶
func (c Channel) BannedRights() (tg.ChatBannedRights, bool)
BannedRights returns banned rights of the user in this channel.
func (Channel) CallActive ¶
CallActive whether a group call or livestream is currently active.
func (Channel) CallNotEmpty ¶
CallNotEmpty whether there's anyone in the group call or livestream.
func (Channel) CheckUsername ¶ added in v0.152.0
CheckUsername checks whether the given username is available for this channel/supergroup.
func (Channel) DeactivateAllUsernames ¶ added in v0.152.0
DeactivateAllUsernames deactivates all purchased usernames of this channel/supergroup.
func (Channel) DefaultBannedRights ¶
func (c Channel) DefaultBannedRights() (tg.ChatBannedRights, bool)
DefaultBannedRights returns default chat rights.
func (Channel) DeletePhoto ¶ added in v0.152.0
DeletePhoto removes the profile photo of this channel/supergroup.
func (Channel) DisableReactions ¶
DisableReactions disables reactions.
func (Channel) Fake ¶
Fake whether this user/chat/channel was reported by many users as a fake or scam: be careful when interacting with it.
func (Channel) InputChannel ¶
func (c Channel) InputChannel() tg.InputChannelClass
InputChannel returns input user for this user.
func (Channel) InputPeer ¶
func (c Channel) InputPeer() tg.InputPeerClass
InputPeer returns input peer for this peer.
func (Channel) InviteLinks ¶
func (c Channel) InviteLinks() InviteLinks
InviteLinks returns InviteLinks for this peer.
func (Channel) IsBroadcast ¶ added in v0.56.0
IsBroadcast whether this Channel is Broadcast.
func (Channel) IsSupergroup ¶ added in v0.56.0
IsSupergroup whether this Channel is Supergroup.
func (Channel) NoForwards ¶
NoForwards whether that message forwarding from this channel is not allowed.
func (Channel) ParticipantsCount ¶
ParticipantsCount returns count of participants.
func (Channel) RecommendedChannels ¶ added in v0.152.0
func (c Channel) RecommendedChannels(ctx context.Context) (RecommendedChannels, error)
RecommendedChannels returns public channels recommended based on similarities in the subscriber bases of this channel and others.
Example ¶
package main
import (
"context"
"fmt"
"go.uber.org/zap"
"github.com/gotd/log/logzap"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/peers"
)
func main() {
logger := zap.NewExample()
client, err := telegram.ClientFromEnvironment(telegram.Options{
Logger: logzap.New(logger.Named("client")),
})
if err != nil {
panic(err)
}
peerManager := peers.Options{Logger: logzap.New(logger)}.Build(client.API())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := client.Run(ctx, func(ctx context.Context) error {
if err := peerManager.Init(ctx); err != nil {
return err
}
p, err := peerManager.Resolve(ctx, "telegram")
if err != nil {
return err
}
ch, ok := p.(peers.Channel)
if !ok {
return fmt.Errorf("%q is not a channel", "telegram")
}
rec, err := ch.RecommendedChannels(ctx)
if err != nil {
return err
}
// rec.Count is the total number of recommendations available.
// For non-Premium accounts the server returns only a subset, so
// len(rec.Channels) may be smaller than rec.Count.
fmt.Printf("got %d of %d recommended channels\n", len(rec.Channels), rec.Count)
for _, c := range rec.Channels {
fmt.Println(c.VisibleName())
}
return nil
}); err != nil {
panic(err)
}
}
Output:
func (Channel) Restricted ¶
func (c Channel) Restricted() ([]tg.RestrictionReason, bool)
Restricted whether this user/chat/channel is restricted.
func (Channel) SetDescription ¶
SetDescription sets new description for this Chat.
func (Channel) SetPhoto ¶ added in v0.152.0
SetPhoto sets the profile photo of this channel/supergroup.
func (Channel) SetReactions ¶
SetReactions sets list of available reactions.
Empty list disables reactions at all.
func (Channel) SetUsername ¶ added in v0.152.0
SetUsername sets the username of this channel/supergroup.
Pass an empty string to remove the username.
func (Channel) TDLibPeerID ¶
func (c Channel) TDLibPeerID() constant.TDLibPeerID
TDLibPeerID returns TDLibPeerID for this entity.
func (Channel) ToBroadcast ¶
ToBroadcast tries to convert this Channel to Broadcast.
func (Channel) ToSupergroup ¶
func (c Channel) ToSupergroup() (Supergroup, bool)
ToSupergroup tries to convert this Channel to Supergroup.
func (Channel) VisibleName ¶
VisibleName returns visible name of peer.
It returns FirstName + " " + LastName for users, and title for chats and channels.
type Chat ¶
type Chat struct {
// contains filtered or unexported fields
}
Chat is chat peer.
func (Chat) ActualChat ¶
ActualChat returns Channel to which this chat migrated.
Also see MigratedTo.
func (Chat) AdminRights ¶
func (c Chat) AdminRights() (tg.ChatAdminRights, bool)
AdminRights returns admin rights of the user in this channel.
func (Chat) CallActive ¶
CallActive whether a group call or livestream is currently active.
func (Chat) CallNotEmpty ¶
CallNotEmpty whether there's anyone in the group call or livestream.
func (Chat) Deactivated ¶
Deactivated whether the group was migrated.
func (Chat) DefaultBannedRights ¶
func (c Chat) DefaultBannedRights() (tg.ChatBannedRights, bool)
DefaultBannedRights returns default chat rights.
func (Chat) DisableReactions ¶
DisableReactions disables reactions.
func (Chat) Fake ¶
Fake whether this user/chat/channel was reported by many users as a fake or scam: be careful when interacting with it.
func (Chat) InputPeer ¶
func (c Chat) InputPeer() tg.InputPeerClass
InputPeer returns input peer for this peer.
func (Chat) InviteLinks ¶
func (c Chat) InviteLinks() InviteLinks
InviteLinks returns InviteLinks for this peer.
func (Chat) IsBroadcast ¶ added in v0.56.0
IsBroadcast whether this Chat is Broadcast.
func (Chat) IsSupergroup ¶ added in v0.56.0
IsSupergroup whether this Chat is Supergroup.
func (Chat) LeaveAndDelete ¶
LeaveAndDelete leaves this chat and removes the entire chat history of this user in this chat.
func (Chat) MigratedTo ¶
func (c Chat) MigratedTo() (tg.InputChannelClass, bool)
MigratedTo returns a supergroup to which this chat migrated.
func (Chat) NoForwards ¶
NoForwards whether that message forwarding from this channel is not allowed.
func (Chat) ParticipantsCount ¶
ParticipantsCount returns count of participants.
func (Chat) Restricted ¶
func (c Chat) Restricted() ([]tg.RestrictionReason, bool)
Restricted whether this user/chat/channel is restricted.
func (Chat) SetDescription ¶
SetDescription sets new description for this Chat.
func (Chat) SetReactions ¶
SetReactions sets list of available reactions.
Empty list disables reactions at all.
func (Chat) TDLibPeerID ¶
func (c Chat) TDLibPeerID() constant.TDLibPeerID
TDLibPeerID returns TDLibPeerID for this entity.
func (Chat) ToBroadcast ¶
ToBroadcast tries to convert this Chat to Broadcast.
func (Chat) ToSupergroup ¶
func (c Chat) ToSupergroup() (Supergroup, bool)
ToSupergroup tries to convert this Chat to Supergroup.
func (Chat) VisibleName ¶
VisibleName returns visible name of peer.
It returns FirstName + " " + LastName for users, and title for chats and channels.
type ExportLinkOptions ¶
type ExportLinkOptions struct {
// Whether users joining the chat via the link need to be approved by chat administrators.
RequestNeeded bool
// Expiration date.
//
// If zero, will not be used.
ExpireDate time.Time
// Maximum number of users that can join using this link.
//
// If zero, will not be used.
UsageLimit int
// Title of this link.
//
// If zero, will not be used.
Title string
}
ExportLinkOptions is options for ExportNew.
type InmemoryCache ¶
type InmemoryCache struct {
// contains filtered or unexported fields
}
InmemoryCache is basic in-memory Cache implementation.
func (*InmemoryCache) FindChannel ¶
FindChannel implements Cache.
func (*InmemoryCache) FindChannelFull ¶
func (f *InmemoryCache) FindChannelFull(ctx context.Context, id int64) (*tg.ChannelFull, bool, error)
FindChannelFull implements Cache.
func (*InmemoryCache) FindChatFull ¶
FindChatFull implements Cache.
func (*InmemoryCache) FindUserFull ¶
FindUserFull implements Cache.
func (*InmemoryCache) SaveChannelFulls ¶
func (f *InmemoryCache) SaveChannelFulls(ctx context.Context, channels ...*tg.ChannelFull) error
SaveChannelFulls implements Cache.
func (*InmemoryCache) SaveChannels ¶
SaveChannels implements Cache.
func (*InmemoryCache) SaveChatFulls ¶
SaveChatFulls implements Cache.
func (*InmemoryCache) SaveUserFulls ¶
SaveUserFulls implements Cache.
type InmemoryStorage ¶
type InmemoryStorage struct {
// contains filtered or unexported fields
}
InmemoryStorage is basic in-memory Storage implementation.
func (*InmemoryStorage) FindPhone ¶
func (f *InmemoryStorage) FindPhone(ctx context.Context, phone string) (key Key, value Value, found bool, err error)
FindPhone implements Storage.
func (*InmemoryStorage) GetContactsHash ¶
func (f *InmemoryStorage) GetContactsHash(ctx context.Context) (int64, error)
GetContactsHash implements Storage.
func (*InmemoryStorage) SaveContactsHash ¶
func (f *InmemoryStorage) SaveContactsHash(ctx context.Context, hash int64) error
SaveContactsHash implements Storage.
type InviteLink ¶
type InviteLink struct {
// contains filtered or unexported fields
}
InviteLink represents invite link.
func (InviteLink) CreatedAt ¶
func (l InviteLink) CreatedAt() time.Time
CreatedAt returns time when was this chat invite created.
func (InviteLink) Creator ¶
func (l InviteLink) Creator(ctx context.Context) (User, error)
Creator returns link creator.
func (InviteLink) ExpireDate ¶
func (l InviteLink) ExpireDate() (time.Time, bool)
ExpireDate returns time when does this chat invite expire.
func (InviteLink) Permanent ¶
func (l InviteLink) Permanent() bool
Permanent whether this chat invite has no expiration
func (InviteLink) Raw ¶
func (l InviteLink) Raw() *tg.ChatInviteExported
Raw returns raw tg.ChatInviteExported.
func (InviteLink) ReplacedWith ¶
func (l InviteLink) ReplacedWith() (InviteLink, bool)
ReplacedWith returns new InviteLink, if any.
func (InviteLink) RequestNeeded ¶
func (l InviteLink) RequestNeeded() bool
RequestNeeded whether users joining the chat via the link need to be approved by chat administrators.
func (InviteLink) Requested ¶
func (l InviteLink) Requested() (int, bool)
Requested returns number of pending join requests.
func (InviteLink) Revoked ¶
func (l InviteLink) Revoked() bool
Revoked whether this chat invite was revoked
func (InviteLink) StartDate ¶
func (l InviteLink) StartDate() (time.Time, bool)
StartDate returns time when was this chat invite last modified.
func (InviteLink) Usage ¶
func (l InviteLink) Usage() (int, bool)
Usage returns how many users joined using this link.
func (InviteLink) UsageLimit ¶
func (l InviteLink) UsageLimit() (int, bool)
UsageLimit returns maximum number of users that can join using this link.
type InviteLinks ¶
type InviteLinks struct {
// contains filtered or unexported fields
}
InviteLinks represents invite links of Chat or Channel.
func (InviteLinks) AddNew ¶
func (e InviteLinks) AddNew(ctx context.Context, opts ExportLinkOptions) (InviteLink, error)
AddNew creates an additional invite link for a chat.
func (InviteLinks) ApproveJoin ¶
func (e InviteLinks) ApproveJoin(ctx context.Context, user tg.InputUserClass) error
ApproveJoin approves join request for given user.
func (InviteLinks) DeclineJoin ¶
func (e InviteLinks) DeclineJoin(ctx context.Context, user tg.InputUserClass) error
DeclineJoin declines join request for given user.
func (InviteLinks) Delete ¶
func (e InviteLinks) Delete(ctx context.Context, link string) error
Delete deletes invite link.
Not available for bots.
func (InviteLinks) Edit ¶
func (e InviteLinks) Edit(ctx context.Context, link string, opts ExportLinkOptions) (InviteLink, error)
Edit edits link info.
func (InviteLinks) ExportNew ¶
func (e InviteLinks) ExportNew(ctx context.Context, opts ExportLinkOptions) (InviteLink, error)
ExportNew creates new primary invite link for a chat.
Notice: Any previously generated primary link is revoked.
See also AddNew.
func (InviteLinks) Get ¶
func (e InviteLinks) Get(ctx context.Context, link string) (InviteLink, error)
Get returns link info.
func (InviteLinks) Revoke ¶
func (e InviteLinks) Revoke(ctx context.Context, link string) (InviteLink, error)
Revoke revokes invite link and returns revoked link info.
If the primary link is revoked, a new link is automatically generated.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager is peer manager.
NB: this package is completely experimental and still WIP.
Example ¶
package main
import (
"context"
"github.com/go-faster/errors"
"go.uber.org/zap"
"github.com/gotd/log/logzap"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/peers"
"github.com/gotd/td/telegram/updates"
"github.com/gotd/td/tg"
)
func main() {
logger := zap.NewExample()
var (
dispatcher = tg.NewUpdateDispatcher()
h telegram.UpdateHandler
)
client, err := telegram.ClientFromEnvironment(telegram.Options{
Logger: logzap.New(logger.Named("client")),
UpdateHandler: telegram.UpdateHandlerFunc(func(ctx context.Context, u tg.UpdatesClass) error {
return h.Handle(ctx, u)
}),
})
if err != nil {
panic(err)
}
peerManager := peers.Options{
Logger: logzap.New(logger),
}.Build(client.API())
gaps := updates.New(updates.Config{
Handler: dispatcher,
AccessHasher: peerManager,
Logger: logzap.New(logger.Named("gaps")),
})
h = peerManager.UpdateHook(gaps)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := client.Run(ctx, func(ctx context.Context) error {
if err := peerManager.Init(ctx); err != nil {
return err
}
u, err := peerManager.Self(ctx)
if err != nil {
return err
}
_, isBot := u.ToBot()
if err := gaps.Run(ctx, client.API(), u.ID(), updates.AuthOptions{IsBot: isBot}); err != nil {
return errors.Wrap(err, "gaps")
}
return nil
}); err != nil {
panic(err)
}
}
Output:
func (*Manager) FromInputPeer ¶
FromInputPeer gets Peer from tg.InputPeerClass.
func (*Manager) GetChannel ¶
GetChannel gets Channel using given tg.InputChannelClass.
func (*Manager) GetChannelAccessHash ¶
func (m *Manager) GetChannelAccessHash(ctx context.Context, userID, channelID int64) (accessHash int64, found bool, err error)
GetChannelAccessHash implements updates.ChannelAccessHasher.
func (*Manager) ImportInvite ¶
ImportInvite imports given hash invite.
func (*Manager) JoinLink ¶
JoinLink joins to private chat using given link or hash. Input examples:
t.me/+AAAAAAAAAAAAAAAA https://t.me/+AAAAAAAAAAAAAAAA t.me/joinchat/AAAAAAAAAAAAAAAA https://t.me/joinchat/AAAAAAAAAAAAAAAA tg:join?invite=AAAAAAAAAAAAAAAA tg://join?invite=AAAAAAAAAAAAAAAA
func (*Manager) RecommendedChannels ¶ added in v0.152.0
func (m *Manager) RecommendedChannels(ctx context.Context) (RecommendedChannels, error)
RecommendedChannels returns channels recommended for the current user, based on the channels the user has joined.
func (*Manager) Resolve ¶
Resolve uses given string to create new peer promise.
Input examples:
@telegram telegram t.me/telegram https://t.me/telegram tg:resolve?domain=telegram tg://resolve?domain=telegram +13115552368 +1 (311) 555-0123 +1 311 555-6162 13115556162
func (*Manager) ResolveBusinessChat ¶ added in v0.121.0
func (m *Manager) ResolveBusinessChat(ctx context.Context, u string) (p Peer, me MsgAndEntities, err error)
ResolveBusinessChat uses given deeplink to resolve business chat link.
Input examples:
t.me/m/slug https://t.me/m/slug tg:message?slug=slug tg://message?slug=slug
func (*Manager) ResolveChannelID ¶ added in v0.56.0
ResolveChannelID creates Channel using given id.
func (*Manager) ResolveChatID ¶ added in v0.56.0
ResolveChatID creates Chat using given id.
func (*Manager) ResolveDeeplink ¶
ResolveDeeplink uses given deeplink to create new peer promise.
Input examples:
t.me/telegram https://t.me/telegram tg:resolve?domain=telegram tg://resolve?domain=telegram
func (*Manager) ResolveDeeplinkJoin ¶ added in v0.106.0
func (*Manager) ResolveDomain ¶
ResolveDomain uses given domain to create new peer promise.
May be prefixed with @ or not.
Input examples:
@telegram telegram
func (*Manager) ResolvePeer ¶
ResolvePeer creates Peer using given tg.PeerClass.
func (*Manager) ResolvePhone ¶
ResolvePhone uses given phone to resolve User.
Input example:
+13115552368 +1 (311) 555-0123 +1 311 555-6162 13115556162
Note that Telegram represents phone numbers according to the E.164 standard without the plus sign (”+”) prefix. The resolver therefore takes an easy route and just deletes any non-digit symbols from phone number string.
func (*Manager) ResolveTDLibID ¶
func (m *Manager) ResolveTDLibID(ctx context.Context, peerID constant.TDLibPeerID) (p Peer, err error)
ResolveTDLibID creates Peer using given constant.TDLibPeerID.
func (*Manager) ResolveUserID ¶ added in v0.56.0
ResolveUserID creates User using given id.
func (*Manager) SetChannelAccessHash ¶
func (m *Manager) SetChannelAccessHash(ctx context.Context, userID, channelID, accessHash int64) error
SetChannelAccessHash implements updates.ChannelAccessHasher.
func (*Manager) UpdateHook ¶
func (m *Manager) UpdateHook(next telegram.UpdateHandler) telegram.UpdateHandler
UpdateHook returns update middleware hook for collecting entities.
func (*Manager) UserResolveHook ¶
func (m *Manager) UserResolveHook(ctx context.Context) entity.UserResolver
UserResolveHook creates entity.UserResolver attached to this Manager.
type MsgAndEntities ¶ added in v0.121.0
type MsgAndEntities struct {
Msg string
Entities []tg.MessageEntityClass
}
type NoopCache ¶
type NoopCache struct{}
NoopCache is no-op implementation of Cache.
func (NoopCache) FindChannel ¶
FindChannel implements Cache.
func (NoopCache) FindChannelFull ¶
FindChannelFull implements Cache.
func (NoopCache) FindChatFull ¶
FindChatFull implements Cache.
func (NoopCache) FindUserFull ¶
FindUserFull implements Cache.
func (NoopCache) SaveChannelFulls ¶
SaveChannelFulls implements Cache.
func (NoopCache) SaveChannels ¶
SaveChannels implements Cache.
func (NoopCache) SaveChatFulls ¶
SaveChatFulls implements Cache.
func (NoopCache) SaveUserFulls ¶
SaveUserFulls implements Cache.
type Peer ¶
type Peer interface {
// ID returns entity ID.
ID() int64
// TDLibPeerID returns TDLibPeerID for this entity.
TDLibPeerID() constant.TDLibPeerID
// VisibleName returns visible name of peer.
//
// It returns FirstName + " " + LastName for users, and title for chats and channels.
VisibleName() string
// Username returns peer username, if any.
Username() (string, bool)
// Restricted whether this user/chat/channel is restricted.
Restricted() ([]tg.RestrictionReason, bool)
// Verified whether this user/chat/channel is verified by Telegram.
Verified() bool
// Scam whether this user/chat/channel is probably a scam.
Scam() bool
// Fake whether this user/chat/channel was reported by many users as a fake or scam: be
// careful when interacting with it.
Fake() bool
// InputPeer returns input peer for this peer.
InputPeer() tg.InputPeerClass
// Sync updates current object.
Sync(ctx context.Context) error
// Manager returns attached Manager.
Manager() *Manager
// Report reports a peer for violation of telegram's Terms of Service.
Report(ctx context.Context, reason tg.ReportReasonClass, message string) error
// Photo returns peer photo, if any.
Photo(ctx context.Context) (*tg.Photo, bool, error)
}
Peer represents generic peer.
type PeerNotFoundError ¶
PeerNotFoundError is returned when Manager unable to find Peer with given tg.PeerClass.
func (*PeerNotFoundError) Error ¶
func (p *PeerNotFoundError) Error() string
Error implements error.
type PhoneNotFoundError ¶
type PhoneNotFoundError struct {
Phone string
}
PhoneNotFoundError is returned when Manager unable to find contact with given phone.
func (*PhoneNotFoundError) Error ¶
func (c *PhoneNotFoundError) Error() string
Error implements error.
type RecommendedChannels ¶ added in v0.152.0
type RecommendedChannels struct {
// Channels is the list of recommended channels returned by the server.
Channels []Channel
// Count is the total number of recommendations available, which may be
// greater than len(Channels) for non-Premium accounts.
Count int
}
RecommendedChannels is the result of a channel recommendations query.
channels.getChannelRecommendations has no offset or hash parameter, so it can't be paginated: the server returns the available recommendations in a single response. Non-Premium accounts receive a server-capped subset of the recommendations, while Count still reports the full total — so a caller can detect that more recommendations exist (e.g. to prompt for Premium) by comparing len(Channels) with Count.
See https://core.telegram.org/method/channels.getChannelRecommendations.
type SearchResult ¶ added in v0.56.0
SearchResult is Search query result.
type Storage ¶
type Storage interface {
Save(ctx context.Context, key Key, value Value) error
Find(ctx context.Context, key Key) (value Value, found bool, _ error)
SavePhone(ctx context.Context, phone string, key Key) error
FindPhone(ctx context.Context, phone string) (key Key, value Value, found bool, err error)
GetContactsHash(ctx context.Context) (int64, error)
SaveContactsHash(ctx context.Context, hash int64) error
}
Storage is peer storage.
type Supergroup ¶
type Supergroup struct {
Channel
}
Supergroup is a supergroup Channel.
func (Supergroup) DisableSlowMode ¶
func (c Supergroup) DisableSlowMode(ctx context.Context) error
DisableSlowMode disables slow mode.
func (Supergroup) ResetStickerSet ¶
func (c Supergroup) ResetStickerSet(ctx context.Context) error
ResetStickerSet resets associated sticker set of this supergroup.
func (Supergroup) SetStickerSet ¶
func (c Supergroup) SetStickerSet(ctx context.Context, set tg.InputStickerSetClass) error
SetStickerSet associates a sticker set to this supergroup.
func (Supergroup) SlowmodeEnabled ¶
func (c Supergroup) SlowmodeEnabled() bool
SlowmodeEnabled whether slow mode is enabled for groups to prevent flood in chat.
func (Supergroup) ToggleJoinRequest ¶ added in v0.152.0
func (c Supergroup) ToggleJoinRequest(ctx context.Context, enabled bool) error
ToggleJoinRequest toggles whether users joining this supergroup must be explicitly approved by an administrator.
func (Supergroup) ToggleJoinToSend ¶ added in v0.152.0
func (c Supergroup) ToggleJoinToSend(ctx context.Context, enabled bool) error
ToggleJoinToSend toggles whether all users should join this supergroup (discussion group) before they are allowed to send messages.
func (Supergroup) TogglePreHistoryHidden ¶ added in v0.152.0
func (c Supergroup) TogglePreHistoryHidden(ctx context.Context, enabled bool) error
TogglePreHistoryHidden hides or shows the previous message history for new members of this supergroup.
If enabled is set, chat history is hidden for new members.
func (Supergroup) ToggleSlowMode ¶
func (c Supergroup) ToggleSlowMode(ctx context.Context, seconds int) error
ToggleSlowMode Toggle supergroup slow mode: if enabled, users will only be able to send one message per seconds.
If seconds is zero or smaller, slow mode will be disabled.
type User ¶
type User struct {
// contains filtered or unexported fields
}
User is user peer.
func (User) Fake ¶
Fake whether this user/chat/channel was reported by many users as a fake or scam: be careful when interacting with it.
func (User) InputPeer ¶
func (u User) InputPeer() tg.InputPeerClass
InputPeer returns input peer for this peer.
func (User) InputUser ¶
func (u User) InputUser() tg.InputUserClass
InputUser returns input user for this user.
func (User) MutualContact ¶
MutualContact whether this user is a mutual contact.
func (User) ReportSpam ¶
ReportSpam reports a new incoming chat for spam, if the peer settings of the chat allow us to do that.
func (User) Restricted ¶
func (u User) Restricted() ([]tg.RestrictionReason, bool)
Restricted whether this user/chat/channel is restricted.
func (User) Status ¶
func (u User) Status() (tg.UserStatusClass, bool)
Status returns user status, if any.
func (User) TDLibPeerID ¶
func (u User) TDLibPeerID() constant.TDLibPeerID
TDLibPeerID returns TDLibPeerID for this entity.
func (User) VisibleName ¶
VisibleName returns visible name of peer.
It returns FirstName + " " + LastName for users, and title for chats and channels.
Source Files
¶
- admin_log.go
- apply.go
- atomic.go
- bot.go
- broadcast.go
- channel.go
- chat.go
- errors.go
- id.go
- integration.go
- invite_link.go
- invite_links.go
- join.go
- manager.go
- memory_storage.go
- msg_and_entities.go
- multichat.go
- options.go
- peerid_map.go
- peers.go
- query.go
- query_full.go
- recommendations.go
- resolve.go
- search.go
- self.go
- storage.go
- supergroup.go
- update.go
- user.go