mirror of
https://github.com/matrix-org/matrix-hookshot.git
synced 2025-03-10 21:19:13 +00:00
Add MatrixSender class for sending messages
This commit is contained in:
parent
665214e52f
commit
6c68bc0276
18
src/App/MatrixSenderApp.ts
Normal file
18
src/App/MatrixSenderApp.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { parseConfig, parseRegistrationFile } from "../Config";
|
||||
import { MatrixSender } from "../MatrixSender";
|
||||
import { LogWrapper } from "../LogWrapper";
|
||||
|
||||
const log = new LogWrapper("App");
|
||||
|
||||
async function start() {
|
||||
const configFile = process.argv[2] || "./config.yml";
|
||||
const registrationFile = process.argv[3] || "./registration.yml";
|
||||
const config = await parseConfig(configFile, process.env);
|
||||
const registration = await parseRegistrationFile(registrationFile);
|
||||
LogWrapper.configureLogging(config.logging.level);
|
||||
new MatrixSender(config, registration).listen();
|
||||
}
|
||||
|
||||
start().catch((ex) => {
|
||||
log.error("MatrixSenderApp encountered an error and has stopped:", ex);
|
||||
});
|
58
src/MatrixSender.ts
Normal file
58
src/MatrixSender.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import { BridgeConfig } from "./Config";
|
||||
import { MessageQueue, createMessageQueue } from "./MessageQueue/MessageQueue";
|
||||
import { MatrixEventContent } from "./MatrixEvent";
|
||||
import { Appservice, IAppserviceRegistration } from "matrix-bot-sdk";
|
||||
import { LogWrapper } from "./LogWrapper";
|
||||
|
||||
export interface IMatrixSendMessage {
|
||||
sender: string|null;
|
||||
type: string;
|
||||
roomId: string;
|
||||
content: MatrixEventContent;
|
||||
}
|
||||
|
||||
export interface IMatrixSendMessageResponse {
|
||||
eventId: string;
|
||||
}
|
||||
|
||||
const log = new LogWrapper("MatrixSender");
|
||||
|
||||
export class MatrixSender {
|
||||
private mq: MessageQueue;
|
||||
private as: Appservice;
|
||||
constructor(private config: BridgeConfig, registration: IAppserviceRegistration) {
|
||||
this.mq = createMessageQueue(this.config);
|
||||
this.as = new Appservice({
|
||||
homeserverName: this.config.bridge.domain,
|
||||
homeserverUrl: this.config.bridge.url,
|
||||
port: 0,
|
||||
bindAddress: "",
|
||||
registration,
|
||||
});
|
||||
}
|
||||
|
||||
public listen() {
|
||||
this.mq.subscribe("matrix.message");
|
||||
this.mq.on<IMatrixSendMessage>("matrix.message", async (msg) => {
|
||||
try {
|
||||
await this.sendMatrixMessage(msg.messageId!, msg.data);
|
||||
} catch (ex) {
|
||||
log.error(`Failed to send message (${msg.data.roomId}, ${msg.data.sender}, ${msg.data.type})`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async sendMatrixMessage(messageId: string, msg: IMatrixSendMessage) {
|
||||
const intent = msg.sender ? this.as.getIntentForUserId(msg.sender) : this.as.botIntent;
|
||||
const eventId = await intent.underlyingClient.sendEvent(msg.roomId, msg.type, msg.content);
|
||||
log.info("Sent", eventId);
|
||||
this.mq.push<IMatrixSendMessageResponse>({
|
||||
eventName: "response.matrix.message",
|
||||
sender: "MatrixSender",
|
||||
data: {
|
||||
eventId,
|
||||
},
|
||||
messageId,
|
||||
});
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user