commands.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package telegram
  2. import (
  3. "app/commons/config"
  4. "app/commons/core"
  5. "app/commons/services"
  6. "fmt"
  7. tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
  8. )
  9. // handleCommand 处理命令
  10. func handleCommand(msg *tgbotapi.Message) {
  11. command := msg.Command()
  12. args := msg.CommandArguments()
  13. core.Log.Infof("处理命令: /%s %s", command, args)
  14. switch command {
  15. case "start":
  16. handleStartCommand(msg)
  17. case "help":
  18. handleHelpCommand(msg)
  19. case "bind":
  20. handleBindCommand(msg)
  21. case "balance":
  22. handleBalanceCommand(msg)
  23. case "records":
  24. handleRecordsCommand(msg)
  25. case "groupinfo":
  26. handleGroupInfoCommand(msg)
  27. default:
  28. sendTextMessage(msg.Chat.ID, "未知命令,使用 /help 查看帮助")
  29. }
  30. }
  31. // handleStartCommand 处理 /start 命令(私聊时带按钮面板)
  32. func handleStartCommand(msg *tgbotapi.Message) {
  33. telegramId := msg.From.ID
  34. username := msg.From.UserName
  35. // 检查用户是否已注册
  36. bindService := &services.TgBindService{}
  37. bound, _ := bindService.IsUserBound(telegramId, username)
  38. var welcomeText string
  39. if bound {
  40. welcomeText = "🎉 欢迎回来!\n\n" +
  41. "✅ 您已注册,可以直接抢红包\n\n" +
  42. "🧧 功能介绍:\n" +
  43. "• 群组定期发放红包福利\n" +
  44. "• 支持普通红包和手气红包\n\n" +
  45. "👇 点击下方按钮使用功能:"
  46. } else {
  47. welcomeText = "🎉 欢迎使用红包机器人!\n\n" +
  48. "🧧 功能介绍:\n" +
  49. "• 群组定期发放红包福利\n" +
  50. "• 支持普通红包(固定金额)\n" +
  51. "• 支持手气红包(随机金额)\n\n" +
  52. "📝 使用步骤:\n" +
  53. "1️⃣ 点击「📝 一键注册」自动注册并绑定\n" +
  54. "2️⃣ 等待群内红包发放\n" +
  55. "3️⃣ 点击按钮抢红包\n\n" +
  56. "👇 点击下方按钮开始:"
  57. }
  58. keyboard := buildKeyboardForUser(telegramId, username)
  59. sendMessageWithKeyboard(msg.Chat.ID, welcomeText, keyboard)
  60. }
  61. // handleHelpCommand 处理 /help 命令
  62. func handleHelpCommand(msg *tgbotapi.Message) {
  63. telegramId := msg.From.ID
  64. username := msg.From.UserName
  65. // 检查用户是否已注册
  66. bindService := &services.TgBindService{}
  67. bound, _ := bindService.IsUserBound(telegramId, username)
  68. var helpText string
  69. if bound {
  70. helpText = "📖 红包机器人帮助\n\n" +
  71. "✅ 您已注册,可以直接抢红包\n\n" +
  72. "🧧 红包玩法:\n" +
  73. "• 群组定期自动发放红包\n" +
  74. "• 点击红包消息下方按钮即可抢\n" +
  75. "• 普通红包:每人金额相同\n" +
  76. "• 手气红包:每人金额随机\n\n" +
  77. "💡 按钮功能:\n" +
  78. "• 💰 余额查询 - 查看抢红包统计\n" +
  79. "• 📊 抢红包记录 - 查看领取明细\n" +
  80. "• ❓ 帮助 - 显示此帮助\n\n" +
  81. "⚠️ 注意:每个红包只能抢一次"
  82. } else {
  83. helpText = "📖 红包机器人帮助\n\n" +
  84. "🧧 红包玩法:\n" +
  85. "• 群组定期自动发放红包\n" +
  86. "• 点击红包消息下方按钮即可抢\n" +
  87. "• 普通红包:每人金额相同\n" +
  88. "• 手气红包:每人金额随机\n\n" +
  89. "📝 快速开始:\n" +
  90. "1️⃣ 点击「📝 一键注册」自动注册并绑定\n" +
  91. "2️⃣ 等待群内红包,点击抢!\n\n" +
  92. "💡 按钮功能:\n" +
  93. "• 📝 一键注册 - 自动注册平台账户\n" +
  94. "• 💰 余额查询 - 查看抢红包统计\n" +
  95. "• 📊 抢红包记录 - 查看领取明细\n" +
  96. "• ❓ 帮助 - 显示此帮助\n\n" +
  97. "⚠️ 注意:未注册无法抢红包,每个红包只能抢一次"
  98. }
  99. keyboard := buildKeyboardForUser(telegramId, username)
  100. sendMessageWithKeyboard(msg.Chat.ID, helpText, keyboard)
  101. }
  102. // handleBindCommand 处理 /bind 命令
  103. func handleBindCommand(msg *tgbotapi.Message) {
  104. telegramId := msg.From.ID
  105. telegramUsername := msg.From.UserName
  106. bindService := &services.TgBindService{}
  107. // 检查是否已绑定
  108. bound, _ := bindService.IsUserBound(telegramId, telegramUsername)
  109. if bound {
  110. sendTextMessage(msg.Chat.ID, "✅ 您已绑定平台账户,无需重复绑定。")
  111. return
  112. }
  113. // 生成绑定码
  114. token, err := bindService.GenerateBindToken(telegramId, telegramUsername)
  115. if err != nil {
  116. sendTextMessage(msg.Chat.ID, fmt.Sprintf("⚠️ %s", err.Error()))
  117. return
  118. }
  119. bindURL := generateBindURL(telegramId, token)
  120. // 发送绑定信息到私聊
  121. text := fmt.Sprintf("🔗 绑定平台账户\n\n"+
  122. "您的绑定码: *%s*\n"+
  123. "⏰ 有效期: 5分钟\n\n"+
  124. "📝 绑定方式:\n"+
  125. "1. 登录平台后点击下方链接\n"+
  126. "2. 或在平台输入绑定码\n\n"+
  127. "⚠️ 请先注册平台账户", token)
  128. keyboard := tgbotapi.NewInlineKeyboardMarkup(
  129. tgbotapi.NewInlineKeyboardRow(
  130. tgbotapi.NewInlineKeyboardButtonURL("🔗 立即绑定", bindURL),
  131. ),
  132. )
  133. // 私聊发送(telegramId 就是私聊 chatId)
  134. _, sendErr := sendMessageWithKeyboard(telegramId, text, keyboard)
  135. if msg.Chat.Type == "group" || msg.Chat.Type == "supergroup" {
  136. if sendErr != nil {
  137. // 用户未私聊过 Bot,无法发送私信
  138. botUsername := bot.Self.UserName
  139. sendTextMessage(msg.Chat.ID, fmt.Sprintf(
  140. "⚠️ 无法发送私信,请先点击 @%s 并发送 /start 开启私聊,然后再发送 /bind", botUsername))
  141. } else {
  142. sendTextMessage(msg.Chat.ID, "📩 绑定信息已通过私信发送,请查看机器人私聊消息。")
  143. }
  144. }
  145. }
  146. // handleBalanceCommand 处理 /balance 命令
  147. func handleBalanceCommand(msg *tgbotapi.Message) {
  148. telegramId := msg.From.ID
  149. username := msg.From.UserName
  150. bindService := &services.TgBindService{}
  151. bound, _ := bindService.IsUserBound(telegramId, username)
  152. if !bound {
  153. sendTextMessage(msg.Chat.ID, "⚠️ 请先点击「📝 一键注册」绑定账户")
  154. return
  155. }
  156. platformService := &services.TgPlatformService{}
  157. records, err := platformService.GetUserGrabRecords(telegramId, 0)
  158. if err != nil {
  159. sendTextMessage(msg.Chat.ID, "⚠️ 查询失败,请稍后再试")
  160. return
  161. }
  162. var totalAmount float64
  163. for _, r := range records {
  164. totalAmount += r.Amount.InexactFloat64()
  165. }
  166. text := fmt.Sprintf("💰 账户信息\n\n"+
  167. "🧧 抢红包次数: %d 次\n"+
  168. "💵 抢红包总额: %.2f\n\n"+
  169. "💡 详细余额请登录平台查看",
  170. len(records), totalAmount)
  171. sendTextMessage(msg.Chat.ID, text)
  172. }
  173. // handleRecordsCommand 处理 /records 命令
  174. func handleRecordsCommand(msg *tgbotapi.Message) {
  175. telegramId := msg.From.ID
  176. username := msg.From.UserName
  177. bindService := &services.TgBindService{}
  178. bound, _ := bindService.IsUserBound(telegramId, username)
  179. if !bound {
  180. sendTextMessage(msg.Chat.ID, "⚠️ 请先点击「📝 一键注册」绑定账户")
  181. return
  182. }
  183. platformService := &services.TgPlatformService{}
  184. records, err := platformService.GetUserGrabRecords(telegramId, 10)
  185. if err != nil {
  186. sendTextMessage(msg.Chat.ID, "⚠️ 查询失败,请稍后再试")
  187. return
  188. }
  189. if len(records) == 0 {
  190. sendTextMessage(msg.Chat.ID, "📊 暂无抢红包记录\n\n等待群内红包发放后即可参与!")
  191. return
  192. }
  193. text := "📊 最近抢红包记录\n\n"
  194. for i, r := range records {
  195. timeStr := r.GrabbedAt.Format("01-02 15:04")
  196. bestTag := ""
  197. if r.IsBest == 1 {
  198. bestTag = " 🏆"
  199. }
  200. text += fmt.Sprintf("%d. %s | %.2f%s\n", i+1, timeStr, r.Amount.InexactFloat64(), bestTag)
  201. }
  202. text += "\n💡 🏆 = 手气最佳"
  203. sendTextMessage(msg.Chat.ID, text)
  204. }
  205. // handleGroupInfoCommand 处理 /groupinfo 命令
  206. func handleGroupInfoCommand(msg *tgbotapi.Message) {
  207. if msg.Chat.Type != "group" && msg.Chat.Type != "supergroup" {
  208. sendTextMessage(msg.Chat.ID, "⚠️ 此命令只能在群组中使用")
  209. return
  210. }
  211. chatType := msg.Chat.Type
  212. if chatType == "group" {
  213. chatType = "普通群组"
  214. } else if chatType == "supergroup" {
  215. chatType = "超级群组"
  216. }
  217. username := msg.Chat.UserName
  218. if username == "" {
  219. username = "无"
  220. } else {
  221. username = "@" + username
  222. }
  223. infoText := fmt.Sprintf(`📊 群组信息
  224. 🆔 群组ID: <code>%d</code>
  225. 📝 群组名称: %s
  226. 🔗 用户名: %s
  227. 📂 群组类型: %s
  228. 💡 提示:
  229. • 点击群组ID可以复制
  230. • 管理员可以使用此ID配置红包发送`,
  231. msg.Chat.ID,
  232. msg.Chat.Title,
  233. username,
  234. chatType,
  235. )
  236. sendHTMLMessage(msg.Chat.ID, infoText)
  237. }
  238. // generateBindURL 生成绑定链接
  239. func generateBindURL(telegramID int64, token string) string {
  240. baseURL := "https://your-domain.com/bind"
  241. telegramConfig := config.EnvConf().Telegram
  242. if telegramConfig != nil && telegramConfig.BindURL != "" {
  243. baseURL = telegramConfig.BindURL
  244. }
  245. return fmt.Sprintf("%s?telegramId=%d&token=%s", baseURL, telegramID, token)
  246. }