Hallo,
ich habe mit Hilfe der "matrix-js-sdk" ein einfaches Typescript erstellt was Nachrichten in einen Raum
innerhalb des matrix.org Netzwerk schicken
namespace matrixClient {
const matrixJsSdk = require('matrix-js-sdk')
const configPrefix:string = '0_userdata.0.matrix'
const serverUrl:string = getState(configPrefix + 'Url'/*matrixUrl*/).val
const testRoomId:string = getState(configPrefix + 'Room'/*matrixRoom*/).val
const loginData:object = JSON.parse(getState(configPrefix + 'Login'/*matrixLogin*/).val)
const matrixMessage: string = 'matrixMessage'
const eventType:string = 'm.room.message'
const messageType:string = 'm.text'
const matrixClient = matrixJsSdk.createClient(serverUrl)
const eventReceiverCallback = function(event, room, toStartOfTimeline):void {
if (event.getType() != eventType &&
event.getRoomId() != testRoomId &&
event.getContent().msgtype != messageType) {
return;
}
log(event.getContent().body)
}
const sendMessageToMatrix = function (data: string): any {
const content: object = {
"msgtype": messageType,
"body": data
}
matrixClient.sendEvent(testRoomId, eventType, content, "").then((res) => {
}).catch((err) => {
log(err)
})
return(Date.now())
}
onStop(() => {
onMessageUnregister(matrixMessage)
matrixClient.stopClient()
matrixClient.logout()
})
matrixClient.login("m.login.password", loginData).then((response) => {
// matrixClient.initCrypto()
matrixClient.startClient()
matrixClient.once('sync', function (state, prevState, res) {
console.log(state) // state will be 'PREPARED' when the client is ready to use
matrixClient.on("Room.timeline", eventReceiverCallback)
onMessage(matrixMessage, sendMessageToMatrix)
})
})
}
zum Senden über diesen Client benutze ich die folgende Funktion, die bei mir in global liegt und damit allen scripts zur Verfügung steht
/**
* send message to matrix messager
*
* @param message Message to send
*/
const sendMessageToMatrix = function(message:string, prefix:string = scriptName + ": ") {
if(isScriptActive('script.js.test.matrix-client')) {
messageTo('matrixMessage', prefix + message)
}
}
Vielleicht kann jemand etwas damit angefangen
VG,
powo01