feat: implement send group notice

This commit is contained in:
H3CoF6
2026-05-09 03:44:08 +08:00
parent d708be4627
commit c862a157db
10 changed files with 158 additions and 26 deletions
@@ -13,6 +13,7 @@ import { PushMsgSchema } from './proto/message';
import { RequestUtil } from './web/request-util';
import { getHonorListWebAPI, WebHonorType } from './web/group-honor';
import { getGroupEssenceMsg, getGroupEssenceMsgAll } from './web/group-essence';
import { setGroupNoticeWebAPI } from './web/group-notice'
import {
OidbMuteMemberSchema,
OidbMuteAllSchema,
@@ -1454,4 +1455,51 @@ export async function getGroupEssenceAll(bridge: Bridge, groupId: number) {
const essenceDataAll = await getGroupEssenceMsgAll(cookieObject, groupCode);
return essenceDataAll;
}
export async function sendGroupNotice(
bridge: Bridge,
groupId: number,
content: string,
options?: {
image?: string; // 未来如果要发图片,传路径进来
pinned?: number;
type?: number;
confirm_required?: number;
}
) {
const groupCode = groupId.toString();
const cookieObject = await getCookies(bridge, 'qun.qq.com');
let picId = '';
let imgWidth = 540;
let imgHeight = 300;
// 📝 预留的图片上传逻辑:
// if (options?.image) {
// const picInfo = await bridge.uploadGroupBulletinPic(groupCode, options.image);
// picId = picInfo.id;
// imgWidth = picInfo.width;
// imgHeight = picInfo.height;
// }
const ret = await setGroupNoticeWebAPI(
cookieObject,
groupCode,
content,
options?.pinned ?? 0,
options?.type ?? 1,
1,
1,
options?.confirm_required ?? 1,
picId,
imgWidth,
imgHeight
);
if (!ret || ret.ec !== 0) {
throw new Error(`设置群公告失败: ${ret?.em || '未知错误(Cookie过期或权限不足)'}`);
}
return ret;
}
+6
View File
@@ -69,6 +69,7 @@ import {
forceFetchClientKey as forceFetchClientKey_,
getGroupEssence as getGroupEssence_,
getGroupEssenceAll as getGroupEssenceAll_,
sendGroupNotice as sendGroupNotice_,
} from './bridge-actions';
import type { GroupFilesResult } from './bridge-actions';
import type { MediaIndexNode } from './bridge-actions';
@@ -472,6 +473,11 @@ export class Bridge {
async getGroupEssenceAll(groupId: number): Promise<any> {
return getGroupEssenceAll_(this, groupId);
}
async sendGroupNotice(groupId: number, content: string, options?: any) {
return sendGroupNotice_(this, groupId, content, options);
}
async fetchGroupFileCount(groupId: number): Promise<{ fileCount: number; maxCount: number }> { return fetchGroupFileCount_(this, groupId); }
}
+1 -16
View File
@@ -1,4 +1,4 @@
import { RequestUtil } from './request-util';
import { RequestUtil, cookieToString, getBknFromCookie } from './request-util';
// 定义接口返回类型
export interface GroupEssenceMsgRet {
@@ -11,21 +11,6 @@ export interface GroupEssenceMsgRet {
[key: string]: any;
}
function cookieToString(cookieObject: Record<string, string>): string {
return Object.entries(cookieObject)
.map(([key, value]) => `${key}=${value}`)
.join('; ');
}
// 获取 QQ Web 接口必需的 bkn (或 gtk) 鉴权参数
function getBknFromCookie(cookieObject: Record<string, string>): string {
const skey = cookieObject['p_skey'] || cookieObject['skey'] || '';
let hash = 5381;
for (let i = 0; i < skey.length; i++) {
hash += (hash << 5) + skey.charCodeAt(i);
}
return (hash & 2147483647).toString();
}
/**
* 分页获取群精华消息
+1 -7
View File
@@ -1,4 +1,4 @@
import { RequestUtil } from './request-util';
import { RequestUtil, cookieToString } from './request-util';
export enum WebHonorType {
TALKATIVE = 'talkative',
@@ -8,12 +8,6 @@ export enum WebHonorType {
ALL = 'all',
}
// 将 Cookie 对象转换为字符串的工具函数
function cookieToString(cookieObject: Record<string, string>): string {
return Object.entries(cookieObject)
.map(([key, value]) => `${key}=${value}`)
.join('; ');
}
async function fetchHonorData(cookieObject: Record<string, string>, groupCode: string, type: number) {
let resJson;
@@ -0,0 +1,59 @@
import { RequestUtil, cookieToString, getBknFromCookie } from './request-util';
export interface SetNoticeRetSuccess {
ec?: number;
em?: string;
[key: string]: any;
}
/**
* 发送群公告 Web API
*/
export async function setGroupNoticeWebAPI(
cookieObject: Record<string, string>,
groupCode: string,
content: string,
pinned: number = 0,
type: number = 1,
isShowEditCard: number = 1,
tipWindowType: number = 1,
confirmRequired: number = 1,
picId: string = '',
imgWidth: number = 540,
imgHeight: number = 300
): Promise<SetNoticeRetSuccess | undefined> {
try {
const settings = JSON.stringify({
is_show_edit_card: isShowEditCard,
tip_window_type: tipWindowType,
confirm_required: confirmRequired,
});
const externalParam = {
pic: picId,
imgWidth: imgWidth.toString(),
imgHeight: imgHeight.toString(),
};
const url = `https://web.qun.qq.com/cgi-bin/announce/add_qun_notice?${new URLSearchParams({
bkn: getBknFromCookie(cookieObject),
qid: groupCode,
text: content,
pinned: pinned.toString(),
type: type.toString(),
settings,
...(picId === '' ? {} : externalParam),
}).toString()}`;
const ret = await RequestUtil.HttpGetJson<SetNoticeRetSuccess>(
url,
'POST', // 注意这里必须是 POST
'',
{ Cookie: cookieToString(cookieObject) }
);
return ret;
} catch (e) {
return undefined;
}
}
+17 -1
View File
@@ -108,4 +108,20 @@ export class RequestUtil {
static async HttpGetText(url: string, method: string = 'GET', data?: any, headers: { [key: string]: string; } = {}) {
return this.HttpGetJson<string>(url, method, data, headers, false, false);
}
}
}
export function cookieToString(cookieObject: Record<string, string>): string {
return Object.entries(cookieObject)
.map(([key, value]) => `${key}=${value}`)
.join('; ');
}
export function getBknFromCookie(cookieObject: Record<string, string>): string {
const skey = cookieObject['p_skey'] || cookieObject['skey'] || '';
let hash = 5381;
for (let i = 0; i < skey.length; i++) {
hash += (hash << 5) + skey.charCodeAt(i);
}
return (hash & 2147483647).toString();
}
+24 -2
View File
@@ -230,8 +230,30 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
// --- Group notice stubs ---
h.registerAction('_send_group_notice', async () => {
return failedResponse(RETCODE.ACTION_FAILED, 'not yet implemented');
h.registerAction('send_group_notice', async (params) => {
const groupId = asNumber(params.group_id);
const content = asString(params.content);
if (!groupId || !content) {
return failedResponse(RETCODE.BAD_REQUEST, 'group_id and content are required');
}
if (!ctx.sendGroupNotice) {
return failedResponse(RETCODE.ACTION_FAILED, 'not implemented');
}
try {
const options = {
image: asString(params.image),
pinned: params.pinned !== undefined ? Number(params.pinned) : 0,
type: params.type !== undefined ? Number(params.type) : 1,
confirm_required: params.confirm_required !== undefined ? Number(params.confirm_required) : 1,
};
await ctx.sendGroupNotice(groupId, content, options);
return okResponse();
} catch (e) {
return failedResponse(RETCODE.ACTION_FAILED, String(e));
}
});
h.registerAction('_get_group_notice', async () => {
+1
View File
@@ -98,6 +98,7 @@ export interface ApiActionContext {
getGroupHonorInfo?: (groupId: number, type: WebHonorType | string) => Promise<any>;
getGroupEssence?: (groupId: number, pageStart?: number, pageLimit?: number) => Promise<GroupEssenceMsgRet>;
getGroupEssenceAll?: (groupId: number) => Promise<GroupEssenceMsgRet[]>;
sendGroupNotice?: (groupId: number, content: string, options?: any) => Promise<any>;
}
type ActionHandler = (params: JsonObject) => Promise<import('./types').ApiResponse>;
@@ -128,6 +128,7 @@ export function buildApiContext(ref: InstanceRef): ApiActionContext {
getGroupHonorInfo: (groupId: number, type: WebHonorType | string)=> bridge.getGroupHonorInfo(groupId, type),
getGroupEssence: (groupId: number, pageStart: number = 0, pageLimit: number = 50) => bridge.getGroupEssence(groupId, pageStart, pageLimit),
getGroupEssenceAll: (groupId: number) => bridge.getGroupEssenceAll(groupId),
sendGroupNotice: (groupId: number, content: string, options?: any) => bridge.sendGroupNotice(groupId, content, options),
};
}