This repository has been archived by the owner on Nov 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from RocketChat/new-freddie
converted to js, added multiples rooms, etc...
- Loading branch information
Showing
5 changed files
with
122 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
module.exports = (HomeServerDomain, RocketChatUserPrefix, HomeServerURL) => { | ||
const handleMatrixMessage = (event, robot, matrix, rocketRoomId, matrixRoomId) => { | ||
console.log('[handleMatrixMessage]'); | ||
const msgpayload = { channel: rocketRoomId, alias: event.user_id, msg: event.content.body }; | ||
return getUserFromMatrix(event.user_id, matrix).then(function (data) { | ||
if(data.avatar) { | ||
msgpayload.avatar = data.avatar; | ||
} | ||
if(data.displayname) { | ||
msgpayload.alias = `${data.displayname}:${HomeServerDomain}` | ||
} | ||
return msgpayload | ||
}, (e) => { | ||
console.error(e); | ||
return msgpayload | ||
}) | ||
.then(msgpayload => { | ||
console.log(`Sending "${ msgpayload.msg }" to Matrix:${ matrixRoomId }`); | ||
robot.adapter.customMessage(msgpayload); | ||
}); | ||
} | ||
function getUserFromMatrix(userId, matrix) { | ||
console.log('[getUserFromMatrix]') | ||
const intent = matrix.getBridge().getIntent(); | ||
// send the message coming in from the HS into Rocket.Chat room | ||
return intent.client.getProfileInfo(userId).then(function(data){ | ||
console.log('[getUserFromMatrix] SUCCESS') | ||
const regex = new RegExp(`(mxc:\/\/)(${escapeRegExp(HomeServerDomain)}\/.*)`, 'g'); | ||
const str = data.avatar_url; | ||
const subst = `${HomeServerURL}/_matrix/media/v1/thumbnail/$2?width=96&height=96&method=crop`; | ||
return { avatar: (str || '').replace(regex, subst) || '', displayname: data.displayname} | ||
}, error => { | ||
console.log('[getUserFromMatrix] ERROR') | ||
console.error(error); | ||
throw error; | ||
}); | ||
} | ||
function escapeRegExp(str) { | ||
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); | ||
} | ||
const getMaps = (params) => { | ||
let tmp = params.HomeServerRoomID && params.RocketChatRoomID ? `${ params.RocketChatRoomID }=${ params.HomeServerRoomID }` : ''; | ||
console.log(tmp); | ||
if (process.env['ROOM_MAP']){ | ||
tmp += (tmp? ',': '') + process.env['ROOM_MAP'] | ||
} | ||
console.log(tmp); | ||
return tmp.split(',').map(val => val.split('=')) | ||
} | ||
const handleRocketChatMessage = (msg, matrixRoomId, matrix) => { | ||
// send the received Rocket.Chat message into the paired HS | ||
const intent = matrix.getBridge().getIntent(RocketChatUserPrefix + msg.message.user.name.toLowerCase() + ":" + HomeServerDomain ); | ||
console.log(`Sending "${ msg.message.text }" to Matrix:${ matrixRoomId }`); | ||
return intent.sendText(matrixRoomId, msg.message.text); | ||
} | ||
return {maps: getMaps, handleMatrixMessage, handleRocketChatMessage}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
const matrixBridge = require('./matrix/matrixbridge'); | ||
const HomeServerURL = process.env['HOMESERVER_URL'] || 'http://0.0.0.0:8008'; | ||
const HomeServerDomain = process.env['HOMESERVER_DOMAIN'] || '0.0.0.0'; | ||
const HomeServerRoomID = process.env['HOMESERVER_ROOM_ID'] || '!WKVTQyzUAjcsqmTGjA:35.185.41.10,'; // HomeServer federated room id | ||
const RocketChatRoomID = process.env['ROCKETCHAT_ROOM_ID'] || 'WIJbAHNEdgMNavYM5'; // Rocket.Chat federated room id | ||
const HomeServerSenderLocal = process.env['HOMESERVER_SENDER_LOCAL'] || 'rcbot'; | ||
const IncomingPort = process.env['INCOMING_PORT'] || 8089; | ||
const RocketChatUserPrefix = process.env['ROCKETCHAT_USER_PREFIX'] || '@rocketchat_'; // user name prefix for Rocket.Chat users | ||
const WritableConfigPath = process.env['WRITABLE_CONFIG_PATH'] || './config'; // path of directory to write configuarion file | ||
|
||
const bridgeHelpers = require('./bridge')(HomeServerDomain, RocketChatUserPrefix, HomeServerURL) | ||
const handleMatrixMessage = bridgeHelpers.handleMatrixMessage; | ||
const handleRocketChatMessage = bridgeHelpers.handleRocketChatMessage; | ||
const ROOM_MAP = bridgeHelpers.maps({HomeServerRoomID: HomeServerRoomID, RocketChatRoomID: RocketChatRoomID}); | ||
|
||
|
||
module.exports = function(robot) { | ||
|
||
const regFileFullPath = WritableConfigPath + '/' + 'rocketchat-registration.yaml'; | ||
// start the matrix bridge and pass in our request handler | ||
matrixBridge.run(IncomingPort, { hsurl : HomeServerURL, domain: HomeServerDomain, registration: regFileFullPath}, { | ||
onMessage:(event, rocketRoomId, matrixRoomId) => { | ||
handleMatrixMessage(event, robot, matrixBridge, rocketRoomId, matrixRoomId) | ||
} | ||
}, ROOM_MAP).then(() => { | ||
// messages coming from Rocket.Chat room(s) monitored | ||
robot.hear(/.*/, (msg) => { | ||
const room = ROOM_MAP.findIndex((ids) => msg.message.room == ids[0]) | ||
if(room < 0 ) { return } | ||
return handleRocketChatMessage(msg, ROOM_MAP[room][1], matrixBridge) | ||
}) | ||
ROOM_MAP.forEach(ids => console.log(`Mapping Rocketchat(${ ids[0] }) <==> Matrix(${ ids[1] })`)) | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters