feat: implement send group notice

This commit is contained in:
H3CoF6 committed 2026-05-09 03:44:08 +08:00
1 parent d708be4627
commit c862a157db
10 files changed
+158 -26

No files matched your search

+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();
}