43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package qq_obv11
|
|
|
|
// This file implements the client role of the OneBot v11 WebSocket channel: the
|
|
// adapter dials out to a OneBot (正向) WebSocket endpoint, authenticating with
|
|
// an access token when configured.
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
// connectClient dials the configured OneBot WebSocket endpoint and installs the
|
|
// resulting connection as the active session.
|
|
func (a *Adapter) connectClient(ctx context.Context) error {
|
|
if a.cfg.RemoteURL == "" {
|
|
return errors.New("qq-obv11: client mode requires remoteURL")
|
|
}
|
|
target := appendQueryToken(a.cfg.RemoteURL, a.cfg.AccessToken)
|
|
conn, err := dialWS(ctx, a.cfg.DialContext, target, a.cfg.AccessToken)
|
|
if err != nil {
|
|
return fmt.Errorf("qq-obv11: dial onebot ws %s: %w", a.cfg.RemoteURL, err)
|
|
}
|
|
a.setConn(conn)
|
|
a.log.Info("connected to onebot ws (client mode)", "url", a.cfg.RemoteURL)
|
|
return nil
|
|
}
|
|
|
|
// appendQueryToken adds an access_token query parameter to a URL, preserving any
|
|
// existing query string.
|
|
func appendQueryToken(rawURL, token string) string {
|
|
if token == "" {
|
|
return rawURL
|
|
}
|
|
sep := "?"
|
|
if strings.Contains(rawURL, "?") {
|
|
sep = "&"
|
|
}
|
|
return rawURL + sep + "access_token=" + url.QueryEscape(token)
|
|
}
|