| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- package telegram
- import (
- "app/commons/core"
- "fmt"
- tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
- )
- // sendTextMessage 发送文本消息
- func sendTextMessage(chatID int64, text string) {
- if !IsEnabled() {
- return
- }
- msg := tgbotapi.NewMessage(chatID, text)
- msg.ParseMode = "Markdown"
- _, err := bot.Send(msg)
- if err != nil {
- core.Log.Errorf("发送消息失败: %v", err)
- }
- }
- // sendHTMLMessage 发送 HTML 格式的消息
- func sendHTMLMessage(chatID int64, text string) {
- if !IsEnabled() {
- return
- }
- msg := tgbotapi.NewMessage(chatID, text)
- msg.ParseMode = "HTML"
- _, err := bot.Send(msg)
- if err != nil {
- core.Log.Errorf("发送消息失败: %v", err)
- }
- }
- // sendMessageWithKeyboard 发送带按钮的消息
- func sendMessageWithKeyboard(chatID int64, text string, keyboard tgbotapi.InlineKeyboardMarkup) error {
- if !IsEnabled() {
- return fmt.Errorf("bot未启用")
- }
- msg := tgbotapi.NewMessage(chatID, text)
- msg.ReplyMarkup = keyboard
- msg.ParseMode = "Markdown"
- _, err := bot.Send(msg)
- if err != nil {
- core.Log.Errorf("发送消息失败: %v", err)
- }
- return err
- }
- // editMessage 编辑消息
- func editMessage(chatID int64, messageID int, text string) {
- if !IsEnabled() {
- return
- }
- edit := tgbotapi.NewEditMessageText(chatID, messageID, text)
- edit.ParseMode = "Markdown"
- _, err := bot.Send(edit)
- if err != nil {
- core.Log.Errorf("编辑消息失败: %v", err)
- }
- }
- // answerCallback 回复回调查询
- func answerCallback(callbackID string, text string) {
- if !IsEnabled() {
- return
- }
- callback := tgbotapi.NewCallback(callbackID, text)
- callback.ShowAlert = false
- _, err := bot.Request(callback)
- if err != nil {
- core.Log.Errorf("回复回调失败: %v", err)
- }
- }
- // sendGroupWelcome 发送机器人加入群组欢迎消息
- func sendGroupWelcome(chatID int64) {
- text := `👋 大家好!我是红包机器人
- 🎉 功能介绍:
- • 定期发放群组红包福利
- • 支持普通红包和手气红包
- 📝 使用方法:
- 1. 前往平台注册账户
- 2. 私聊我发送 /bind 绑定账户
- 3. 绑定后即可抢群内红包
- ⚠️ 未绑定账户无法抢红包
- 💡 使用 /help 查看详细帮助`
- sendTextMessage(chatID, text)
- }
- // sendNewMemberWelcome 发送新成员欢迎消息
- func sendNewMemberWelcome(chatID int64, username, firstName string) {
- displayName := firstName
- if username != "" {
- displayName = "@" + username
- }
- text := fmt.Sprintf(`👋 欢迎 %s 加入群组!
- 🧧 本群定期发放红包福利!
- 📝 快速开始:
- 1. 前往平台注册账户
- 2. 私聊我发送 /bind 绑定账户
- 3. 绑定后即可抢红包
- ⚠️ 未绑定账户无法抢红包`, displayName)
- sendTextMessage(chatID, text)
- }
- // SendRedPacketToGroup 发送红包消息到群组(供外部调用)
- func SendRedPacketToGroup(groupID string, packetNo string, senderName string, totalAmount float64, totalCount int, packetType int, blessingWords string) {
- if !IsEnabled() {
- core.Log.Warn("Telegram Bot 未启用,无法发送红包消息")
- return
- }
- var packetTypeText string
- if packetType == 1 {
- packetTypeText = "普通红包"
- } else {
- packetTypeText = "手气红包"
- }
- text := fmt.Sprintf(`🧧 %s 发了一个红包
- 💰 %s
- 总金额: %.2f VND
- 个数: %d 个
- 类型: %s
- 快来抢红包吧!`,
- senderName,
- blessingWords,
- totalAmount,
- totalCount,
- packetTypeText)
- // 创建抢红包按钮
- keyboard := tgbotapi.NewInlineKeyboardMarkup(
- tgbotapi.NewInlineKeyboardRow(
- tgbotapi.NewInlineKeyboardButtonData(
- "🎁 抢红包",
- fmt.Sprintf("grab_%s", packetNo),
- ),
- ),
- )
- // 解析群组 ID(字符串转 int64)
- var chatID int64
- fmt.Sscanf(groupID, "%d", &chatID)
- sendMessageWithKeyboard(chatID, text, keyboard)
- }
- // PreviewRedPacketToGroup 发送红包预览消息到群组(供外部调用)
- func PreviewRedPacketToGroup(groupID string, senderName string, totalAmount float64, totalCount int, packetType int, blessingWords string) {
- if !IsEnabled() {
- core.Log.Warn("Telegram Bot 未启用,无法发送预览消息")
- return
- }
- var packetTypeText string
- if packetType == 1 {
- packetTypeText = "普通红包"
- } else {
- packetTypeText = "手气红包"
- }
- text := fmt.Sprintf(`🔍 【预览模式】红包预览
- 🧧 %s 发了一个红包
- 💰 %s
- 总金额: %.2f VND
- 个数: %d 个
- 类型: %s
- ⚠️ 这是预览消息,不是真实红包
- 实际发送时才能抢红包`,
- senderName,
- blessingWords,
- totalAmount,
- totalCount,
- packetTypeText)
- // 解析群组 ID(字符串转 int64)
- var chatID int64
- fmt.Sscanf(groupID, "%d", &chatID)
- sendTextMessage(chatID, text)
- }
|