weChatPublicServeMessage

This commit is contained in:
wenjiarui 2023-01-30 16:00:33 +08:00
parent d4945d2cbb
commit ab29f6f47b
3 changed files with 163 additions and 4 deletions

View File

@ -1,3 +1,17 @@
declare type TextServeMessageOption = {
openId: string;
type: 'text';
content: string;
};
declare type NewsServeMessageOption = {
openId: string;
type: 'news';
title: string;
description?: string;
url: string;
picurl?: string;
};
declare type ServeMessageOption = TextServeMessageOption | NewsServeMessageOption;
export declare class WechatPublicInstance {
appId: string;
appSecret: string;
@ -33,9 +47,11 @@ export declare class WechatPublicInstance {
};
clientMsgId?: string;
}): Promise<any>;
sendServeMessage(options: ServeMessageOption): Promise<any>;
batchGetArticle(options: {
offset?: number;
count: number;
noContent?: 0 | 1;
}): Promise<any>;
}
export {};

View File

@ -205,6 +205,70 @@ var WechatPublicInstance = /** @class */ (function () {
});
});
};
WechatPublicInstance.prototype.sendServeMessage = function (options) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var openId, type, myInit, result, errcode;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
openId = options.openId, type = options.type;
myInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
};
switch (type) {
case 'text': {
Object.assign(myInit, {
body: JSON.stringify({
touser: openId,
msgtype: 'text',
text: {
content: options.content,
},
}),
});
break;
}
case 'news': {
Object.assign(myInit, {
body: JSON.stringify({
touser: openId,
msgtype: 'news',
news: {
articles: [
{
title: options.title,
description: options.description,
url: options.url,
picurl: options.picurl
}
]
},
}),
});
break;
}
default: {
throw new Error('当前消息类型暂不支持');
}
}
return [4 /*yield*/, this.access("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".concat(this.accessToken), {
errcode: 0,
errmsg: 'ok',
}, myInit)];
case 1:
result = _a.sent();
errcode = result.errcode;
if (errcode === 0) {
return [2 /*return*/, Object.assign({ success: true }, result)];
}
return [2 /*return*/, Object.assign({ success: false }, result)];
}
});
});
};
WechatPublicInstance.prototype.batchGetArticle = function (options) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var offset, count, noContent, myInit, result, errcode;

View File

@ -2,6 +2,23 @@ require('isomorphic-fetch');
import crypto from 'crypto';
import { Buffer } from 'buffer';
// 目前先支持text和news, 其他type文档https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Service_Center_messages.html
// type ServeMessageType = 'text' | 'news' | 'mpnews' | 'mpnewsarticle' | 'image' | 'voice' | 'video' | 'music' | 'msgmenu';/
type TextServeMessageOption = {
openId: string,
type: 'text',
content: string,
}
type NewsServeMessageOption = {
openId: string,
type: 'news',
title: string,
description?: string,
url: string,
picurl?: string,
}
type ServeMessageOption = TextServeMessageOption | NewsServeMessageOption;
export class WechatPublicInstance {
appId: string;
@ -118,11 +135,11 @@ export class WechatPublicInstance {
}
const scene = sceneId
? {
scene_id: sceneId,
}
scene_id: sceneId,
}
: {
scene_str: sceneStr,
};
scene_str: sceneStr,
};
let actionName = sceneId ? 'QR_SCENE' : 'QR_STR_SCENE';
let myInit = {
method: 'POST',
@ -211,6 +228,68 @@ export class WechatPublicInstance {
}
return Object.assign({ success: false }, result);
}
async sendServeMessage(options: ServeMessageOption) {
const { openId, type } = options;
const myInit = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
};
switch (type) {
case 'text': {
Object.assign(
myInit, {
body: JSON.stringify({
touser: openId,
msgtype: 'text',
text: {
content: options.content,
},
}),
}
);
break;
}
case 'news': {
Object.assign(
myInit, {
body: JSON.stringify({
touser: openId,
msgtype: 'news',
news: {
articles: [
{
title: options.title,
description: options.description,
url: options.url,
picurl: options.picurl
}
]
},
}),
}
);
break;
}
default: {
throw new Error('当前消息类型暂不支持')
}
}
const result = await this.access(
`https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=${this.accessToken}`,
{
errcode: 0,
errmsg: 'ok',
},
myInit
);
const { errcode } = result;
if (errcode === 0) {
return Object.assign({ success: true }, result);
}
return Object.assign({ success: false }, result);
}
async batchGetArticle(options: {
offset?: number;
count: number;