Skip to content

Commit 66f538a

Browse files
committed
feat(bot): display recommended offers for auto-buy package selection
feat(bot): allow selecting recommended offers for auto-buy package refactor(bot): update kbAutoPackage to accept recommended offers as argument fix(bot): add loading message when fetching recommended offers
1 parent 1e4dec2 commit 66f538a

3 files changed

Lines changed: 35 additions & 11 deletions

File tree

bot/autobuy.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ func (h *Handler) cbSetAutoThreshold(b *gotgbot.Bot, chatID, msgID, userID int64
6565
threshStr = "Habis (0 MB)"
6666
}
6767

68-
kb := kbAutoPackage()
68+
h.editMsg(b, chatID, msgID, "⏳ Mengambil rekomendasi paket...", nil)
69+
70+
apiCtx := context.Background()
71+
offers, _ := h.api.GetRecommendedOffers(apiCtx, session)
72+
73+
kb := kbAutoPackage(offers)
6974
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)
7075
}
7176

bot/handler.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ func (h *Handler) handleCallback(b *gotgbot.Bot, ctx *ext.Context) error {
197197
case data == "auto_pkg_ilmupedia":
198198
h.cbSetAutoPackage(b, chatID, msgID, userID, "ilmupedia")
199199

200+
case strings.HasPrefix(data, "auto_pkg_offer_"):
201+
offerID := strings.TrimPrefix(data, "auto_pkg_offer_")
202+
h.cbSetAutoPackage(b, chatID, msgID, userID, offerID)
203+
200204
case data == "auto_pkg_custom":
201205
h.editMsg(b, chatID, msgID, "🆔 Kirim Offer ID paket untuk auto-buy.\n\nContoh: `0fc00fd41bcd26376d806925d746705e`", nil)
202206
session := h.sessions.Get(userID)
@@ -286,7 +290,9 @@ func (h *Handler) handleMessage(b *gotgbot.Bot, ctx *ext.Context) error {
286290
threshStr = "Habis (0 MB)"
287291
}
288292

289-
kb := kbAutoPackage()
293+
apiCtx := context.Background()
294+
offers, _ := h.api.GetRecommendedOffers(apiCtx, session)
295+
kb := kbAutoPackage(offers)
290296
_, err := ctx.EffectiveMessage.Reply(b, fmt.Sprintf("✅ Interval: *%d menit*\n📉 Batas Kuota: *%s*\n\n📦 Pilih paket untuk auto-buy:", session.AutoBuyInterval, threshStr), &gotgbot.SendMessageOpts{
291297
ParseMode: "Markdown",
292298
ReplyMarkup: kb,

bot/keyboards.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,29 @@ func kbAutoThreshold() gotgbot.InlineKeyboardMarkup {
117117
}
118118
}
119119

120-
func kbAutoPackage() gotgbot.InlineKeyboardMarkup {
121-
return gotgbot.InlineKeyboardMarkup{
122-
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
123-
{
124-
{Text: "📚 Ilmupedia", CallbackData: "auto_pkg_ilmupedia"},
125-
{Text: "🆔 Custom Id", CallbackData: "auto_pkg_custom"},
126-
},
127-
{{Text: "🔙 Kembali", CallbackData: "auto_buy"}},
128-
},
120+
func kbAutoPackage(offers []telkomsel.RecommendedOffer) gotgbot.InlineKeyboardMarkup {
121+
var rows [][]gotgbot.InlineKeyboardButton
122+
123+
for _, o := range offers {
124+
label := fmt.Sprintf("📦 %s - Rp%s", o.Name, o.Price)
125+
if len(label) > 64 {
126+
label = label[:61] + "..."
127+
}
128+
rows = append(rows, []gotgbot.InlineKeyboardButton{
129+
{Text: label, CallbackData: "auto_pkg_offer_" + o.ID},
130+
})
129131
}
132+
133+
rows = append(rows,
134+
[]gotgbot.InlineKeyboardButton{
135+
{Text: "🆔 Custom Id", CallbackData: "auto_pkg_custom"},
136+
},
137+
[]gotgbot.InlineKeyboardButton{
138+
{Text: "🔙 Kembali", CallbackData: "auto_buy"},
139+
},
140+
)
141+
142+
return gotgbot.InlineKeyboardMarkup{InlineKeyboard: rows}
130143
}
131144

132145
func kbAutoPay() gotgbot.InlineKeyboardMarkup {

0 commit comments

Comments
 (0)