buttons.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package telegram
  2. import (
  3. "app/commons/core"
  4. "app/commons/services"
  5. "fmt"
  6. tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
  7. "strings"
  8. )
  9. // handleActionCallback 处理按钮面板的回调(action_ 前缀)
  10. func handleActionCallback(callback *tgbotapi.CallbackQuery) {
  11. action := strings.TrimPrefix(callback.Data, "action_")
  12. switch action {
  13. case "register":
  14. handleRegisterAction(callback)
  15. case "balance":
  16. handleBalanceAction(callback)
  17. case "records":
  18. handleRecordsAction(callback)
  19. case "help":
  20. handleHelpAction(callback)
  21. default:
  22. answerCallback(callback.ID, "未知操作")
  23. }
  24. }
  25. // handleRegisterAction 一键注册+绑定
  26. func handleRegisterAction(callback *tgbotapi.CallbackQuery) {
  27. telegramId := callback.From.ID
  28. username := callback.From.UserName
  29. firstName := callback.From.FirstName
  30. platformService := &services.TgPlatformService{}
  31. userId, isNew, err := platformService.RegisterAndBind(telegramId, username, firstName)
  32. if err != nil {
  33. answerCallback(callback.ID, fmt.Sprintf("⚠️ %s", err.Error()))
  34. core.Log.Errorf("一键注册失败 - telegramId: %d, error: %v", telegramId, err)
  35. return
  36. }
  37. if isNew {
  38. answerCallback(callback.ID, "✅ 注册成功!已自动绑定,可以抢红包了!")
  39. // 私信发送详细信息
  40. text := fmt.Sprintf("🎉 注册成功!\n\n"+
  41. "✅ 平台账户已创建并自动绑定\n"+
  42. "🆔 用户ID: %d\n\n"+
  43. "现在可以直接在群里抢红包了!", userId)
  44. sendTextMessage(telegramId, text)
  45. } else {
  46. // 用户已存在(之前通过OAuth注册但未在bot绑定),现在已自动绑定
  47. answerCallback(callback.ID, "✅ 已绑定!可以抢红包了!")
  48. }
  49. core.Log.Infof("一键注册 - 用户: %s, telegramId: %d, userId: %d, isNew: %v",
  50. username, telegramId, userId, isNew)
  51. }
  52. // handleBalanceAction 查询余额
  53. func handleBalanceAction(callback *tgbotapi.CallbackQuery) {
  54. telegramId := callback.From.ID
  55. username := callback.From.UserName
  56. // 检查绑定状态
  57. bindService := &services.TgBindService{}
  58. bound, _ := bindService.IsUserBound(telegramId, username)
  59. if !bound {
  60. answerCallback(callback.ID, "⚠️ 请先点击「一键注册」绑定账户")
  61. return
  62. }
  63. // 查询本地抢红包总额
  64. platformService := &services.TgPlatformService{}
  65. records, err := platformService.GetUserGrabRecords(telegramId, 0)
  66. if err != nil {
  67. answerCallback(callback.ID, "⚠️ 查询失败,请稍后再试")
  68. return
  69. }
  70. // 统计抢红包总额和次数
  71. var totalAmount float64
  72. for _, r := range records {
  73. totalAmount += r.Amount.InexactFloat64()
  74. }
  75. text := fmt.Sprintf("💰 账户信息\n\n"+
  76. "🧧 抢红包次数: %d 次\n"+
  77. "💵 抢红包总额: %.2f\n\n"+
  78. "💡 详细余额请登录平台查看",
  79. len(records), totalAmount)
  80. sendTextMessage(telegramId, text)
  81. answerCallback(callback.ID, "💰 已发送到私聊")
  82. }
  83. // handleRecordsAction 查询抢红包记录
  84. func handleRecordsAction(callback *tgbotapi.CallbackQuery) {
  85. telegramId := callback.From.ID
  86. username := callback.From.UserName
  87. // 检查绑定状态
  88. bindService := &services.TgBindService{}
  89. bound, _ := bindService.IsUserBound(telegramId, username)
  90. if !bound {
  91. answerCallback(callback.ID, "⚠️ 请先点击「一键注册」绑定账户")
  92. return
  93. }
  94. // 查询最近10条记录
  95. platformService := &services.TgPlatformService{}
  96. records, err := platformService.GetUserGrabRecords(telegramId, 10)
  97. if err != nil {
  98. answerCallback(callback.ID, "⚠️ 查询失败,请稍后再试")
  99. return
  100. }
  101. if len(records) == 0 {
  102. sendTextMessage(telegramId, "📊 暂无抢红包记录\n\n等待群内红包发放后即可参与!")
  103. answerCallback(callback.ID, "📊 已发送到私聊")
  104. return
  105. }
  106. text := "📊 最近抢红包记录\n\n"
  107. for i, r := range records {
  108. timeStr := r.GrabbedAt.Format("01-02 15:04")
  109. bestTag := ""
  110. if r.IsBest == 1 {
  111. bestTag = " 🏆"
  112. }
  113. text += fmt.Sprintf("%d. %s | %.2f%s\n", i+1, timeStr, r.Amount.InexactFloat64(), bestTag)
  114. }
  115. text += "\n💡 🏆 = 手气最佳"
  116. sendTextMessage(telegramId, text)
  117. answerCallback(callback.ID, "📊 已发送到私聊")
  118. }
  119. // handleHelpAction 帮助信息
  120. func handleHelpAction(callback *tgbotapi.CallbackQuery) {
  121. helpText := `📖 红包机器人帮助
  122. 🧧 红包相关:
  123. • 群组定期自动发放红包
  124. • 点击红包消息下方的按钮即可抢红包
  125. • 普通红包:每人金额相同
  126. • 手气红包:每人金额随机
  127. 📝 使用步骤:
  128. 1️⃣ 点击「📝 一键注册」自动注册并绑定
  129. 2️⃣ 等待群内红包发放
  130. 3️⃣ 点击红包按钮抢红包
  131. 💡 按钮功能:
  132. • 📝 一键注册 - 自动注册平台账户并绑定
  133. • 💰 余额查询 - 查看抢红包统计
  134. • 📊 抢红包记录 - 查看最近领取记录
  135. • ❓ 帮助 - 显示此帮助信息
  136. ⚠️ 注意:
  137. • 未注册无法抢红包
  138. • 每个红包只能抢一次
  139. • 红包有效期由管理员设定`
  140. sendTextMessage(callback.From.ID, helpText)
  141. answerCallback(callback.ID, "📖 已发送到私聊")
  142. }
  143. // buildWelcomeKeyboard 构建欢迎消息按钮面板(群组消息用,含注册按钮)
  144. func buildWelcomeKeyboard() tgbotapi.InlineKeyboardMarkup {
  145. return tgbotapi.NewInlineKeyboardMarkup(
  146. tgbotapi.NewInlineKeyboardRow(
  147. tgbotapi.NewInlineKeyboardButtonData("📝 一键注册", "action_register"),
  148. tgbotapi.NewInlineKeyboardButtonData("💰 余额查询", "action_balance"),
  149. ),
  150. tgbotapi.NewInlineKeyboardRow(
  151. tgbotapi.NewInlineKeyboardButtonData("📊 抢红包记录", "action_records"),
  152. tgbotapi.NewInlineKeyboardButtonData("❓ 帮助", "action_help"),
  153. ),
  154. )
  155. }
  156. // buildKeyboardForUser 根据用户绑定状态构建按钮面板(私聊用)
  157. // 已注册用户不显示"一键注册"按钮
  158. func buildKeyboardForUser(telegramId int64, username string) tgbotapi.InlineKeyboardMarkup {
  159. bindService := &services.TgBindService{}
  160. bound, _ := bindService.IsUserBound(telegramId, username)
  161. if bound {
  162. // 已注册:只显示功能按钮
  163. return tgbotapi.NewInlineKeyboardMarkup(
  164. tgbotapi.NewInlineKeyboardRow(
  165. tgbotapi.NewInlineKeyboardButtonData("💰 余额查询", "action_balance"),
  166. tgbotapi.NewInlineKeyboardButtonData("📊 抢红包记录", "action_records"),
  167. ),
  168. tgbotapi.NewInlineKeyboardRow(
  169. tgbotapi.NewInlineKeyboardButtonData("❓ 帮助", "action_help"),
  170. ),
  171. )
  172. }
  173. // 未注册:显示完整面板(含注册按钮)
  174. return buildWelcomeKeyboard()
  175. }