wechaty-puppet-padplus
Version:
Puppet Padplus for Wechaty
169 lines • 6.89 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const config_1 = require("../../config");
const PadPlusServer_pb_1 = require("../../server-manager/proto-ts/PadPlusServer_pb");
const schemas_1 = require("../../schemas");
const message_1 = require("wechaty-puppet/dist/src/schemas/message");
const PRE = 'PadplusMessage';
class PadplusMessage {
constructor(requestClient) {
// Send url link
this.sendUrlLink = (selfId, receiverId, content) => __awaiter(this, void 0, void 0, function* () {
config_1.log.verbose(PRE, `sendUrlLink()`);
return this.sendMessage(selfId, receiverId, content, schemas_1.PadplusMessageType.App);
});
// send contact card
this.sendContact = (selfId, receiver, content) => __awaiter(this, void 0, void 0, function* () {
config_1.log.verbose(PRE, `sendContact()`);
return this.sendMessage(selfId, receiver, content, schemas_1.PadplusMessageType.ShareCard);
});
this.sendFile = (selfId, receiver, url, fileName, subType, fileSize) => __awaiter(this, void 0, void 0, function* () {
config_1.log.verbose(PRE, `sendFile()`);
let data = {};
if (subType === 'doc') {
const content = {
des: fileName,
fileSize,
thumburl: '',
title: fileName,
url,
};
data = {
appMsgType: message_1.WechatAppMessageType.Attach,
content: JSON.stringify(content),
fileName,
fromUserName: selfId,
messageType: schemas_1.PadplusMessageType.App,
subType,
toUserName: receiver,
url,
};
const res = yield this.requestClient.request({
apiType: PadPlusServer_pb_1.ApiType.SEND_MESSAGE,
data,
});
if (res) {
const msgDataStr = res.getData();
if (msgDataStr) {
const msgData = JSON.parse(msgDataStr);
return msgData;
}
else {
throw new Error(`can not parse message data from grpc`);
}
}
else {
throw new Error(`can not get response from grpc server`);
}
}
if (subType === 'pic') {
data = {
fileName,
fromUserName: selfId,
messageType: schemas_1.PadplusMessageType.Image,
subType,
toUserName: receiver,
url,
};
}
else if (subType === 'video') {
const content = {
des: fileName,
thumburl: '',
title: fileName,
type: 5,
url,
};
data = {
content: JSON.stringify(content),
fileName,
fromUserName: selfId,
messageType: schemas_1.PadplusMessageType.App,
subType: 'doc',
toUserName: receiver,
url,
};
}
const res = yield this.requestClient.request({
apiType: PadPlusServer_pb_1.ApiType.SEND_FILE,
data,
});
if (res) {
const msgDataStr = res.getData();
if (msgDataStr) {
const msgData = JSON.parse(msgDataStr);
return msgData;
}
else {
throw new Error(`can not parse message data from grpc`);
}
}
else {
throw new Error(`can not get response from grpc server`);
}
});
this.requestClient = requestClient;
}
// Send message (text, image, url, video, file, gif)
sendMessage(selfId, receiverId, content, messageType, mentionListStr) {
return __awaiter(this, void 0, void 0, function* () {
config_1.log.verbose(PRE, `sendMessage()`);
const data = {
content: content,
fromUserName: selfId,
mentionListStr,
messageType,
toUserName: receiverId,
};
try {
const res = yield this.requestClient.request({
apiType: PadPlusServer_pb_1.ApiType.SEND_MESSAGE,
data,
});
if (res) {
const msgDataStr = res.getData();
if (msgDataStr) {
const msgData = JSON.parse(msgDataStr);
return msgData;
}
else {
throw new Error(`can not parse message data from grpc`);
}
}
else {
throw new Error(`can not get response from grpc server`);
}
}
catch (e) {
throw new Error(`can not send message due to this error: ${e}`);
}
});
}
loadRichMeidaData(mediaData) {
return __awaiter(this, void 0, void 0, function* () {
config_1.log.silly(PRE, `loadRichMeidaData()`);
const res = yield this.requestClient.request({
apiType: PadPlusServer_pb_1.ApiType.GET_MESSAGE_MEDIA,
data: mediaData,
});
config_1.log.silly(PRE, `loadRichMeidaData() : ${JSON.stringify(res)}`);
if (res) {
return res;
}
else {
throw new Error(`can not load rich media data.`);
}
});
}
}
exports.PadplusMessage = PadplusMessage;
//# sourceMappingURL=message.js.map