add chat creation for blink creator

This commit is contained in:
kulikov0 committed 2026-10-22 14:09:51 +02:00
1 parent 6176f4eaf5
commit a1bc54dd62
5 files changed
+1669 -55

No files matched your search

+88 -37
View File
@@ -11,6 +11,7 @@ import (
"strconv"
"strings"
"github.com/google/uuid"
headless "github.com/kulikov0/headless-client"
"whitelist-bypass/relay/common"
)
@@ -77,43 +78,6 @@ func IsUnauthorized(err error) bool {
return errors.As(err, &statusErr) && statusErr.Status == http.StatusUnauthorized
}
func apiDo(client *http.Client, method, path, bearer string, body any) ([]byte, int, error) {
var reader io.Reader
if body != nil {
payload, err := json.Marshal(body)
if err != nil {
return nil, 0, err
}
reader = bytes.NewReader(payload)
}
request, err := http.NewRequest(method, APIBase+path, reader)
if err != nil {
return nil, 0, err
}
for name, values := range headless.ChromeWindows.Headers(headless.DestEmpty) {
request.Header[name] = values
}
if bearer != "" {
request.Header.Set("Authorization", "Bearer "+bearer)
}
if body != nil {
request.Header.Set("Content-Type", "application/json")
}
request.Header.Set("Origin", Origin)
request.Header.Set("Referer", Origin+"/")
request.Header.Set("X-User-Agent", ClientUserAgent)
if client == nil {
client = headless.ChromeWindows.HTTPClient()
}
response, err := client.Do(request)
if err != nil {
return nil, 0, err
}
defer response.Body.Close()
raw, err := io.ReadAll(response.Body)
return raw, response.StatusCode, err
}
func RequestLogin(client *http.Client) (LoginRequest, error) {
var result LoginRequest
raw, status, err := apiDo(client, http.MethodGet, "/login/webapp/token/request", "", nil)
@@ -190,3 +154,90 @@ func ConnectCall(client *http.Client, accessToken string, chatID int64) (CallCon
}
return result, nil
}
func FetchAccountID(client *http.Client, accessToken string) (int64, error) {
var result struct {
AccountID int64 `json:"account_id"`
}
raw, status, err := apiDo(client, http.MethodGet, "/account/profile", accessToken, nil)
if err != nil {
return 0, fmt.Errorf("account profile: %w", err)
}
if status != http.StatusOK {
return 0, &StatusError{Operation: "account profile", Status: status, Body: common.BodySnippet(raw)}
}
if err := json.Unmarshal(raw, &result); err != nil {
return 0, fmt.Errorf("account profile decode: %w", err)
}
if result.AccountID <= 0 {
return 0, fmt.Errorf("account profile: no account_id in response")
}
return result.AccountID, nil
}
func CreateGroupChat(client *http.Client, accessToken string, accountID int64, name string) (int64, error) {
var result struct {
Data struct {
ChatID int64 `json:"chat_id"`
} `json:"data"`
}
raw, status, err := apiDo(client, http.MethodPost, "/messenger/v2/group-chat/create", accessToken, map[string]any{
"action_type": "create_group_chat",
"request_id": uuid.New().String(),
"actor_id": accountID,
"action_data": map[string]any{
"members_ids": []int64{},
"name": name,
},
})
if err != nil {
return 0, fmt.Errorf("group chat create: %w", err)
}
if status != http.StatusOK {
return 0, &StatusError{Operation: "group chat create", Status: status, Body: common.BodySnippet(raw)}
}
if err := json.Unmarshal(raw, &result); err != nil {
return 0, fmt.Errorf("group chat create decode: %w", err)
}
if result.Data.ChatID <= 0 {
return 0, fmt.Errorf("group chat create: no chat_id in %s", common.BodySnippet(raw))
}
return result.Data.ChatID, nil
}
func apiDo(client *http.Client, method, path, bearer string, body any) ([]byte, int, error) {
var reader io.Reader
if body != nil {
payload, err := json.Marshal(body)
if err != nil {
return nil, 0, err
}
reader = bytes.NewReader(payload)
}
request, err := http.NewRequest(method, APIBase+path, reader)
if err != nil {
return nil, 0, err
}
for name, values := range headless.ChromeWindows.Headers(headless.DestEmpty) {
request.Header[name] = values
}
if bearer != "" {
request.Header.Set("Authorization", "Bearer "+bearer)
}
if body != nil {
request.Header.Set("Content-Type", "application/json")
}
request.Header.Set("Origin", Origin)
request.Header.Set("Referer", Origin+"/")
request.Header.Set("X-User-Agent", ClientUserAgent)
if client == nil {
client = headless.ChromeWindows.HTTPClient()
}
response, err := client.Do(request)
if err != nil {
return nil, 0, err
}
defer response.Body.Close()
raw, err := io.ReadAll(response.Body)
return raw, response.StatusCode, err
}