threema-gateway-notify
Version:
Send Notifications via Threema Gateway with Typescript
55 lines • 2.6 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const assert_1 = __importDefault(require("assert"));
const querystring_1 = __importDefault(require("querystring"));
class ThreemaConnector {
/**
* @param {string[]} toIds - List of Threema-Ids receiving the notification
* @param {string} fromId - ThreemaId sending the notification
* @param {string} apiSecret - Threema Gateway Api Secret corresponds to the sender
*/
constructor(toIds, fromId, apiSecret) {
this.toIds = toIds;
this.fromId = fromId;
this.apiSecret = apiSecret;
assert_1.default.ok(this.toIds, "please pass parameter toIds:string[]");
assert_1.default.ok(this.toIds.length > 0, "parameter 'toIds:string[]' must not be empty");
assert_1.default.ok(this.fromId, 'please pass parameter fromId:string');
assert_1.default.ok(this.apiSecret, 'please pass parameter apiSecret:string');
}
sendMessage(message) {
return __awaiter(this, void 0, void 0, function* () {
this.toIds.forEach(id => this.sendMessageToId(message, id));
});
}
sendMessageToId(message, toId) {
const data = {
from: this.fromId,
to: toId,
text: message,
secret: this.apiSecret
};
const config = {
url: 'https://msgapi.threema.ch/send_simple',
method: 'post',
headers: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8' },
data: querystring_1.default.stringify(data)
};
return axios_1.default(config);
}
}
exports.ThreemaConnector = ThreemaConnector;
//# sourceMappingURL=threema.js.map