commands.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. welcomeText := `🎉 欢迎使用红包机器人!
  34. 🧧 功能介绍:
  35. • 群组定期发放红包福利
  36. • 支持普通红包(固定金额)
  37. • 支持手气红包(随机金额)
  38. 📝 使用步骤:
  39. 1️⃣ 前往平台注册账户
  40. 2️⃣ 使用 /bind 绑定平台账户
  41. 3️⃣ 等待群内红包发放
  42. 4️⃣ 点击按钮抢红包
  43. 💡 快速开始:
  44. /help - 查看帮助
  45. /bind - 绑定账户
  46. ⚠️ 未绑定账户无法抢红包`
  47. sendTextMessage(msg.Chat.ID, welcomeText)
  48. }
  49. // handleHelpCommand 处理 /help 命令
  50. func handleHelpCommand(msg *tgbotapi.Message) {
  51. helpText := `📖 命令帮助
  52. 🧧 红包相关:
  53. • 群组定期自动发放红包
  54. • 点击红包消息下方的按钮即可抢红包
  55. • 普通红包:每人金额相同
  56. • 手气红包:每人金额随机
  57. 👤 账户相关:
  58. /bind - 绑定平台账户
  59. ℹ️ 其他:
  60. /help - 显示此帮助
  61. /start - 显示欢迎消息
  62. /groupinfo - 查看群组信息(仅限群组)
  63. 💡 提示:
  64. • 抢红包需要先绑定账户
  65. • 每个红包只能抢一次
  66. • 红包24小时内有效`
  67. sendTextMessage(msg.Chat.ID, helpText)
  68. }
  69. // handleBindCommand 处理 /bind 命令
  70. func handleBindCommand(msg *tgbotapi.Message) {
  71. telegramId := msg.From.ID
  72. telegramUsername := msg.From.UserName
  73. bindService := &services.TgBindService{}
  74. // 检查是否已绑定
  75. bound, _ := bindService.IsUserBound(telegramId)
  76. if bound {
  77. sendTextMessage(msg.Chat.ID, "✅ 您已绑定平台账户,无需重复绑定。")
  78. return
  79. }
  80. // 生成绑定码
  81. token, err := bindService.GenerateBindToken(telegramId, telegramUsername)
  82. if err != nil {
  83. sendTextMessage(msg.Chat.ID, fmt.Sprintf("⚠️ %s", err.Error()))
  84. return
  85. }
  86. bindURL := generateBindURL(telegramId, token)
  87. // 如果在群组中,提示查看私聊
  88. if msg.Chat.Type == "group" || msg.Chat.Type == "supergroup" {
  89. sendTextMessage(msg.Chat.ID, "📩 绑定信息已通过私信发送,请查看机器人私聊消息。")
  90. }
  91. // 发送绑定信息到私聊
  92. text := fmt.Sprintf("🔗 绑定平台账户\n\n"+
  93. "您的绑定码: *%s*\n"+
  94. "⏰ 有效期: 5分钟\n\n"+
  95. "📝 绑定方式:\n"+
  96. "1. 登录平台后点击下方链接\n"+
  97. "2. 或在平台输入绑定码\n\n"+
  98. "⚠️ 请先注册平台账户", token)
  99. keyboard := tgbotapi.NewInlineKeyboardMarkup(
  100. tgbotapi.NewInlineKeyboardRow(
  101. tgbotapi.NewInlineKeyboardButtonURL("🔗 立即绑定", bindURL),
  102. ),
  103. )
  104. // 私聊发送(telegramId 就是私聊 chatId)
  105. sendMessageWithKeyboard(telegramId, text, keyboard)
  106. }
  107. // handleBalanceCommand 处理 /balance 命令
  108. func handleBalanceCommand(msg *tgbotapi.Message) {
  109. sendTextMessage(msg.Chat.ID, "💰 余额查询功能开发中...")
  110. }
  111. // handleRecordsCommand 处理 /records 命令
  112. func handleRecordsCommand(msg *tgbotapi.Message) {
  113. sendTextMessage(msg.Chat.ID, "📊 记录查询功能开发中...")
  114. }
  115. // handleGroupInfoCommand 处理 /groupinfo 命令
  116. func handleGroupInfoCommand(msg *tgbotapi.Message) {
  117. if msg.Chat.Type != "group" && msg.Chat.Type != "supergroup" {
  118. sendTextMessage(msg.Chat.ID, "⚠️ 此命令只能在群组中使用")
  119. return
  120. }
  121. chatType := msg.Chat.Type
  122. if chatType == "group" {
  123. chatType = "普通群组"
  124. } else if chatType == "supergroup" {
  125. chatType = "超级群组"
  126. }
  127. username := msg.Chat.UserName
  128. if username == "" {
  129. username = "无"
  130. } else {
  131. username = "@" + username
  132. }
  133. infoText := fmt.Sprintf(`📊 群组信息
  134. 🆔 群组ID: <code>%d</code>
  135. 📝 群组名称: %s
  136. 🔗 用户名: %s
  137. 📂 群组类型: %s
  138. 💡 提示:
  139. • 点击群组ID可以复制
  140. • 管理员可以使用此ID配置红包发送`,
  141. msg.Chat.ID,
  142. msg.Chat.Title,
  143. username,
  144. chatType,
  145. )
  146. sendHTMLMessage(msg.Chat.ID, infoText)
  147. }
  148. // generateBindURL 生成绑定链接
  149. func generateBindURL(telegramID int64, token string) string {
  150. baseURL := "https://your-domain.com/bind"
  151. telegramConfig := config.EnvConf().Telegram
  152. if telegramConfig != nil && telegramConfig.BindURL != "" {
  153. baseURL = telegramConfig.BindURL
  154. }
  155. return fmt.Sprintf("%s?telegramId=%d&token=%s", baseURL, telegramID, token)
  156. }