From 7a50084bb9faea96e0499b2ec832158613047abb Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Mon, 14 Jun 2021 16:46:19 -0600 Subject: [PATCH] Add MMS support maybe --- main.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/main.js b/main.js index e5ee13d..15c6cc2 100644 --- a/main.js +++ b/main.js @@ -55,7 +55,7 @@ function checkSMS() { var msg = messages[i]; logger.info("Received SMS from " + msg.from + ": " + msg.body); createOrJoinSMSRoom(msg.from, function (roomid) { - sendMatrix(roomid, msg.body); + sendMatrix(roomid, msg.body, msg.media); }); } } catch (ex) { @@ -128,7 +128,53 @@ function createOrJoinSMSRoom(tel, callback) { * @param {function|undefined} callback passes true when successful, false on failure. * @returns {undefined} */ -function sendMatrix(roomid, body, callback) { +function sendMatrix(roomid, body, media, callback) { + + if (body == "" && typeof media != "undefined") { + body = "Attachment"; + } + + if (typeof media == "string") { + var content = { + body: body, + msgtype: "m.file", + url: media + } + 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); + }); + } else if (typeof media == "array") { + for (var i = 0; i < media.length; i++) { + var content = { + body: body, + msgtype: "m.file", + url: media[i] + } + 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); + }); + } + } var content = { body: body, @@ -240,6 +286,7 @@ client.login("m.login.password", {"user": settings.matrixuser, "password": setti 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, ""); } }); }); \ No newline at end of file