@iot9x.com/nestjs-ucpaas-voice
Version:
nestjs 云之讯平台语音集成插件
79 lines (78 loc) • 3.83 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UcpaasSendService = void 0;
const axios_1 = require("@nestjs/axios");
const common_1 = require("@nestjs/common");
const rxjs_1 = require("rxjs");
const common_constant_1 = require("../constants/common.constant");
const error_wrapper_decorator_1 = require("../decorators/error-wrapper.decorator");
const ucpaas_error_1 = require("../errors/ucpaas.error");
const regex_util_1 = require("../utils/regex.util");
const md5_1 = __importDefault(require("md5"));
const ucpaas_constant_1 = require("../constants/ucpaas.constant");
const dayjs_1 = __importDefault(require("dayjs"));
let UcpaasSendService = class UcpaasSendService {
options;
httpService;
regexUtil;
constructor(options, httpService, regexUtil) {
this.options = options;
this.httpService = httpService;
this.regexUtil = regexUtil;
}
/**
* 发送语音通知
* @param params 发送语音通知请求参数
* @returns 发送结果
*/
async voiceNotify(params) {
const nowStr = (0, dayjs_1.default)().format('YYYYMMDDHHmmss');
const { data } = await (0, rxjs_1.lastValueFrom)(this.httpService.post(`${ucpaas_constant_1.UCPAAS_VOICE_BASE_API}/${ucpaas_constant_1.UCPAAS_VOICE_API_VERSION}/Accounts/${this.options.sid}/Calls/voiceNotify?sig=${(0, md5_1.default)(`${this.options.sid}${this.options.token}${nowStr}`).toUpperCase()}`, {
voiceNotify: {
appId: this.options.appId,
...params
}
}, {
headers: {
Authorization: Buffer.from(`${this.options.sid}:${nowStr}`).toString('base64')
}
}));
if (data.resp?.respCode !== '000000')
throw new ucpaas_error_1.UcpaasError(`错误码:${data.resp?.respCode},错误信息:${ucpaas_constant_1.UcpaasErrorMessage[data.resp?.respCode] || '未知错误'},通话标识:${data.resp?.voiceNotify?.callId}`);
return {
success: true,
data: {
callId: data.resp?.voiceNotify?.callId,
createDate: data.resp?.voiceNotify?.createDate
}
};
}
};
__decorate([
(0, error_wrapper_decorator_1.UcpaasErrorWrapper)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], UcpaasSendService.prototype, "voiceNotify", null);
UcpaasSendService = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(common_constant_1.OPTIONS_PROVIDER)),
__metadata("design:paramtypes", [Object, axios_1.HttpService,
regex_util_1.RegexUtil])
], UcpaasSendService);
exports.UcpaasSendService = UcpaasSendService;