@@ -15,13 +15,13 @@ func (b *Bot) installBusinessHandlers() {
1515
1616 return nil
1717 })
18- b .disp .OnBotNewBusinessMessage (func (ctx context.Context , _ tg.Entities , u * tg.UpdateBotNewBusinessMessage ) error {
19- b .dispatchBusinessMessage (ctx , u .ConnectionID , u .Message , false )
18+ b .disp .OnBotNewBusinessMessage (func (ctx context.Context , e tg.Entities , u * tg.UpdateBotNewBusinessMessage ) error {
19+ b .dispatchBusinessMessage (ctx , e , u .ConnectionID , u .Message , false )
2020
2121 return nil
2222 })
23- b .disp .OnBotEditBusinessMessage (func (ctx context.Context , _ tg.Entities , u * tg.UpdateBotEditBusinessMessage ) error {
24- b .dispatchBusinessMessage (ctx , u .ConnectionID , u .Message , true )
23+ b .disp .OnBotEditBusinessMessage (func (ctx context.Context , e tg.Entities , u * tg.UpdateBotEditBusinessMessage ) error {
24+ b .dispatchBusinessMessage (ctx , e , u .ConnectionID , u .Message , true )
2525
2626 return nil
2727 })
@@ -47,7 +47,7 @@ func (b *Bot) installBusinessHandlers() {
4747// and routes it as a (edited) business message. Unlike dispatchMessage it keeps
4848// outgoing messages: the Bot API delivers the whole business conversation,
4949// including messages the account itself sent.
50- func (b * Bot ) dispatchBusinessMessage (ctx context.Context , connectionID string , msg tg.MessageClass , edited bool ) {
50+ func (b * Bot ) dispatchBusinessMessage (ctx context.Context , e tg. Entities , connectionID string , msg tg.MessageClass , edited bool ) {
5151 m , err := b .messageFromTg (ctx , msg )
5252 if err != nil {
5353 b .logger ().Error (ctx , "Convert business message" , log .Error (err ))
@@ -60,6 +60,9 @@ func (b *Bot) dispatchBusinessMessage(ctx context.Context, connectionID string,
6060 }
6161
6262 m .BusinessConnectionID = connectionID
63+ if m .raw != nil {
64+ m .businessPeer = inputPeerFromEntities (m .raw .PeerID , e )
65+ }
6366
6467 u := & Update {}
6568 if edited {
@@ -71,6 +74,41 @@ func (b *Bot) dispatchBusinessMessage(ctx context.Context, connectionID string,
7174 b .route (ctx , u )
7275}
7376
77+ // inputPeerFromEntities builds the input peer for a message's chat from the
78+ // access hashes delivered with the update, so a business send addresses the chat
79+ // with the account's own access hash. Returns nil when the peer is absent.
80+ func inputPeerFromEntities (peer tg.PeerClass , e tg.Entities ) tg.InputPeerClass {
81+ switch p := peer .(type ) {
82+ case * tg.PeerUser :
83+ if u , ok := e .Users [p .UserID ]; ok {
84+ return & tg.InputPeerUser {UserID : p .UserID , AccessHash : u .AccessHash }
85+ }
86+ case * tg.PeerChannel :
87+ if ch , ok := e .Channels [p .ChannelID ]; ok {
88+ return & tg.InputPeerChannel {ChannelID : p .ChannelID , AccessHash : ch .AccessHash }
89+ }
90+ case * tg.PeerChat :
91+ return & tg.InputPeerChat {ChatID : p .ChatID }
92+ }
93+
94+ return nil
95+ }
96+
97+ // businessChatID returns a send target for a business message's chat that carries
98+ // the account-scoped access hash, bypassing the bot's peer store.
99+ func businessChatID (m * Message ) (ChatID , bool ) {
100+ if m == nil || m .businessPeer == nil {
101+ return nil , false
102+ }
103+
104+ ref , err := peerRefFromInputPeer (m .businessPeer )
105+ if err != nil {
106+ return nil , false
107+ }
108+
109+ return Peer (ref ), true
110+ }
111+
74112// businessConnectionID returns the connection id the update belongs to, or empty
75113// when the update is not a business update.
76114func (u * Update ) businessConnectionID () string {
@@ -132,6 +170,14 @@ func (c *Context) BusinessMessage() *Message {
132170 }
133171}
134172
173+ // BusinessChat returns a send target for the business message's chat that
174+ // carries the account-scoped access hash, and whether the update is a business
175+ // message. Use it to address sends on behalf of the connected account; the bot's
176+ // stored access hash is invalid in the business context.
177+ func (c * Context ) BusinessChat () (ChatID , bool ) {
178+ return businessChatID (c .BusinessMessage ())
179+ }
180+
135181// Business returns a BusinessContext scoped to the update's business connection,
136182// and whether the update belongs to one. Use it to act on behalf of the
137183// connected account from within a business update handler.
0 commit comments