Reformat Telegram /commands to !commands

This commit is contained in:
Tulir Asokan
2017-12-02 13:54:49 +02:00
parent a1ec408b5b
commit 2bda80526c
2 changed files with 22 additions and 8 deletions
+17 -2
View File
@@ -68,9 +68,10 @@ function telegramToMatrix(message, entities, app) {
addSimpleTag(tags, entity, "pre", pc)
addTag(tags, entity, "code", `class="language-${entity.language}"`, pc + 1)
break
case "messageEntityHashtag":
case "messageEntityBotCommand":
// 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)
break
case "messageEntityMentionName":
@@ -153,6 +154,7 @@ const inlineCode = /<(code)>()(.*?)<\/code>/g
const emailAddresses = /<a href="(mailto):(.*?)">([^]*?)<\/a>/g
const mentions = /<a href="https:\/\/(matrix\.to)\/#\/(@.+?)">(.*?)<\/a>/g
const hyperlinks = /<(a href)="(.*?)">([^]*?)<\/a>/g
const commands = /(\s|^)!([^\s]+)/g
const REGEX_CAPTURE_GROUP_COUNT = 3
RegExp.any = function(...regexes) {
@@ -263,9 +265,22 @@ function regexMonsterHandler(identifier, arg, text, index, app) {
* @param {string} message The HTML-formatted message.
* @returns {{message: string, entities: Array}} The Telegram entity-formatted message.
*/
function matrixToTelegram(message, app) {
function matrixToTelegram(message, isHTML, app) {
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
message = message.replace(linebreaks, "\n")
message = message.replace(paragraphs, "$1\n")
+5 -6
View File
@@ -251,12 +251,11 @@ class Portal {
await this.loadAccessHash(sender.telegramPuppet)
switch (evt.content.msgtype) {
case "m.text":
if (evt.content.format === "org.matrix.custom.html") {
const { message, entities } = formatter.matrixToTelegram(evt.content.formatted_body, this.app)
await sender.telegramPuppet.sendMessage(this.peer, message, entities)
} else {
await sender.telegramPuppet.sendMessage(this.peer, evt.content.body)
}
const { message, entities } = formatter.matrixToTelegram(
evt.content.formatted_body || evt.content.body,
evt.content.format === "org.matrix.custom.html",
this.app)
await sender.telegramPuppet.sendMessage(this.peer, message, entities)
break
case "m.video":
case "m.audio":