messages.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. // sendTextMessage 发送文本消息
  8. func sendTextMessage(chatID int64, text string) {
  9. if !IsEnabled() {
  10. return
  11. }
  12. msg := tgbotapi.NewMessage(chatID, text)
  13. msg.ParseMode = "Markdown"
  14. _, err := bot.Send(msg)
  15. if err != nil {
  16. core.Log.Errorf("发送消息失败: %v", err)
  17. }
  18. }
  19. // sendHTMLMessage 发送 HTML 格式的消息
  20. func sendHTMLMessage(chatID int64, text string) {
  21. if !IsEnabled() {
  22. return
  23. }
  24. msg := tgbotapi.NewMessage(chatID, text)
  25. msg.ParseMode = "HTML"
  26. _, err := bot.Send(msg)
  27. if err != nil {
  28. core.Log.Errorf("发送消息失败: %v", err)
  29. }
  30. }
  31. // sendMessageWithKeyboard 发送带按钮的消息
  32. func sendMessageWithKeyboard(chatID int64, text string, keyboard tgbotapi.InlineKeyboardMarkup) {
  33. if !IsEnabled() {
  34. return
  35. }
  36. msg := tgbotapi.NewMessage(chatID, text)
  37. msg.ReplyMarkup = keyboard
  38. msg.ParseMode = "Markdown"
  39. _, err := bot.Send(msg)
  40. if err != nil {
  41. core.Log.Errorf("发送消息失败: %v", err)
  42. }
  43. }
  44. // editMessage 编辑消息
  45. func editMessage(chatID int64, messageID int, text string) {
  46. if !IsEnabled() {
  47. return
  48. }
  49. edit := tgbotapi.NewEditMessageText(chatID, messageID, text)
  50. edit.ParseMode = "Markdown"
  51. _, err := bot.Send(edit)
  52. if err != nil {
  53. core.Log.Errorf("编辑消息失败: %v", err)
  54. }
  55. }
  56. // answerCallback 回复回调查询
  57. func answerCallback(callbackID string, text string) {
  58. if !IsEnabled() {
  59. return
  60. }
  61. callback := tgbotapi.NewCallback(callbackID, text)
  62. callback.ShowAlert = false
  63. _, err := bot.Request(callback)
  64. if err != nil {
  65. core.Log.Errorf("回复回调失败: %v", err)
  66. }
  67. }
  68. // sendGroupWelcome 发送机器人加入群组欢迎消息
  69. func sendGroupWelcome(chatID int64) {
  70. text := `👋 大家好!我是红包机器人
  71. 🎉 功能介绍:
  72. • 定期发放群组红包福利
  73. • 支持普通红包和手气红包
  74. 📝 使用方法:
  75. 1. 前往平台注册账户
  76. 2. 私聊我发送 /bind 绑定账户
  77. 3. 绑定后即可抢群内红包
  78. ⚠️ 未绑定账户无法抢红包
  79. 💡 使用 /help 查看详细帮助`
  80. sendTextMessage(chatID, text)
  81. }
  82. // sendNewMemberWelcome 发送新成员欢迎消息
  83. func sendNewMemberWelcome(chatID int64, username, firstName string) {
  84. displayName := firstName
  85. if username != "" {
  86. displayName = "@" + username
  87. }
  88. text := fmt.Sprintf(`👋 欢迎 %s 加入群组!
  89. 🧧 本群定期发放红包福利!
  90. 📝 快速开始:
  91. 1. 前往平台注册账户
  92. 2. 私聊我发送 /bind 绑定账户
  93. 3. 绑定后即可抢红包
  94. ⚠️ 未绑定账户无法抢红包`, displayName)
  95. sendTextMessage(chatID, text)
  96. }
  97. // SendRedPacketToGroup 发送红包消息到群组(供外部调用)
  98. func SendRedPacketToGroup(groupID string, packetNo string, senderName string, totalAmount float64, totalCount int, packetType int, blessingWords string) {
  99. if !IsEnabled() {
  100. core.Log.Warn("Telegram Bot 未启用,无法发送红包消息")
  101. return
  102. }
  103. var packetTypeText string
  104. if packetType == 1 {
  105. packetTypeText = "普通红包"
  106. } else {
  107. packetTypeText = "手气红包"
  108. }
  109. text := fmt.Sprintf(`🧧 %s 发了一个红包
  110. 💰 %s
  111. 总金额: %.2f VND
  112. 个数: %d 个
  113. 类型: %s
  114. 快来抢红包吧!`,
  115. senderName,
  116. blessingWords,
  117. totalAmount,
  118. totalCount,
  119. packetTypeText)
  120. // 创建抢红包按钮
  121. keyboard := tgbotapi.NewInlineKeyboardMarkup(
  122. tgbotapi.NewInlineKeyboardRow(
  123. tgbotapi.NewInlineKeyboardButtonData(
  124. "🎁 抢红包",
  125. fmt.Sprintf("grab_%s", packetNo),
  126. ),
  127. ),
  128. )
  129. // 解析群组 ID(字符串转 int64)
  130. var chatID int64
  131. fmt.Sscanf(groupID, "%d", &chatID)
  132. sendMessageWithKeyboard(chatID, text, keyboard)
  133. }
  134. // PreviewRedPacketToGroup 发送红包预览消息到群组(供外部调用)
  135. func PreviewRedPacketToGroup(groupID string, senderName string, totalAmount float64, totalCount int, packetType int, blessingWords string) {
  136. if !IsEnabled() {
  137. core.Log.Warn("Telegram Bot 未启用,无法发送预览消息")
  138. return
  139. }
  140. var packetTypeText string
  141. if packetType == 1 {
  142. packetTypeText = "普通红包"
  143. } else {
  144. packetTypeText = "手气红包"
  145. }
  146. text := fmt.Sprintf(`🔍 【预览模式】红包预览
  147. 🧧 %s 发了一个红包
  148. 💰 %s
  149. 总金额: %.2f VND
  150. 个数: %d 个
  151. 类型: %s
  152. ⚠️ 这是预览消息,不是真实红包
  153. 实际发送时才能抢红包`,
  154. senderName,
  155. blessingWords,
  156. totalAmount,
  157. totalCount,
  158. packetTypeText)
  159. // 解析群组 ID(字符串转 int64)
  160. var chatID int64
  161. fmt.Sscanf(groupID, "%d", &chatID)
  162. sendTextMessage(chatID, text)
  163. }