messages.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. • 余额管理
  76. 📝 使用方法:
  77. 1. 发送 /bind 绑定账户
  78. 2. 等待群内红包
  79. 3. 点击按钮抢红包
  80. 💡 使用 /help 查看详细帮助`
  81. sendTextMessage(chatID, text)
  82. }
  83. // sendNewMemberWelcome 发送新成员欢迎消息
  84. func sendNewMemberWelcome(chatID int64, username, firstName string) {
  85. displayName := firstName
  86. if username != "" {
  87. displayName = "@" + username
  88. }
  89. text := fmt.Sprintf(`👋 欢迎 %s 加入群组!
  90. 🧧 本群定期发放红包福利!
  91. 📝 快速开始:
  92. 1. 发送 /bind 绑定账户
  93. 2. 等待群内红包
  94. 3. 点击按钮抢红包
  95. 💡 使用 /help 查看更多功能`, displayName)
  96. sendTextMessage(chatID, text)
  97. }
  98. // SendRedPacketToGroup 发送红包消息到群组(供外部调用)
  99. func SendRedPacketToGroup(groupID string, packetNo string, senderName string, totalAmount float64, totalCount int, packetType int, blessingWords string) {
  100. if !IsEnabled() {
  101. core.Log.Warn("Telegram Bot 未启用,无法发送红包消息")
  102. return
  103. }
  104. var packetTypeText string
  105. if packetType == 1 {
  106. packetTypeText = "普通红包"
  107. } else {
  108. packetTypeText = "手气红包"
  109. }
  110. text := fmt.Sprintf(`🧧 %s 发了一个红包
  111. 💰 %s
  112. 总金额: %.2f VND
  113. 个数: %d 个
  114. 类型: %s
  115. 快来抢红包吧!`,
  116. senderName,
  117. blessingWords,
  118. totalAmount,
  119. totalCount,
  120. packetTypeText)
  121. // 创建抢红包按钮
  122. keyboard := tgbotapi.NewInlineKeyboardMarkup(
  123. tgbotapi.NewInlineKeyboardRow(
  124. tgbotapi.NewInlineKeyboardButtonData(
  125. "🎁 抢红包",
  126. fmt.Sprintf("grab_%s", packetNo),
  127. ),
  128. ),
  129. )
  130. // 解析群组 ID(字符串转 int64)
  131. var chatID int64
  132. fmt.Sscanf(groupID, "%d", &chatID)
  133. sendMessageWithKeyboard(chatID, text, keyboard)
  134. }