Skip to content

Commit 916deea

Browse files
ernadoclaude
andcommitted
feat: add GetStarTransactions
Returns the bot's Telegram Stars transaction history via payments.getStarsTransactions (peer inputPeerSelf). The integer offset is stringified into the MTProto pagination token, matching the official server. Each tg.StarsTransaction maps to a Bot API StarTransaction: the sign of the raw amount selects source (incoming) vs receiver (outgoing), and the raw StarsTransactionPeer plus flags map onto the TransactionPartner sealed union (User/Chat/Fragment/TelegramAds/TelegramApi/AffiliateProgram/Other), with Fragment carrying a RevenueWithdrawalState union. These unions are outbound-only and rely on plain encoding/json struct tags, matching ChatBoostSource. Adds chatsByID/chatFromRaw helpers to build a Bot API Chat from raw chat/channel peers offline. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 4ca7b12 commit 916deea

3 files changed

Lines changed: 574 additions & 1 deletion

File tree

convert_member.go

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package botapi
22

3-
import "github.com/gotd/td/tg"
3+
import (
4+
"github.com/gotd/td/constant"
5+
"github.com/gotd/td/tg"
6+
)
47

58
// chatMemberFromParticipant converts an MTProto channel participant into a Bot
69
// API ChatMember. users maps user id to the resolved tg.User harvested from the
@@ -104,3 +107,75 @@ func usersByID(users []tg.UserClass) map[int64]*tg.User {
104107

105108
return m
106109
}
110+
111+
// chatsByID indexes resolved tg.Chat/tg.Channel values from an MTProto response
112+
// by their raw (unmarked) id.
113+
func chatsByID(chats []tg.ChatClass) map[int64]tg.ChatClass {
114+
m := make(map[int64]tg.ChatClass, len(chats))
115+
for _, c := range chats {
116+
switch c := c.(type) {
117+
case *tg.Chat:
118+
m[c.ID] = c
119+
case *tg.ChatForbidden:
120+
m[c.ID] = c
121+
case *tg.Channel:
122+
m[c.ID] = c
123+
case *tg.ChannelForbidden:
124+
m[c.ID] = c
125+
}
126+
}
127+
128+
return m
129+
}
130+
131+
// chatFromRaw builds a Bot API Chat from a raw chat/channel peer, enriching it
132+
// from the resolved chats map when the peer is present. It works offline: when
133+
// the chat is absent only the marked id and kind are filled.
134+
func chatFromRaw(p tg.PeerClass, chats map[int64]tg.ChatClass) Chat {
135+
var id constant.TDLibPeerID
136+
137+
switch p := p.(type) {
138+
case *tg.PeerChat:
139+
id.Chat(p.ChatID)
140+
141+
c := Chat{ID: int64(id), Type: ChatTypeGroup}
142+
143+
switch raw := chats[p.ChatID].(type) {
144+
case *tg.Chat:
145+
c.Title = raw.Title
146+
case *tg.ChatForbidden:
147+
c.Title = raw.Title
148+
}
149+
150+
return c
151+
case *tg.PeerChannel:
152+
id.Channel(p.ChannelID)
153+
154+
c := Chat{ID: int64(id), Type: ChatTypeSupergroup}
155+
156+
switch raw := chats[p.ChannelID].(type) {
157+
case *tg.Channel:
158+
c.Title = raw.Title
159+
c.Username = raw.Username
160+
c.IsForum = raw.Forum
161+
c.Type = channelChatType(raw.Broadcast)
162+
case *tg.ChannelForbidden:
163+
c.Title = raw.Title
164+
c.Type = channelChatType(raw.Broadcast)
165+
}
166+
167+
return c
168+
default:
169+
return Chat{}
170+
}
171+
}
172+
173+
// channelChatType reports the Bot API chat type for a channel by its broadcast
174+
// flag: broadcast channels are "channel", supergroups are "supergroup".
175+
func channelChatType(broadcast bool) ChatType {
176+
if broadcast {
177+
return ChatTypeChannel
178+
}
179+
180+
return ChatTypeSupergroup
181+
}

0 commit comments

Comments
 (0)