diff --git a/main.js b/main.js index 7a681cd..15689c6 100644 --- a/main.js +++ b/main.js @@ -163,11 +163,11 @@ function getAndUploadFile(url, callback) { * Send a message to a Matrix room. * @param {string} roomid the room to post the message in. * @param {string} body message content. + * @param {array} media Array of media URLs to download via HTTP(s) and send to Matrix. * @param {function|undefined} callback passes true when successful, false on failure. * @returns {undefined} */ function sendMatrix(roomid, body, media, callback) { - if (Array.isArray(media)) { for (var i = 0; i < media.length; i++) { getAndUploadFile(media[i], function (uri, mimetype) { @@ -205,7 +205,7 @@ function sendMatrix(roomid, body, media, callback) { var content = { body: body, msgtype: "m.text" - } + }; client.sendEvent(roomid, "m.room.message", content, "").then((res) => { if (typeof callback == "function") { callback(true); @@ -224,6 +224,26 @@ function sendMatrix(roomid, body, media, callback) { } } +function sendMatrixNotice(roomid, body, callback) { + var content = { + body: body, + msgtype: "m.notice" + }; + client.sendEvent(roomid, "m.room.message", content, "").then((res) => { + if (typeof callback == "function") { + callback(true); + } + }).catch((err) => { + if (typeof callback == "function") { + callback(false); + } + if (err.data.error == "Unknown room") { + return; + } + logger.error(err); + }); +} + /** * Send a SMS to a phone number. * @param {string} number @@ -289,19 +309,20 @@ client.login("m.login.password", {"user": settings.matrixuser, "password": setti if (event.getType() !== "m.room.message") { return; // only use messages } - if (client.getUserId() == event.event.sender) { + if (client.getUserId() == event.getSender()) { return; // skip own messages to prevent loop } - if (event.event.content.body.startsWith("!sms")) { + if (event.getContent().body.startsWith("!sms")) { // capture command to start room for new number - const matches = event.event.content.body.match(/([1-9]?[0-9]{10})/g); + const matches = event.getContent().body.match(/([1-9]?[0-9]{10})/g); if (matches.length == 1) { var tel = matches[0]; if (tel.length == 10) { // make it the full number tel = "1" + tel; } - logger.info("Got request to start new SMS conversation with " + tel + " from " + event.event.sender + "."); + logger.info("Got request to start new SMS conversation with " + tel + " from " + event.getSender() + "."); + sendMatrixNotice(event.getRoomId(), "Starting new conversation with " + tel); createOrJoinSMSRoom(tel, function () { return; }); @@ -309,13 +330,12 @@ client.login("m.login.password", {"user": settings.matrixuser, "password": setti return; } //sendMatrix(room.roomId, "echo " + event.event.content.body); - //console.log(room); const matches = room.name.match(/([1-9][0-9]+)/g); if (matches.length == 1) { var tel = matches[0]; - logger.info("Got message for " + tel + " from " + event.event.sender + ", relaying."); - sendSMS(tel, event.event.content.body); - client.sendEvent(room.roomId, "m.fully_read", room.roomId, ""); + logger.info("Got message for " + tel + " from " + event.getSender() + ", relaying."); + sendSMS(tel, event.getContent().body); + client.sendReadReceipt(event, {}); } }); }); \ No newline at end of file