This commit is contained in:
Huaaudio 2026-03-22 05:06:23 +01:00
parent 8d04fb7403
commit 5fcf08ed09
7 changed files with 93 additions and 88 deletions

View file

@ -35,4 +35,3 @@ func NewOnboardCommand() *cobra.Command {
return cmd return cmd
} }

View file

@ -116,7 +116,7 @@ func saveWeixinConfig(token, baseURL, proxy string) error {
} }
func writeMinimalWeixinConfig(cfgPath, token, baseURL, proxy string) error { func writeMinimalWeixinConfig(cfgPath, token, baseURL, proxy string) error {
if err := os.MkdirAll(internal.GetPicoclawHome(), 0755); err != nil { if err := os.MkdirAll(internal.GetPicoclawHome(), 0o755); err != nil {
return err return err
} }
@ -137,7 +137,7 @@ func writeMinimalWeixinConfig(cfgPath, token, baseURL, proxy string) error {
if err != nil { if err != nil {
return err return err
} }
if err := os.WriteFile(cfgPath, data, 0600); err != nil { if err := os.WriteFile(cfgPath, data, 0o600); err != nil {
return err return err
} }
fmt.Printf("✓ Created config at %s\n", cfgPath) fmt.Printf("✓ Created config at %s\n", cfgPath)

View file

@ -53,7 +53,7 @@ func randomWechatUIN() string {
return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%d", uint32Val))) return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%d", uint32Val)))
} }
func (c *ApiClient) post(ctx context.Context, endpoint string, body interface{}, responseObj interface{}) error { func (c *ApiClient) post(ctx context.Context, endpoint string, body any, responseObj any) error {
u, err := url.Parse(c.BaseURL) u, err := url.Parse(c.BaseURL)
if err != nil { if err != nil {
return err return err
@ -74,11 +74,12 @@ func (c *ApiClient) post(ctx context.Context, endpoint string, body interface{},
if endpoint == "ilink/bot/get_bot_qrcode" || endpoint == "ilink/bot/get_qrcode_status" { if endpoint == "ilink/bot/get_bot_qrcode" || endpoint == "ilink/bot/get_qrcode_status" {
// QR routes have different headers sometimes, but let's stick to base ones // QR routes have different headers sometimes, but let's stick to base ones
if endpoint == "ilink/bot/get_qrcode_status" { if endpoint == "ilink/bot/get_qrcode_status" {
req.Header.Set("iLink-App-ClientVersion", "1") // Use direct map assignment to send exact header name the Tencent API expects
req.Header["iLink-App-ClientVersion"] = []string{"1"}
} }
} else { } else {
req.Header.Set("AuthorizationType", "ilink_bot_token") req.Header["AuthorizationType"] = []string{"ilink_bot_token"}
req.Header.Set("X-WECHAT-UIN", randomWechatUIN()) req.Header["X-WECHAT-UIN"] = []string{randomWechatUIN()}
if c.Token != "" { if c.Token != "" {
req.Header.Set("Authorization", "Bearer "+c.Token) req.Header.Set("Authorization", "Bearer "+c.Token)
} }
@ -191,7 +192,7 @@ func (c *ApiClient) GetQRCodeStatus(ctx context.Context, qrcode string) (*Status
if err != nil { if err != nil {
return nil, err return nil, err
} }
req.Header.Set("iLink-App-ClientVersion", "1") req.Header["iLink-App-ClientVersion"] = []string{"1"}
resp, err := c.HttpClient.Do(req) resp, err := c.HttpClient.Do(req)
if err != nil { if err != nil {

View file

@ -7,10 +7,11 @@ import (
"time" "time"
"github.com/mdp/qrterminal/v3" "github.com/mdp/qrterminal/v3"
"github.com/sipeed/picoclaw/pkg/logger" "github.com/sipeed/picoclaw/pkg/logger"
) )
// AuthFlow opts // AuthFlowOpts configures the interactive QR login flow.
type AuthFlowOpts struct { type AuthFlowOpts struct {
BaseURL string BaseURL string
BotType string BotType string
@ -20,7 +21,10 @@ type AuthFlowOpts struct {
// PerformLoginInteractive starts the Weixin QR login flow and blocks until login is successful or times out. // PerformLoginInteractive starts the Weixin QR login flow and blocks until login is successful or times out.
// It prints a QR code to the terminal for the user to scan. // It prints a QR code to the terminal for the user to scan.
// Returns the BotToken, UserID, AccountID, and BaseUrl on success. // Returns the BotToken, UserID, AccountID, and BaseUrl on success.
func PerformLoginInteractive(ctx context.Context, opts AuthFlowOpts) (botToken, userID, accountID, baseUrl string, err error) { func PerformLoginInteractive(
ctx context.Context,
opts AuthFlowOpts,
) (botToken, userID, accountID, baseUrl string, err error) {
if opts.BaseURL == "" { if opts.BaseURL == "" {
opts.BaseURL = "https://ilinkai.weixin.qq.com/" opts.BaseURL = "https://ilinkai.weixin.qq.com/"
} }
@ -89,7 +93,7 @@ func PerformLoginInteractive(ctx context.Context, opts AuthFlowOpts) (botToken,
if statusResp.BotToken == "" || statusResp.IlinkBotID == "" { if statusResp.BotToken == "" || statusResp.IlinkBotID == "" {
return "", "", "", "", fmt.Errorf("login confirmed but missing bot_token or ilink_bot_id") return "", "", "", "", fmt.Errorf("login confirmed but missing bot_token or ilink_bot_id")
} }
logger.InfoCF("weixin", "Login successful", map[string]interface{}{ logger.InfoCF("weixin", "Login successful", map[string]any{
"account_id": statusResp.IlinkBotID, "account_id": statusResp.IlinkBotID,
}) })
@ -97,7 +101,7 @@ func PerformLoginInteractive(ctx context.Context, opts AuthFlowOpts) (botToken,
case "expired": case "expired":
return "", "", "", "", fmt.Errorf("qrcode expired, please try again") return "", "", "", "", fmt.Errorf("qrcode expired, please try again")
default: default:
logger.WarnCF("weixin", "Unknown QR code status", map[string]interface{}{ logger.WarnCF("weixin", "Unknown QR code status", map[string]any{
"status": statusResp.Status, "status": statusResp.Status,
}) })
} }

View file

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/sipeed/picoclaw/pkg/bus" "github.com/sipeed/picoclaw/pkg/bus"
"github.com/sipeed/picoclaw/pkg/channels" "github.com/sipeed/picoclaw/pkg/channels"
"github.com/sipeed/picoclaw/pkg/config" "github.com/sipeed/picoclaw/pkg/config"
@ -76,7 +77,7 @@ func (c *WeixinChannel) Stop(ctx context.Context) error {
return nil return nil
} }
// pollLoop is the long-poll receive loop. It runs until ctx is cancelled. // pollLoop is the long-poll receive loop. It runs until ctx is canceled.
func (c *WeixinChannel) pollLoop(ctx context.Context) { func (c *WeixinChannel) pollLoop(ctx context.Context) {
const ( const (
defaultPollTimeoutMs = 35_000 defaultPollTimeoutMs = 35_000