fix: 统一所有平台使用同一userID,TG和QQ数据互通
This commit is contained in:
@@ -12,6 +12,9 @@ import (
|
|||||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DefaultUserID 统一用户ID,使所有平台共享同一份账本
|
||||||
|
const DefaultUserID int64 = 1
|
||||||
|
|
||||||
type TGBot struct {
|
type TGBot struct {
|
||||||
api *tgbotapi.BotAPI
|
api *tgbotapi.BotAPI
|
||||||
finance *service.FinanceService
|
finance *service.FinanceService
|
||||||
@@ -53,7 +56,6 @@ func (b *TGBot) Start(ctx context.Context) {
|
|||||||
func (b *TGBot) handleMessage(msg *tgbotapi.Message) {
|
func (b *TGBot) handleMessage(msg *tgbotapi.Message) {
|
||||||
text := msg.Text
|
text := msg.Text
|
||||||
chatID := msg.Chat.ID
|
chatID := msg.Chat.ID
|
||||||
userID := msg.From.ID
|
|
||||||
|
|
||||||
var reply string
|
var reply string
|
||||||
|
|
||||||
@@ -66,7 +68,7 @@ func (b *TGBot) handleMessage(msg *tgbotapi.Message) {
|
|||||||
|
|
||||||
case text == "/today":
|
case text == "/today":
|
||||||
today := time.Now().Format("2006-01-02")
|
today := time.Now().Format("2006-01-02")
|
||||||
items, err := b.finance.GetTransactionsByDate(userID, today)
|
items, err := b.finance.GetTransactionsByDate(DefaultUserID, today)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
reply = "❌ 查询失败"
|
reply = "❌ 查询失败"
|
||||||
} else if len(items) == 0 {
|
} else if len(items) == 0 {
|
||||||
@@ -84,7 +86,7 @@ func (b *TGBot) handleMessage(msg *tgbotapi.Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case text == "/list":
|
case text == "/list":
|
||||||
items, err := b.finance.GetTransactions(userID, 10)
|
items, err := b.finance.GetTransactions(DefaultUserID, 10)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
reply = "❌ 查询失败"
|
reply = "❌ 查询失败"
|
||||||
} else if len(items) == 0 {
|
} else if len(items) == 0 {
|
||||||
@@ -103,10 +105,10 @@ func (b *TGBot) handleMessage(msg *tgbotapi.Message) {
|
|||||||
|
|
||||||
default:
|
default:
|
||||||
// 记账逻辑
|
// 记账逻辑
|
||||||
amount, category, err := b.finance.AddTransaction(userID, text)
|
amount, category, err := b.finance.AddTransaction(DefaultUserID, text)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
reply = "❌ 记账失败,请稍后重试"
|
reply = "❌ 记账失败,请稍后重试"
|
||||||
log.Printf("记账失败 user=%d: %v", userID, err)
|
log.Printf("记账失败: %v", err)
|
||||||
} else if amount == 0 {
|
} else if amount == 0 {
|
||||||
reply = "📍 没看到金额,这笔花了多少钱?"
|
reply = "📍 没看到金额,这笔花了多少钱?"
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package qq
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash/fnv"
|
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -18,6 +17,9 @@ import (
|
|||||||
"github.com/tencent-connect/botgo/token"
|
"github.com/tencent-connect/botgo/token"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DefaultUserID 统一用户ID,使所有平台共享同一份账本
|
||||||
|
const DefaultUserID int64 = 1
|
||||||
|
|
||||||
type QQBot struct {
|
type QQBot struct {
|
||||||
api openapi.OpenAPI
|
api openapi.OpenAPI
|
||||||
finance *service.FinanceService
|
finance *service.FinanceService
|
||||||
@@ -34,13 +36,6 @@ func NewQQBot(appID string, secret string, finance *service.FinanceService) *QQB
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// hashUserID 将 QQ 的字符串用户标识转为 int64
|
|
||||||
func hashUserID(authorID string) int64 {
|
|
||||||
h := fnv.New64a()
|
|
||||||
h.Write([]byte(authorID))
|
|
||||||
return int64(h.Sum64())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *QQBot) Start(ctx context.Context) {
|
func (b *QQBot) Start(ctx context.Context) {
|
||||||
// 创建 token source 并启动自动刷新
|
// 创建 token source 并启动自动刷新
|
||||||
tokenSource := token.NewQQBotTokenSource(b.credentials)
|
tokenSource := token.NewQQBotTokenSource(b.credentials)
|
||||||
@@ -89,7 +84,6 @@ func isCommand(text string, keywords ...string) bool {
|
|||||||
|
|
||||||
// processAndReply 通用记账处理
|
// processAndReply 通用记账处理
|
||||||
func (b *QQBot) processAndReply(userID string, content string) string {
|
func (b *QQBot) processAndReply(userID string, content string) string {
|
||||||
uid := hashUserID(userID)
|
|
||||||
text := strings.TrimSpace(message.ETLInput(content))
|
text := strings.TrimSpace(message.ETLInput(content))
|
||||||
if text == "" {
|
if text == "" {
|
||||||
return ""
|
return ""
|
||||||
@@ -111,7 +105,7 @@ func (b *QQBot) processAndReply(userID string, content string) string {
|
|||||||
"• 帮助 — 本帮助信息"
|
"• 帮助 — 本帮助信息"
|
||||||
|
|
||||||
case isCommand(text, "查看", "记录", "列表", "list", "/list", "最近"):
|
case isCommand(text, "查看", "记录", "列表", "list", "/list", "最近"):
|
||||||
items, err := b.finance.GetTransactions(uid, 10)
|
items, err := b.finance.GetTransactions(DefaultUserID, 10)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "❌ 查询失败"
|
return "❌ 查询失败"
|
||||||
}
|
}
|
||||||
@@ -126,7 +120,7 @@ func (b *QQBot) processAndReply(userID string, content string) string {
|
|||||||
return sb.String()
|
return sb.String()
|
||||||
|
|
||||||
case isCommand(text, "今日", "今天", "today"):
|
case isCommand(text, "今日", "今天", "today"):
|
||||||
items, err := b.finance.GetTransactionsByDate(uid, today)
|
items, err := b.finance.GetTransactionsByDate(DefaultUserID, today)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "❌ 查询失败"
|
return "❌ 查询失败"
|
||||||
}
|
}
|
||||||
@@ -144,7 +138,7 @@ func (b *QQBot) processAndReply(userID string, content string) string {
|
|||||||
return sb.String()
|
return sb.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
amount, category, err := b.finance.AddTransaction(uid, text)
|
amount, category, err := b.finance.AddTransaction(DefaultUserID, text)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("QQ记账失败 user=%s: %v", userID, err)
|
log.Printf("QQ记账失败 user=%s: %v", userID, err)
|
||||||
return "❌ 记账失败,请稍后重试"
|
return "❌ 记账失败,请稍后重试"
|
||||||
|
|||||||
Reference in New Issue
Block a user