1.0.9 • Published 1 year ago
matrix-revolt-parser v1.0.9
matrix-revolt-parser
This package is a message parser for sending messages between Matrix and
Revolt. For that, it has two parsers: RevoltMessageParser and
MatrixMessageParser.
This is a fork of @deurstann/matrix-discord-parser, itself a fork of matrix-discord-parser.
RevoltMessageParser
Example code:
import { RevoltMessageParser, IRevoltMessageParserOpts } from "matrix-revolt-parser";
const parser = new RevoltMessageParser();
const opts = {
callbacks: {
getUser: async (id: string) => null,
getChannel: async (id: string) => null,
getEmoji: async (id: string) => null;
},
} as IRevoltMessageParserOpts;
const message = msg; // Type Message from revolt.js
const result = await parser.FormatMessage(opts, msg);
console.log(result.body); // the body of the matrix message
console.log(result.formattedBody); // the formatted body of the matrix message
console.log(result.msgtype); // the msgtype of the matrix messageAll options of IRevoltMessageParserOpts:
callbacks:IRevoltMessageParserCallbacks, the callbacks to handlegetUser:async (id: string) => Promise<IRevoltMessageParserEntity | null>, resolves to either the information on the specified Revolt user or to nullgetChannel:async (id: string) => Promise<IRevoltMessageParserEntity | null>, resolves to either the information of the specified Revolt channel or to nullgetEmoji:async (id: string) => Promise<IRevoltEmojiEntity | null>, resolves to either the mxc uri of the specified Revolt emoji or to null
All properties of IRevoltMessageParserEntity:
name:string, the name of the entitymxid:string, the resulting matrix ID of the entity
All properties of IRevoltEmojiEntity:
name:string, the name of the emojimxc:string, the MXC URI for the emoji
All properties of IRevoltMessageParserResult:
body:string, the body of the resultformattedBody:string, the formatted (html) body of the resultmsgtype:string, the matrix msgtype of the result
MatrixMessageParser
Example code:
import { MatrixMessageParser, IMatrixMessageParserOpts } from "matrix-revolt-parser";
const parser = new MatrixMessageParser();
const opts = {
callbacks: {
getUserId: async (mxid: string) => null,
getChannelId: async (mxid: string) => null,
getEmoji: async (mxc: string, name: string) => null,
mxcUrlToHttp: async (mxc: string) => "http://example.com",
},
displayname: "Alice",
determineCodeLanguage: true,
} as IMatrixMessageParserOpts;
const msg = { // raw matrix event content
msgtype: "m.text",
body: "**blah**",
format: "org.matrix.custom.html",
formatted_body: "<strong>blah</strong>",
};
const parsed = await parser.FormatMessage(opts, msg);
msg.send(parsed); // send this message to discordAll options of IMatrixMessageParserOpts:
callbacks:IMatrixMessageParserCallbacks, the callbacks to handlegetUserId:async (mxid: string) => Promise<string | null>, return the discord user ID given an mxid, or nullgetChannelId:async (mxid: string) => Promise<string | null>, return the discord channel ID given an mxid, or nullgetEmoji:async (mxc: string, name: string) => Promise<string | null>, return a Revolt emoji ID given an mxc uri and a name, or nullmxcUrlToHttp:async (mxc: string) => Promise<string>, resolve an mxc uri to a publicly available http url.
displayname:string, the display name of the sender of the message (used form.emoteparsing)determineCodeLanguage:Boolean(defaultfalse), whether the language of code-blocks should be auto-determined, if not specified
Returned is a Revolt-formatted string, ready to be sent.