Outgoing MMS and webhook both work now, finally

master
Skylar Ittner 2 years ago
parent 3ca8f75807
commit f32ee82029

@ -19,6 +19,30 @@ Back and forth it goes!
Or just run `node main.js`. Or just run `node main.js`.
## Configuration
Here's what the various config options mean.
* `homeserver`: Matrix homeserver.
* `mediaurlpath`: The public HTTP url for accessing media sent by a Matrix user. The parts in curly brackets will be replaced with the proper values. Check what to set the domain to by right-clicking a media file in Element.
* `matrixdomain`: The part after the @ in your username.
* `matrixuser`: Bot account username for this bridge.
* `matrixpass`: Bot account password.
* `inviteusers`: List of users that the bot will invite to new SMS rooms.
* `smsapikey`: Voxtelesys SMS API key.
* `smsfrom`: Default DID. Outgoing SMS will be sent from this number.
* `smsonlyto`: List of DIDs this bridge should handle incoming SMS from.
* `listenport`: HTTP port for webhook endpoint server.
* `publicurl`: Public URL of this bridge's built-in HTTP server.
* `loglevel`: debug, info, or error.
* `smsinterval`: Bridge will poll Voxtelesys API every X seconds. Set to 0 to use webhook only.
## Webhook Setup
In the Voxtelesys Portal, go to APIs -> Messaging Applications. Edit your application and set
HTTP Method to "POST", Events to "Inbound messages (MOs)", MO Webhook to "http://bridge-url:8069/webhook",
and Authentication to "None".
## Starting a new conversation ## Starting a new conversation
When a SMS is received from a new number, a new room is created. If you need to send a message to When a SMS is received from a new number, a new room is created. If you need to send a message to
@ -30,5 +54,4 @@ invited to join a new room and will then be able to send your message.
* If you leave a room the bot won't invite you back, even if there are new SMS messages. * If you leave a room the bot won't invite you back, even if there are new SMS messages.
* Any messages posted in a Matrix room while the bot is offline will not be relayed to SMS when it comes back. * Any messages posted in a Matrix room while the bot is offline will not be relayed to SMS when it comes back.
* Only unread SMS are fetched, so if anything else is checking the API this bot won't get those. * Only unread SMS are fetched, so if anything else is checking the API this bot won't get those.
* This polls the API for messages every 15 seconds (configurable), which is a bit ugly.
* If it crashes try the latest [Node.js LTS](https://github.com/nodesource/distributions) (v14 as of commit date) * If it crashes try the latest [Node.js LTS](https://github.com/nodesource/distributions) (v14 as of commit date)

@ -18,7 +18,8 @@ import { fileURLToPath } from 'url';
import { dirname } from 'path'; import { dirname } from 'path';
import request from 'request'; import request from 'request';
import FileType from 'file-type'; import FileType from 'file-type';
import http from 'http'; import express from 'express';
import bodyParser from 'body-parser';
// Load settings from config.json // Load settings from config.json
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename); const __dirname = dirname(__filename);
@ -408,12 +409,13 @@ function sendMMS(number, from, mediauri, mimetype, callback) {
} }
} }
function handleHTTPRequest(request, response) { function handleHTTPRequest(req, res) {
try { try {
if (request.url == "/webhook") { logger.info("Got HTTP request: " + req.url);
if (req.url == "/webhook") {
try { try {
logger.debug("Got webhook: " + request.body); logger.debug("Got webhook: " + req.body);
var msg = JSON.parse(request.body); var msg = req.body;
if (msg.type == "mo") { if (msg.type == "mo") {
if (settings.smsonlyto.length > 0 && settings.smsonlyto.indexOf(msg.to) != -1) { if (settings.smsonlyto.length > 0 && settings.smsonlyto.indexOf(msg.to) != -1) {
@ -421,27 +423,27 @@ function handleHTTPRequest(request, response) {
createOrJoinSMSRoom(msg.from, msg.to, function (roomid) { createOrJoinSMSRoom(msg.from, msg.to, function (roomid) {
sendMatrix(roomid, msg.body, msg.media); sendMatrix(roomid, msg.body, msg.media);
}); });
response.sendStatus(204); res.sendStatus(204);
response.end(); res.end();
} else { } else {
logger.info("Received SMS from " + msg.from + " for " + msg.to + ", ignoring based on smsonlyto list."); logger.info("Received SMS from " + msg.from + " for " + msg.to + ", ignoring based on smsonlyto list.");
response.sendStatus(403); res.sendStatus(403);
response.end(); res.end();
} }
} else { } else {
response.sendStatus(403); res.sendStatus(403);
response.end(); res.end();
} }
} catch (ex) { } catch (ex) {
logger.error("Decoding webhook body: " + ex); logger.error("Decoding webhook body: " + ex);
response.sendStatus(500); logger.error(req.body);
response.end(); res.sendStatus(500);
res.end();
} }
} else { } else {
try { try {
response.sendStatus(404); res.sendStatus(404);
response.body("404 not found"); res.end();
response.end();
} catch (err) { } catch (err) {
logger.error(err); logger.error(err);
} }
@ -452,15 +454,23 @@ function handleHTTPRequest(request, response) {
} }
const client = sdk.createClient(settings.homeserver); const client = sdk.createClient(settings.homeserver);
var httpserver = express();
var jsonParser = bodyParser.json();
client.login("m.login.password", {"user": settings.matrixuser, "password": settings.matrixpass}).then((response) => { client.login("m.login.password", {"user": settings.matrixuser, "password": settings.matrixpass}).then((response) => {
client.startClient(); client.startClient();
logger.info("Plugged into the matrix."); logger.info("Plugged into the matrix.");
client.once('sync', function (state, prevState, res) { client.once('sync', function (state, prevState, res) {
logger.debug("Initial sync complete (" + state + ")"); logger.debug("Initial sync complete (" + state + ")");
initialsynccomplete = true; initialsynccomplete = true;
var httpserver = http.createServer(handleHTTPRequest); httpserver.post("*", jsonParser, (req, res) => {
httpserver.listen(settings.listenport); handleHTTPRequest(req, res);
logger.info("HTTP server started on port " + settings.listenport); });
httpserver.get("*", (req, res) => {
handleHTTPRequest(req, res);
});
httpserver.listen(settings.listenport, () => {
logger.info("HTTP server listening on port " + settings.listenport);
});
logger.info("Up and running."); logger.info("Up and running.");
if (settings.smsinterval > 0) { if (settings.smsinterval > 0) {
setInterval(checkSMS, settings.smsinterval * 1000); setInterval(checkSMS, settings.smsinterval * 1000);

1788
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -14,6 +14,8 @@
"author": "Netsyms Technologies", "author": "Netsyms Technologies",
"contributors": [], "contributors": [],
"dependencies": { "dependencies": {
"body-parser": "^1.19.1",
"express": "^4.17.2",
"file-type": "^16.5.0", "file-type": "^16.5.0",
"log4js": "^6.3.0", "log4js": "^6.3.0",
"matrix-js-sdk": "^12.0.1" "matrix-js-sdk": "^12.0.1"

Loading…
Cancel
Save