| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- package telegram
- import (
- "app/commons/config"
- "app/commons/core"
- "app/commons/services"
- "fmt"
- tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
- )
- // handleCommand 处理命令
- func handleCommand(msg *tgbotapi.Message) {
- command := msg.Command()
- args := msg.CommandArguments()
- core.Log.Infof("处理命令: /%s %s", command, args)
- switch command {
- case "start":
- handleStartCommand(msg)
- case "help":
- handleHelpCommand(msg)
- case "bind":
- handleBindCommand(msg)
- case "balance":
- handleBalanceCommand(msg)
- case "records":
- handleRecordsCommand(msg)
- case "groupinfo":
- handleGroupInfoCommand(msg)
- default:
- sendTextMessage(msg.Chat.ID, "未知命令,使用 /help 查看帮助")
- }
- }
- // handleStartCommand 处理 /start 命令(私聊时带按钮面板)
- func handleStartCommand(msg *tgbotapi.Message) {
- telegramId := msg.From.ID
- username := msg.From.UserName
- // 检查用户是否已注册
- bindService := &services.TgBindService{}
- bound, _ := bindService.IsUserBound(telegramId, username)
- var welcomeText string
- if bound {
- welcomeText = "🎉 欢迎回来!\n\n" +
- "✅ 您已注册,可以直接抢红包\n\n" +
- "🧧 功能介绍:\n" +
- "• 群组定期发放红包福利\n" +
- "• 支持普通红包和手气红包\n\n" +
- "👇 点击下方按钮使用功能:"
- } else {
- welcomeText = "🎉 欢迎使用红包机器人!\n\n" +
- "🧧 功能介绍:\n" +
- "• 群组定期发放红包福利\n" +
- "• 支持普通红包(固定金额)\n" +
- "• 支持手气红包(随机金额)\n\n" +
- "📝 使用步骤:\n" +
- "1️⃣ 点击「📝 一键注册」自动注册并绑定\n" +
- "2️⃣ 等待群内红包发放\n" +
- "3️⃣ 点击按钮抢红包\n\n" +
- "👇 点击下方按钮开始:"
- }
- keyboard := buildKeyboardForUser(telegramId, username)
- sendMessageWithKeyboard(msg.Chat.ID, welcomeText, keyboard)
- }
- // handleHelpCommand 处理 /help 命令
- func handleHelpCommand(msg *tgbotapi.Message) {
- telegramId := msg.From.ID
- username := msg.From.UserName
- // 检查用户是否已注册
- bindService := &services.TgBindService{}
- bound, _ := bindService.IsUserBound(telegramId, username)
- var helpText string
- if bound {
- helpText = "📖 红包机器人帮助\n\n" +
- "✅ 您已注册,可以直接抢红包\n\n" +
- "🧧 红包玩法:\n" +
- "• 群组定期自动发放红包\n" +
- "• 点击红包消息下方按钮即可抢\n" +
- "• 普通红包:每人金额相同\n" +
- "• 手气红包:每人金额随机\n\n" +
- "💡 按钮功能:\n" +
- "• 💰 余额查询 - 查看抢红包统计\n" +
- "• 📊 抢红包记录 - 查看领取明细\n" +
- "• ❓ 帮助 - 显示此帮助\n\n" +
- "⚠️ 注意:每个红包只能抢一次"
- } else {
- helpText = "📖 红包机器人帮助\n\n" +
- "🧧 红包玩法:\n" +
- "• 群组定期自动发放红包\n" +
- "• 点击红包消息下方按钮即可抢\n" +
- "• 普通红包:每人金额相同\n" +
- "• 手气红包:每人金额随机\n\n" +
- "📝 快速开始:\n" +
- "1️⃣ 点击「📝 一键注册」自动注册并绑定\n" +
- "2️⃣ 等待群内红包,点击抢!\n\n" +
- "💡 按钮功能:\n" +
- "• 📝 一键注册 - 自动注册平台账户\n" +
- "• 💰 余额查询 - 查看抢红包统计\n" +
- "• 📊 抢红包记录 - 查看领取明细\n" +
- "• ❓ 帮助 - 显示此帮助\n\n" +
- "⚠️ 注意:未注册无法抢红包,每个红包只能抢一次"
- }
- keyboard := buildKeyboardForUser(telegramId, username)
- sendMessageWithKeyboard(msg.Chat.ID, helpText, keyboard)
- }
- // handleBindCommand 处理 /bind 命令
- func handleBindCommand(msg *tgbotapi.Message) {
- telegramId := msg.From.ID
- telegramUsername := msg.From.UserName
- bindService := &services.TgBindService{}
- // 检查是否已绑定
- bound, _ := bindService.IsUserBound(telegramId, telegramUsername)
- if bound {
- sendTextMessage(msg.Chat.ID, "✅ 您已绑定平台账户,无需重复绑定。")
- return
- }
- // 生成绑定码
- token, err := bindService.GenerateBindToken(telegramId, telegramUsername)
- if err != nil {
- sendTextMessage(msg.Chat.ID, fmt.Sprintf("⚠️ %s", err.Error()))
- return
- }
- bindURL := generateBindURL(telegramId, token)
- // 发送绑定信息到私聊
- text := fmt.Sprintf("🔗 绑定平台账户\n\n"+
- "您的绑定码: *%s*\n"+
- "⏰ 有效期: 5分钟\n\n"+
- "📝 绑定方式:\n"+
- "1. 登录平台后点击下方链接\n"+
- "2. 或在平台输入绑定码\n\n"+
- "⚠️ 请先注册平台账户", token)
- keyboard := tgbotapi.NewInlineKeyboardMarkup(
- tgbotapi.NewInlineKeyboardRow(
- tgbotapi.NewInlineKeyboardButtonURL("🔗 立即绑定", bindURL),
- ),
- )
- // 私聊发送(telegramId 就是私聊 chatId)
- _, sendErr := sendMessageWithKeyboard(telegramId, text, keyboard)
- if msg.Chat.Type == "group" || msg.Chat.Type == "supergroup" {
- if sendErr != nil {
- // 用户未私聊过 Bot,无法发送私信
- botUsername := bot.Self.UserName
- sendTextMessage(msg.Chat.ID, fmt.Sprintf(
- "⚠️ 无法发送私信,请先点击 @%s 并发送 /start 开启私聊,然后再发送 /bind", botUsername))
- } else {
- sendTextMessage(msg.Chat.ID, "📩 绑定信息已通过私信发送,请查看机器人私聊消息。")
- }
- }
- }
- // handleBalanceCommand 处理 /balance 命令
- func handleBalanceCommand(msg *tgbotapi.Message) {
- telegramId := msg.From.ID
- username := msg.From.UserName
- bindService := &services.TgBindService{}
- bound, _ := bindService.IsUserBound(telegramId, username)
- if !bound {
- sendTextMessage(msg.Chat.ID, "⚠️ 请先点击「📝 一键注册」绑定账户")
- return
- }
- platformService := &services.TgPlatformService{}
- records, err := platformService.GetUserGrabRecords(telegramId, 0)
- if err != nil {
- sendTextMessage(msg.Chat.ID, "⚠️ 查询失败,请稍后再试")
- return
- }
- var totalAmount float64
- for _, r := range records {
- totalAmount += r.Amount.InexactFloat64()
- }
- text := fmt.Sprintf("💰 账户信息\n\n"+
- "🧧 抢红包次数: %d 次\n"+
- "💵 抢红包总额: %.2f\n\n"+
- "💡 详细余额请登录平台查看",
- len(records), totalAmount)
- sendTextMessage(msg.Chat.ID, text)
- }
- // handleRecordsCommand 处理 /records 命令
- func handleRecordsCommand(msg *tgbotapi.Message) {
- telegramId := msg.From.ID
- username := msg.From.UserName
- bindService := &services.TgBindService{}
- bound, _ := bindService.IsUserBound(telegramId, username)
- if !bound {
- sendTextMessage(msg.Chat.ID, "⚠️ 请先点击「📝 一键注册」绑定账户")
- return
- }
- platformService := &services.TgPlatformService{}
- records, err := platformService.GetUserGrabRecords(telegramId, 10)
- if err != nil {
- sendTextMessage(msg.Chat.ID, "⚠️ 查询失败,请稍后再试")
- return
- }
- if len(records) == 0 {
- sendTextMessage(msg.Chat.ID, "📊 暂无抢红包记录\n\n等待群内红包发放后即可参与!")
- return
- }
- text := "📊 最近抢红包记录\n\n"
- for i, r := range records {
- timeStr := r.GrabbedAt.Format("01-02 15:04")
- bestTag := ""
- if r.IsBest == 1 {
- bestTag = " 🏆"
- }
- text += fmt.Sprintf("%d. %s | %.2f%s\n", i+1, timeStr, r.Amount.InexactFloat64(), bestTag)
- }
- text += "\n💡 🏆 = 手气最佳"
- sendTextMessage(msg.Chat.ID, text)
- }
- // handleGroupInfoCommand 处理 /groupinfo 命令
- func handleGroupInfoCommand(msg *tgbotapi.Message) {
- if msg.Chat.Type != "group" && msg.Chat.Type != "supergroup" {
- sendTextMessage(msg.Chat.ID, "⚠️ 此命令只能在群组中使用")
- return
- }
- chatType := msg.Chat.Type
- if chatType == "group" {
- chatType = "普通群组"
- } else if chatType == "supergroup" {
- chatType = "超级群组"
- }
- username := msg.Chat.UserName
- if username == "" {
- username = "无"
- } else {
- username = "@" + username
- }
- infoText := fmt.Sprintf(`📊 群组信息
- 🆔 群组ID: <code>%d</code>
- 📝 群组名称: %s
- 🔗 用户名: %s
- 📂 群组类型: %s
- 💡 提示:
- • 点击群组ID可以复制
- • 管理员可以使用此ID配置红包发送`,
- msg.Chat.ID,
- msg.Chat.Title,
- username,
- chatType,
- )
- sendHTMLMessage(msg.Chat.ID, infoText)
- }
- // generateBindURL 生成绑定链接
- func generateBindURL(telegramID int64, token string) string {
- baseURL := "https://your-domain.com/bind"
- telegramConfig := config.EnvConf().Telegram
- if telegramConfig != nil && telegramConfig.BindURL != "" {
- baseURL = telegramConfig.BindURL
- }
- return fmt.Sprintf("%s?telegramId=%d&token=%s", baseURL, telegramID, token)
- }
|