initial commit

This commit is contained in:
ale 2021-06-30 18:13:00 +02:00
commit 1048f38e20
4 changed files with 95 additions and 0 deletions

13
README.md Normal file
View File

@ -0,0 +1,13 @@
# Matrix Gateway Bot written in NodeJS
## Install
npm i or yarn
## Configure
Edit config.js
## Run
node index

9
config.js Normal file
View File

@ -0,0 +1,9 @@
module.exports = {
nick: 'MatrixBot',
xmppService: 'xmpp://',
xmppUsername: '',
xmppPassword: '',
xmppRoom: '',
matrixServer: '',
matrixRoom: ''
}

59
index.js Normal file
View File

@ -0,0 +1,59 @@
const { client, xml } = require('@xmpp/client'),
sdk = require('matrix-js-sdk'),
config = require('./config'),
xmpp = client({
service: config.xmppService,
username: config.xmppUsername,
password: config.xmppPassword
}),
sendMessage = async message => {
await xmpp.send(xml(
'message',
{ type: 'groupchat', to: xmppRoom },
xml('body', {}, message)
))
},
nick = config.nick,
token = config.matrixToken,
matrix = sdk.createClient({
baseUrl: 'https://' + config.matrixServer,
accessToken: token,
userId: '@' + nick + ':' + config.matrixServer
}),
xmppRoom = config.xmppRoom,
matrixRoom = config.matrixRoom
xmpp.on('error', err => {
console.error(err)
})
xmpp.on('offline', () => {
console.log('Go to offline')
})
xmpp.on('stanza', async stanza => {
if (stanza.is('message') && !stanza.getChild('delay')) {
const user = stanza.attrs.from.replace(/.*\//, ''),
message = stanza.getChildText('body')
if (user && user !== nick && message) {
await matrix.sendMessage(matrixRoom, { body: user + ': ' + message, msgtype: 'm.text' }).catch(console.error)
}
}
})
xmpp.on('online', async address => {
await xmpp.send(xml('presence', { to: xmppRoom + '/' + nick },
xml('x', { xmlns: 'http://jabber.org/protocol/muc' }
))).catch(console.error)
console.log('Connected like: ' + address)
matrix.on('Room.timeline', (event, room, toStartOfTimeline) => {
if (event.event.sender !== '@' + nick + ':' + config.matrixServer && event.getType() === 'm.room.message') {
sendMessage(event.event.sender.replace(/:.*$/, '') + ': ' + event.event.content.body)
}
})
await matrix.setDisplayName(nick)
await matrix.joinRoom(matrixRoom).catch(console.error)
matrix.startClient()
})
xmpp.start().catch(console.error)

14
package.json Normal file
View File

@ -0,0 +1,14 @@
{
"name": "matrix-bot",
"version": "1.0.0",
"description": "Matrix Gateway Bot",
"main": "index.js",
"repository": "https://git.manalejandro.com/ale/matrix-bot",
"author": "ale",
"license": "MIT",
"private": true,
"dependencies": {
"@xmpp/client": "*",
"matrix-js-sdk": "*"
}
}