Reformat Telegram /commands to !commands
This commit is contained in:
+17
-2
@@ -68,9 +68,10 @@ function telegramToMatrix(message, entities, app) {
|
|||||||
addSimpleTag(tags, entity, "pre", pc)
|
addSimpleTag(tags, entity, "pre", pc)
|
||||||
addTag(tags, entity, "code", `class="language-${entity.language}"`, pc + 1)
|
addTag(tags, entity, "code", `class="language-${entity.language}"`, pc + 1)
|
||||||
break
|
break
|
||||||
case "messageEntityHashtag":
|
|
||||||
case "messageEntityBotCommand":
|
case "messageEntityBotCommand":
|
||||||
// TODO bridge bot commands differently?
|
// TODO bridge bot commands differently?
|
||||||
|
message = `${message.substr(0, entity.offset)}!${message.substr(entity.offset + 1)}`
|
||||||
|
case "messageEntityHashtag":
|
||||||
addTag(tags, entity, "font", "color=\"blue\"", --pc)
|
addTag(tags, entity, "font", "color=\"blue\"", --pc)
|
||||||
break
|
break
|
||||||
case "messageEntityMentionName":
|
case "messageEntityMentionName":
|
||||||
@@ -153,6 +154,7 @@ const inlineCode = /<(code)>()(.*?)<\/code>/g
|
|||||||
const emailAddresses = /<a href="(mailto):(.*?)">([^]*?)<\/a>/g
|
const emailAddresses = /<a href="(mailto):(.*?)">([^]*?)<\/a>/g
|
||||||
const mentions = /<a href="https:\/\/(matrix\.to)\/#\/(@.+?)">(.*?)<\/a>/g
|
const mentions = /<a href="https:\/\/(matrix\.to)\/#\/(@.+?)">(.*?)<\/a>/g
|
||||||
const hyperlinks = /<(a href)="(.*?)">([^]*?)<\/a>/g
|
const hyperlinks = /<(a href)="(.*?)">([^]*?)<\/a>/g
|
||||||
|
const commands = /(\s|^)!([^\s]+)/g
|
||||||
const REGEX_CAPTURE_GROUP_COUNT = 3
|
const REGEX_CAPTURE_GROUP_COUNT = 3
|
||||||
|
|
||||||
RegExp.any = function(...regexes) {
|
RegExp.any = function(...regexes) {
|
||||||
@@ -263,9 +265,22 @@ function regexMonsterHandler(identifier, arg, text, index, app) {
|
|||||||
* @param {string} message The HTML-formatted message.
|
* @param {string} message The HTML-formatted message.
|
||||||
* @returns {{message: string, entities: Array}} The Telegram entity-formatted message.
|
* @returns {{message: string, entities: Array}} The Telegram entity-formatted message.
|
||||||
*/
|
*/
|
||||||
function matrixToTelegram(message, app) {
|
function matrixToTelegram(message, isHTML, app) {
|
||||||
const entities = []
|
const entities = []
|
||||||
|
|
||||||
|
message = message.replace(commands, (_, prefix, command, index) => {
|
||||||
|
entities.push({
|
||||||
|
_: "messageEntityBotCommand",
|
||||||
|
offset: index + prefix.length,
|
||||||
|
length: command.length + 1,
|
||||||
|
})
|
||||||
|
return `${prefix}/${command}`
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!isHTML) {
|
||||||
|
return { message, entities }
|
||||||
|
}
|
||||||
|
|
||||||
// First replace all the things that don't get converted into Telegram entities
|
// First replace all the things that don't get converted into Telegram entities
|
||||||
message = message.replace(linebreaks, "\n")
|
message = message.replace(linebreaks, "\n")
|
||||||
message = message.replace(paragraphs, "$1\n")
|
message = message.replace(paragraphs, "$1\n")
|
||||||
|
|||||||
+5
-6
@@ -251,12 +251,11 @@ class Portal {
|
|||||||
await this.loadAccessHash(sender.telegramPuppet)
|
await this.loadAccessHash(sender.telegramPuppet)
|
||||||
switch (evt.content.msgtype) {
|
switch (evt.content.msgtype) {
|
||||||
case "m.text":
|
case "m.text":
|
||||||
if (evt.content.format === "org.matrix.custom.html") {
|
const { message, entities } = formatter.matrixToTelegram(
|
||||||
const { message, entities } = formatter.matrixToTelegram(evt.content.formatted_body, this.app)
|
evt.content.formatted_body || evt.content.body,
|
||||||
await sender.telegramPuppet.sendMessage(this.peer, message, entities)
|
evt.content.format === "org.matrix.custom.html",
|
||||||
} else {
|
this.app)
|
||||||
await sender.telegramPuppet.sendMessage(this.peer, evt.content.body)
|
await sender.telegramPuppet.sendMessage(this.peer, message, entities)
|
||||||
}
|
|
||||||
break
|
break
|
||||||
case "m.video":
|
case "m.video":
|
||||||
case "m.audio":
|
case "m.audio":
|
||||||
|
|||||||
Reference in New Issue
Block a user