rcs-data
Version:
RCS消息数据结构
121 lines • 3.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Builder = exports.Suggestion = exports.ResponseAction = exports.ResponseReply = exports.SuggestionResponse = void 0;
const mime_1 = require("../mime");
const Suggestion_1 = require("./Suggestion");
Object.defineProperty(exports, "Suggestion", { enumerable: true, get: function () { return Suggestion_1.Suggestion; } });
const RcsMsgInitial_1 = require("./RcsMsgInitial");
class ResponseReply {
constructor(data) {
this.reply = data;
}
get displayText() {
return this.reply.displayText;
}
get postbackData() {
return this.reply.postback.data;
}
toJSON() {
return {
reply: this.reply,
};
}
}
exports.ResponseReply = ResponseReply;
class ResponseAction {
constructor(data) {
this.action = data;
}
get displayText() {
return this.action.displayText;
}
get postbackData() {
return this.action.postback.data;
}
toJSON() {
return {
action: this.action,
};
}
}
exports.ResponseAction = ResponseAction;
class Builder {
static of(type) {
let builder = new Builder();
builder.type = type;
return builder;
}
displayText(value) {
this._displayText = value;
return this;
}
postbackData(value) {
this._postbackData = value;
return this;
}
build() {
let { type, _displayText, _postbackData } = this;
if (type === 'reply') {
return new SuggestionResponse(new ResponseReply(new Suggestion_1.Suggestion(_displayText, _postbackData)));
}
else if (type === 'action') {
return new SuggestionResponse(new ResponseAction(new Suggestion_1.Suggestion(_displayText, _postbackData)));
}
throw new TypeError(`不支持的构造类型[${type}]`);
}
}
exports.Builder = Builder;
class SuggestionResponse extends RcsMsgInitial_1.RcsMsgInitial {
constructor(response) {
let mime = (0, mime_1.factory)({
contentType: 'application/vnd.gsma.botsuggestion.response.v1.0+json',
body: () => {
let body = JSON.stringify({ response }, null, 1);
body = body.split('\n').join('\r\n');
mime.header('Content-Length', `${body.length}`);
return body;
},
});
super(mime);
this._response = response;
}
get content() {
return this._response;
}
get messagePlainObject() {
let msg = {
contentType: 'application/vnd.gsma.botsuggestion.response.v1.0+json',
contentText: { response: this.content.toJSON() },
};
return msg;
}
static parse(raw) {
let obj = JSON.parse(raw);
let { response } = obj;
if (!response && typeof response !== 'object')
throw new TypeError('数据格式错误,没有包含[response]字段');
let builder;
let { reply, action } = response;
if (reply && typeof reply === 'object') {
builder = Builder.of('reply');
}
else if (action && typeof action === 'object') {
builder = Builder.of('action');
}
else {
throw new TypeError('数据格式错误,没有包含[reply/action]字段');
}
let { displayText, postback } = response[builder.type];
if (displayText && typeof displayText === 'string')
builder.displayText(displayText);
if (postback && typeof postback === 'object') {
let { data } = postback;
if (data && typeof data === 'string')
builder.postbackData(data);
}
let ins = builder.build();
return ins;
}
}
exports.SuggestionResponse = SuggestionResponse;
//# sourceMappingURL=SuggestionResponse.js.map