Send read receipts to Matrix when sending SMS, add feedback to !sms command
This commit is contained in:
parent
ba94886e72
commit
db203ea945
40
main.js
40
main.js
@ -163,11 +163,11 @@ function getAndUploadFile(url, callback) {
|
|||||||
* 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.
|
||||||
* @param {string} body message content.
|
* @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.
|
* @param {function|undefined} callback passes true when successful, false on failure.
|
||||||
* @returns {undefined}
|
* @returns {undefined}
|
||||||
*/
|
*/
|
||||||
function sendMatrix(roomid, body, media, callback) {
|
function sendMatrix(roomid, body, media, callback) {
|
||||||
|
|
||||||
if (Array.isArray(media)) {
|
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, mimetype) {
|
getAndUploadFile(media[i], function (uri, mimetype) {
|
||||||
@ -205,7 +205,7 @@ function sendMatrix(roomid, body, media, callback) {
|
|||||||
var content = {
|
var content = {
|
||||||
body: body,
|
body: body,
|
||||||
msgtype: "m.text"
|
msgtype: "m.text"
|
||||||
}
|
};
|
||||||
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") {
|
||||||
callback(true);
|
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.
|
* Send a SMS to a phone number.
|
||||||
* @param {string} number
|
* @param {string} number
|
||||||
@ -289,19 +309,20 @@ client.login("m.login.password", {"user": settings.matrixuser, "password": setti
|
|||||||
if (event.getType() !== "m.room.message") {
|
if (event.getType() !== "m.room.message") {
|
||||||
return; // only use messages
|
return; // only use messages
|
||||||
}
|
}
|
||||||
if (client.getUserId() == event.event.sender) {
|
if (client.getUserId() == event.getSender()) {
|
||||||
return; // skip own messages to prevent loop
|
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
|
// 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) {
|
if (matches.length == 1) {
|
||||||
var tel = matches[0];
|
var tel = matches[0];
|
||||||
if (tel.length == 10) {
|
if (tel.length == 10) {
|
||||||
// make it the full number
|
// make it the full number
|
||||||
tel = "1" + tel;
|
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 () {
|
createOrJoinSMSRoom(tel, function () {
|
||||||
return;
|
return;
|
||||||
});
|
});
|
||||||
@ -309,13 +330,12 @@ client.login("m.login.password", {"user": settings.matrixuser, "password": setti
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//sendMatrix(room.roomId, "echo " + event.event.content.body);
|
//sendMatrix(room.roomId, "echo " + event.event.content.body);
|
||||||
//console.log(room);
|
|
||||||
const matches = room.name.match(/([1-9][0-9]+)/g);
|
const matches = room.name.match(/([1-9][0-9]+)/g);
|
||||||
if (matches.length == 1) {
|
if (matches.length == 1) {
|
||||||
var tel = matches[0];
|
var tel = matches[0];
|
||||||
logger.info("Got message for " + tel + " from " + event.event.sender + ", relaying.");
|
logger.info("Got message for " + tel + " from " + event.getSender() + ", relaying.");
|
||||||
sendSMS(tel, event.event.content.body);
|
sendSMS(tel, event.getContent().body);
|
||||||
client.sendEvent(room.roomId, "m.fully_read", room.roomId, "");
|
client.sendReadReceipt(event, {});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user