|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/gotd/td/constant" |
| 8 | + "github.com/gotd/td/tg" |
| 9 | +) |
| 10 | + |
| 11 | +// Peer kinds for a PeerRef. |
| 12 | +const ( |
| 13 | + peerKindUser = "user" |
| 14 | + peerKindChat = "chat" |
| 15 | + peerKindChannel = "channel" |
| 16 | +) |
| 17 | + |
| 18 | +// PeerRef is a serializable reference to a chat that carries its access hash. |
| 19 | +// |
| 20 | +// Sending to a chat requires its MTProto access hash. The bot harvests and |
| 21 | +// persists access hashes for peers it has seen, but addressing a chat after a |
| 22 | +// restart otherwise depends on that stored peer data. A PeerRef captures the |
| 23 | +// id and access hash in a self-contained, JSON-serializable form: persist it |
| 24 | +// yourself (in a DB, a file, …) and, after a restart, send to it with Peer — |
| 25 | +// directly from the reference, no re-resolution. |
| 26 | +// |
| 27 | +// ref, _ := bot.PeerRef(ctx, botapi.ID(chatID)) // resolve once, capture the hash |
| 28 | +// data, _ := json.Marshal(ref) // persist it |
| 29 | +// // … restart … |
| 30 | +// var ref botapi.PeerRef |
| 31 | +// _ = json.Unmarshal(data, &ref) |
| 32 | +// bot.SendMessage(ctx, botapi.Peer(ref), "still works") |
| 33 | +type PeerRef struct { |
| 34 | + Kind string `json:"kind"` // "user", "chat" or "channel" |
| 35 | + ID int64 `json:"id"` |
| 36 | + AccessHash int64 `json:"access_hash,omitempty"` |
| 37 | +} |
| 38 | + |
| 39 | +// PeerRef resolves a chat to a serializable reference including its access hash. |
| 40 | +func (b *Bot) PeerRef(ctx context.Context, chat ChatID) (PeerRef, error) { |
| 41 | + peer, err := b.resolveInputPeer(ctx, chat) |
| 42 | + if err != nil { |
| 43 | + return PeerRef{}, err |
| 44 | + } |
| 45 | + return peerRefFromInputPeer(peer) |
| 46 | +} |
| 47 | + |
| 48 | +// peerRefFromInputPeer extracts a PeerRef from a resolved input peer. |
| 49 | +func peerRefFromInputPeer(p tg.InputPeerClass) (PeerRef, error) { |
| 50 | + switch v := p.(type) { |
| 51 | + case *tg.InputPeerUser: |
| 52 | + return PeerRef{Kind: peerKindUser, ID: v.UserID, AccessHash: v.AccessHash}, nil |
| 53 | + case *tg.InputPeerChannel: |
| 54 | + return PeerRef{Kind: peerKindChannel, ID: v.ChannelID, AccessHash: v.AccessHash}, nil |
| 55 | + case *tg.InputPeerChat: |
| 56 | + return PeerRef{Kind: peerKindChat, ID: v.ChatID}, nil |
| 57 | + default: |
| 58 | + return PeerRef{}, &Error{Code: 400, Description: "Bad Request: chat can't be referenced"} |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// inputPeer reconstructs the MTProto input peer from the reference. |
| 63 | +func (r PeerRef) inputPeer() (tg.InputPeerClass, error) { |
| 64 | + switch r.Kind { |
| 65 | + case peerKindUser: |
| 66 | + return &tg.InputPeerUser{UserID: r.ID, AccessHash: r.AccessHash}, nil |
| 67 | + case peerKindChannel: |
| 68 | + return &tg.InputPeerChannel{ChannelID: r.ID, AccessHash: r.AccessHash}, nil |
| 69 | + case peerKindChat: |
| 70 | + return &tg.InputPeerChat{ChatID: r.ID}, nil |
| 71 | + default: |
| 72 | + return nil, &Error{Code: 400, Description: "Bad Request: invalid peer reference"} |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// tdlibID returns the TDLib (Bot API) chat id for the reference. |
| 77 | +func (r PeerRef) tdlibID() int64 { |
| 78 | + var id constant.TDLibPeerID |
| 79 | + switch r.Kind { |
| 80 | + case peerKindUser: |
| 81 | + id.User(r.ID) |
| 82 | + case peerKindChat: |
| 83 | + id.Chat(r.ID) |
| 84 | + case peerKindChannel: |
| 85 | + id.Channel(r.ID) |
| 86 | + } |
| 87 | + return int64(id) |
| 88 | +} |
| 89 | + |
| 90 | +// chatIDRef adapts a PeerRef to the ChatID union. |
| 91 | +type chatIDRef struct{ ref PeerRef } |
| 92 | + |
| 93 | +func (chatIDRef) isChatID() {} |
| 94 | + |
| 95 | +// MarshalJSON encodes the reference as its bare Bot API chat id. |
| 96 | +func (c chatIDRef) MarshalJSON() ([]byte, error) { |
| 97 | + return []byte(strconv.FormatInt(c.ref.tdlibID(), 10)), nil |
| 98 | +} |
| 99 | + |
| 100 | +// Peer targets a chat by a serializable reference captured with Bot.PeerRef. |
| 101 | +// The send is addressed directly from the reference's access hash — no stored |
| 102 | +// peer data and no re-resolution — so it works across restarts. |
| 103 | +func Peer(ref PeerRef) ChatID { return chatIDRef{ref} } |
0 commit comments