| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- 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) {
- welcomeText := `🎉 欢迎使用红包机器人!
- 🧧 功能介绍:
- • 群组定期发放红包福利
- • 支持普通红包(固定金额)
- • 支持手气红包(随机金额)
- 📝 使用步骤:
- 1️⃣ 前往平台注册账户
- 2️⃣ 使用 /bind 绑定平台账户
- 3️⃣ 等待群内红包发放
- 4️⃣ 点击按钮抢红包
- 💡 快速开始:
- /help - 查看帮助
- /bind - 绑定账户
- ⚠️ 未绑定账户无法抢红包`
- sendTextMessage(msg.Chat.ID, welcomeText)
- }
- // handleHelpCommand 处理 /help 命令
- func handleHelpCommand(msg *tgbotapi.Message) {
- helpText := `📖 命令帮助
- 🧧 红包相关:
- • 群组定期自动发放红包
- • 点击红包消息下方的按钮即可抢红包
- • 普通红包:每人金额相同
- • 手气红包:每人金额随机
- 👤 账户相关:
- /bind - 绑定平台账户
- ℹ️ 其他:
- /help - 显示此帮助
- /start - 显示欢迎消息
- /groupinfo - 查看群组信息(仅限群组)
- 💡 提示:
- • 抢红包需要先绑定账户
- • 每个红包只能抢一次
- • 红包24小时内有效`
- sendTextMessage(msg.Chat.ID, helpText)
- }
- // handleBindCommand 处理 /bind 命令
- func handleBindCommand(msg *tgbotapi.Message) {
- telegramId := msg.From.ID
- telegramUsername := msg.From.UserName
- bindService := &services.TgBindService{}
- // 检查是否已绑定
- bound, _ := bindService.IsUserBound(telegramId)
- 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)
- // 如果在群组中,提示查看私聊
- if msg.Chat.Type == "group" || msg.Chat.Type == "supergroup" {
- sendTextMessage(msg.Chat.ID, "📩 绑定信息已通过私信发送,请查看机器人私聊消息。")
- }
- // 发送绑定信息到私聊
- 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)
- sendMessageWithKeyboard(telegramId, text, keyboard)
- }
- // handleBalanceCommand 处理 /balance 命令
- func handleBalanceCommand(msg *tgbotapi.Message) {
- sendTextMessage(msg.Chat.ID, "💰 余额查询功能开发中...")
- }
- // handleRecordsCommand 处理 /records 命令
- func handleRecordsCommand(msg *tgbotapi.Message) {
- sendTextMessage(msg.Chat.ID, "📊 记录查询功能开发中...")
- }
- // 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)
- }
|