Maybe it'll work this time?

This commit is contained in:
Skylar Ittner 2021-06-14 17:00:21 -06:00
parent 84acad8ebc
commit 9404a97e91

39
main.js
View File

@ -14,6 +14,8 @@ import https from 'https';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import { dirname } from 'path'; import { dirname } from 'path';
const request = require('request');
// Load settings from config.json // Load settings from config.json
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
@ -121,6 +123,31 @@ function createOrJoinSMSRoom(tel, callback) {
}); });
} }
function getAndUploadFile(url, callback) {
logger.info("Downloading MMS media " + url);
// download
request({url, encoding: null}, (err, resp, buffer) => {
// upload
logger.info("Uploading MMS media to Matrix " + url);
client.uploadContent(buffer, {
onlyContentUri: true
}).then((res) => {
if (typeof callback == "function") {
callback(res);
logger.info("Media URI: " + res);
}
}).catch((err) => {
if (typeof callback == "function") {
callback(false);
}
if (err.data.error == "Unknown room") {
return;
}
logger.error(err);
});
});
}
/** /**
* Send a message to a Matrix room. * Send a message to a Matrix room.
* @param {string} roomid the room to post the message in. * @param {string} roomid the room to post the message in.
@ -135,10 +162,11 @@ function sendMatrix(roomid, body, media, callback) {
} }
if (typeof media == "string") { if (typeof media == "string") {
getAndUploadFile(media, function (uri) {
var content = { var content = {
body: media, body: body,
msgtype: "m.file", msgtype: "m.file",
url: media url: uri
} }
client.sendEvent(roomid, "m.room.message", content, "").then((res) => { client.sendEvent(roomid, "m.room.message", content, "").then((res) => {
if (typeof callback == "function") { if (typeof callback == "function") {
@ -153,12 +181,14 @@ function sendMatrix(roomid, body, media, callback) {
} }
logger.error(err); logger.error(err);
}); });
});
} else if (Array.isArray(media)) { } else if (Array.isArray(media)) {
for (var i = 0; i < media.length; i++) { for (var i = 0; i < media.length; i++) {
getAndUploadFile(media[i], function (uri) {
var content = { var content = {
body: media[i], body: body,
msgtype: "m.file", msgtype: "m.file",
url: media[i] url: uri
} }
client.sendEvent(roomid, "m.room.message", content, "").then((res) => { client.sendEvent(roomid, "m.room.message", content, "").then((res) => {
if (typeof callback == "function") { if (typeof callback == "function") {
@ -173,6 +203,7 @@ function sendMatrix(roomid, body, media, callback) {
} }
logger.error(err); logger.error(err);
}); });
});
} }
} }