commands.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package telegram
  2. import (
  3. "app/commons/core"
  4. "fmt"
  5. tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
  6. )
  7. // handleCommand 处理命令
  8. func handleCommand(msg *tgbotapi.Message) {
  9. command := msg.Command()
  10. args := msg.CommandArguments()
  11. core.Log.Infof("处理命令: /%s %s", command, args)
  12. switch command {
  13. case "start":
  14. handleStartCommand(msg)
  15. case "help":
  16. handleHelpCommand(msg)
  17. case "bind":
  18. handleBindCommand(msg)
  19. case "balance":
  20. handleBalanceCommand(msg)
  21. case "records":
  22. handleRecordsCommand(msg)
  23. case "groupinfo":
  24. handleGroupInfoCommand(msg)
  25. default:
  26. sendTextMessage(msg.Chat.ID, "未知命令,使用 /help 查看帮助")
  27. }
  28. }
  29. // handleStartCommand 处理 /start 命令
  30. func handleStartCommand(msg *tgbotapi.Message) {
  31. welcomeText := `🎉 欢迎使用红包机器人!
  32. 🧧 功能介绍:
  33. • 群组定期发放红包福利
  34. • 支持普通红包(固定金额)
  35. • 支持手气红包(随机金额)
  36. • 查看抢红包记录
  37. • 查看余额
  38. 📝 使用步骤:
  39. 1️⃣ 使用 /bind 绑定平台账户
  40. 2️⃣ 等待群内红包发放
  41. 3️⃣ 点击按钮抢红包
  42. 💡 快速开始:
  43. /help - 查看帮助
  44. /bind - 绑定账户
  45. /balance - 查看余额
  46. /records - 查看记录
  47. 祝你抢红包开心!🎊`
  48. sendTextMessage(msg.Chat.ID, welcomeText)
  49. }
  50. // handleHelpCommand 处理 /help 命令
  51. func handleHelpCommand(msg *tgbotapi.Message) {
  52. helpText := `📖 命令帮助
  53. 🧧 红包相关:
  54. • 群组定期自动发放红包
  55. • 点击红包消息下方的按钮即可抢红包
  56. • 普通红包:每人金额相同
  57. • 手气红包:每人金额随机
  58. 👤 账户相关:
  59. /bind - 绑定平台账户
  60. /balance - 查看余额
  61. /records - 查看抢红包记录
  62. ℹ️ 其他:
  63. /help - 显示此帮助
  64. /start - 显示欢迎消息
  65. /groupinfo - 查看群组信息(仅限群组)
  66. 💡 提示:
  67. • 抢红包需要先绑定账户
  68. • 每个红包只能抢一次
  69. • 红包24小时内有效
  70. • 使用 /groupinfo 可以查看群组ID`
  71. sendTextMessage(msg.Chat.ID, helpText)
  72. }
  73. // handleBindCommand 处理 /bind 命令
  74. func handleBindCommand(msg *tgbotapi.Message) {
  75. // 生成绑定链接
  76. bindURL := generateBindURL(msg.From.ID)
  77. text := fmt.Sprintf(`🔗 绑定平台账户
  78. 请点击下方链接完成绑定:
  79. %s
  80. 💡 提示:
  81. • 如果未注册,请先注册账户
  82. • 绑定后即可抢红包
  83. • 绑定链接仅供本人使用`, bindURL)
  84. // 创建按钮
  85. keyboard := tgbotapi.NewInlineKeyboardMarkup(
  86. tgbotapi.NewInlineKeyboardRow(
  87. tgbotapi.NewInlineKeyboardButtonURL("🔗 立即绑定", bindURL),
  88. ),
  89. )
  90. sendMessageWithKeyboard(msg.Chat.ID, text, keyboard)
  91. }
  92. // handleBalanceCommand 处理 /balance 命令
  93. func handleBalanceCommand(msg *tgbotapi.Message) {
  94. // TODO: 调用 API 获取余额
  95. sendTextMessage(msg.Chat.ID, "💰 余额查询功能开发中...")
  96. }
  97. // handleRecordsCommand 处理 /records 命令
  98. func handleRecordsCommand(msg *tgbotapi.Message) {
  99. // TODO: 调用 API 获取抢红包记录
  100. sendTextMessage(msg.Chat.ID, "📊 记录查询功能开发中...")
  101. }
  102. // handleGroupInfoCommand 处理 /groupinfo 命令
  103. func handleGroupInfoCommand(msg *tgbotapi.Message) {
  104. // 检查是否在群组中
  105. if msg.Chat.Type != "group" && msg.Chat.Type != "supergroup" {
  106. sendTextMessage(msg.Chat.ID, "⚠️ 此命令只能在群组中使用")
  107. return
  108. }
  109. // 获取群组信息
  110. chatType := msg.Chat.Type
  111. if chatType == "group" {
  112. chatType = "普通群组"
  113. } else if chatType == "supergroup" {
  114. chatType = "超级群组"
  115. }
  116. username := msg.Chat.UserName
  117. if username == "" {
  118. username = "无"
  119. } else {
  120. username = "@" + username
  121. }
  122. infoText := fmt.Sprintf(`📊 群组信息
  123. 🆔 群组ID: <code>%d</code>
  124. 📝 群组名称: %s
  125. 🔗 用户名: %s
  126. 📂 群组类型: %s
  127. 💡 提示:
  128. • 点击群组ID可以复制
  129. • 管理员可以使用此ID配置红包发送`,
  130. msg.Chat.ID,
  131. msg.Chat.Title,
  132. username,
  133. chatType,
  134. )
  135. // 使用 HTML 解析模式发送消息,这样可以点击复制
  136. sendHTMLMessage(msg.Chat.ID, infoText)
  137. }
  138. // generateBindURL 生成绑定链接
  139. func generateBindURL(telegramID int64) string {
  140. // TODO: 生成真实的绑定链接
  141. baseURL := "https://your-domain.com/bind"
  142. return fmt.Sprintf("%s?telegramId=%d", baseURL, telegramID)
  143. }