Skip to content

Commit 5ff87ae

Browse files
ernadoclaude
andcommitted
feat: add GetMyStarBalance
GetMyStarBalance (payments.getStarsStatus) returning the bot's Telegram Stars balance as a StarAmount. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent bd030d9 commit 5ff87ae

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

star_balance.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package botapi
2+
3+
import (
4+
"context"
5+
6+
"github.com/gotd/td/tg"
7+
)
8+
9+
// StarAmount describes an amount of Telegram Stars.
10+
type StarAmount struct {
11+
// Amount is the integer number of Telegram Stars, rounded to 0; can be
12+
// negative.
13+
Amount int `json:"amount"`
14+
// NanostarAmount is the number of 1/1000000000 shares of Telegram Stars,
15+
// rounded to 0; can be negative.
16+
NanostarAmount int `json:"nanostar_amount,omitempty"`
17+
}
18+
19+
// GetMyStarBalance returns the current Telegram Stars balance of the bot.
20+
func (b *Bot) GetMyStarBalance(ctx context.Context) (StarAmount, error) {
21+
res, err := b.raw.PaymentsGetStarsStatus(ctx, &tg.PaymentsGetStarsStatusRequest{
22+
Peer: &tg.InputPeerSelf{},
23+
})
24+
if err != nil {
25+
return StarAmount{}, asAPIError(err)
26+
}
27+
28+
amount, ok := res.Balance.(*tg.StarsAmount)
29+
if !ok {
30+
return StarAmount{}, &Error{Code: 500, Description: "Internal Server Error: unexpected stars balance"}
31+
}
32+
33+
return StarAmount{Amount: int(amount.Amount), NanostarAmount: amount.Nanos}, nil
34+
}

star_balance_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package botapi
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/gotd/td/tg"
8+
)
9+
10+
func TestGetMyStarBalance(t *testing.T) {
11+
inv := newMockInvoker()
12+
inv.reply(tg.PaymentsGetStarsStatusRequestTypeID, &tg.PaymentsStarsStatus{
13+
Balance: &tg.StarsAmount{Amount: 1500, Nanos: 250},
14+
})
15+
16+
got, err := newMockBot(inv).GetMyStarBalance(context.Background())
17+
if err != nil {
18+
t.Fatalf("GetMyStarBalance: %v", err)
19+
}
20+
21+
if got.Amount != 1500 || got.NanostarAmount != 250 {
22+
t.Fatalf("balance = %#v", got)
23+
}
24+
25+
var req tg.PaymentsGetStarsStatusRequest
26+
27+
inv.decode(t, tg.PaymentsGetStarsStatusRequestTypeID, &req)
28+
29+
if _, ok := req.Peer.(*tg.InputPeerSelf); !ok {
30+
t.Fatalf("peer = %#v, want self", req.Peer)
31+
}
32+
}

0 commit comments

Comments
 (0)