@@ -11,6 +11,7 @@ import (
1111 "github.com/PaulSonOfLars/gotgbot/v2"
1212
1313 "telkomsel-bot/telkomsel"
14+ "telkomsel-bot/util"
1415)
1516
1617func (h * Handler ) cbShowAutoMonitor (b * gotgbot.Bot , chatID , msgID , userID int64 ) {
@@ -21,9 +22,13 @@ func (h *Handler) cbShowAutoMonitor(b *gotgbot.Bot, chatID, msgID, userID int64)
2122
2223 if session .AutoBuyActive {
2324 kb := kbAutoRunning ()
25+ threshStr := fmt .Sprintf ("< %d MB" , session .AutoBuyThreshold )
26+ if session .AutoBuyThreshold == 0 {
27+ threshStr = "Habis (0 MB)"
28+ }
2429 h .editMsg (b , chatID , msgID , fmt .Sprintf (
25- "🤖 *Auto-Buy Sedang Aktif!*\n \n ⏱ Interval: *%d menit*\n 📦 Paket: *%s*\n 💳 Bayar: *Pulsa*\n \n Monitor berjalan di background..." ,
26- session .AutoBuyInterval , session .AutoBuyPackage ,
30+ "🤖 *Auto-Buy Sedang Aktif!*\n \n ⏱ Interval: *%d menit*\n 📉 Batas Kuota: *%s* \n 📦 Paket: *%s*\n 💳 Bayar: *Pulsa*\n \n Monitor berjalan di background..." ,
31+ session .AutoBuyInterval , threshStr , session .AutoBuyPackage ,
2732 ), & kb )
2833 return
2934 }
@@ -41,8 +46,26 @@ func (h *Handler) cbSetAutoInterval(b *gotgbot.Bot, chatID, msgID, userID int64,
4146 session .AutoBuyInterval = minutes
4247 h .sessions .Set (userID , session )
4348
49+ kb := kbAutoThreshold ()
50+ h .editMsg (b , chatID , msgID , fmt .Sprintf ("✅ Interval: *%d menit*\n \n 📉 Pilih batas minimum kuota untuk auto-buy:" , minutes ), & kb )
51+ }
52+
53+ func (h * Handler ) cbSetAutoThreshold (b * gotgbot.Bot , chatID , msgID , userID int64 , threshold int ) {
54+ session , ok := h .checkSession (b , chatID , msgID , userID )
55+ if ! ok {
56+ return
57+ }
58+
59+ session .AutoBuyThreshold = threshold
60+ h .sessions .Set (userID , session )
61+
62+ threshStr := fmt .Sprintf ("< %d MB" , threshold )
63+ if threshold == 0 {
64+ threshStr = "Habis (0 MB)"
65+ }
66+
4467 kb := kbAutoPackage ()
45- h .editMsg (b , chatID , msgID , fmt .Sprintf ("✅ Interval: *%d menit*\n \n 📦 Pilih paket untuk auto-buy:" , minutes ), & kb )
68+ h .editMsg (b , chatID , msgID , fmt .Sprintf ("✅ Interval: *%d menit*\n 📉 Batas Kuota: *%s* \n \n 📦 Pilih paket untuk auto-buy:" , session . AutoBuyInterval , threshStr ), & kb )
4669}
4770
4871func (h * Handler ) cbSetAutoPackage (b * gotgbot.Bot , chatID , msgID , userID int64 , pkg string ) {
@@ -54,8 +77,13 @@ func (h *Handler) cbSetAutoPackage(b *gotgbot.Bot, chatID, msgID, userID int64,
5477 session .AutoBuyPackage = pkg
5578 h .sessions .Set (userID , session )
5679
80+ threshStr := fmt .Sprintf ("< %d MB" , session .AutoBuyThreshold )
81+ if session .AutoBuyThreshold == 0 {
82+ threshStr = "Habis (0 MB)"
83+ }
84+
5785 kb := kbAutoPay ()
58- h .editMsg (b , chatID , msgID , fmt .Sprintf ("✅ Interval: *%d menit*\n 📦 Paket: *%s*\n \n 💳 Pembayaran via:" , session .AutoBuyInterval , pkg ), & kb )
86+ h .editMsg (b , chatID , msgID , fmt .Sprintf ("✅ Interval: *%d menit*\n 📉 Batas Kuota: *%s* \n 📦 Paket: *%s*\n \n 💳 Pembayaran via:" , session .AutoBuyInterval , threshStr , pkg ), & kb )
5987}
6088
6189func (h * Handler ) cbStartAutoBuy (b * gotgbot.Bot , chatID , msgID , userID int64 ) {
@@ -81,10 +109,15 @@ func (h *Handler) cbStartAutoBuy(b *gotgbot.Bot, chatID, msgID, userID int64) {
81109 h .autoStops [userID ] = cancel
82110 h .autoStopsMu .Unlock ()
83111
112+ threshStr := fmt .Sprintf ("< %d MB" , session .AutoBuyThreshold )
113+ if session .AutoBuyThreshold == 0 {
114+ threshStr = "Habis (0 MB)"
115+ }
116+
84117 kb := kbAutoRunning ()
85118 h .editMsg (b , chatID , msgID , fmt .Sprintf (
86- "🤖 *Auto-Buy Aktif!*\n \n ⏱ Interval: *%d menit*\n 📦 Paket: *%s*\n 💳 Bayar: *Pulsa*\n \n Monitor berjalan di background..." ,
87- session .AutoBuyInterval , session .AutoBuyPackage ,
119+ "🤖 *Auto-Buy Aktif!*\n \n ⏱ Interval: *%d menit*\n 📉 Batas Kuota: *%s* \n 📦 Paket: *%s*\n 💳 Bayar: *Pulsa*\n \n Monitor berjalan di background..." ,
120+ session .AutoBuyInterval , threshStr , session .AutoBuyPackage ,
88121 ), & kb )
89122
90123 go h .runAutoBuyMonitor (autCtx , b , chatID , userID )
@@ -157,10 +190,26 @@ func (h *Handler) runAutoBuyMonitor(ctx context.Context, b *gotgbot.Bot, chatID,
157190 }
158191
159192 needsBuy := false
160- for _ , group := range quota .Groups {
161- if strings .EqualFold (group .Class , "Internet" ) && len (group .Items ) == 0 {
193+ if session .AutoBuyThreshold == 0 {
194+ for _ , group := range quota .Groups {
195+ if strings .EqualFold (group .Class , "Internet" ) && len (group .Items ) == 0 {
196+ needsBuy = true
197+ break
198+ }
199+ }
200+ } else {
201+ var totalInternetQuota float64
202+ hasInternetGroup := false
203+ for _ , group := range quota .Groups {
204+ if strings .EqualFold (group .Class , "Internet" ) {
205+ hasInternetGroup = true
206+ for _ , item := range group .Items {
207+ totalInternetQuota += util .ParseQuotaToMB (item .Remaining )
208+ }
209+ }
210+ }
211+ if ! hasInternetGroup || totalInternetQuota <= float64 (session .AutoBuyThreshold ) {
162212 needsBuy = true
163- break
164213 }
165214 }
166215
0 commit comments