Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions lib/platforms/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,10 @@ const setMessageId = (message, messageId) => ({
}
});

// convert to the weird dialect of Telegram
const convertMarkdown = content => content
.replace(/\*\*(.*?)\*\*/g, '--$1--') // tmp replacement
.replace(/\*(.*?)\*/g, '_$1_')
.replace(/\-\-(.*?)\-\-/g, '*$1*');

const MESSAGE_MAX_SIZE = 4096;
// rich messages support a much larger payload than regular messages
// (up to 32768 UTF-8 bytes / 500 top-level blocks)
const MESSAGE_RICH_MAX_SIZE = 32768;

const Telegram = new ChatExpress({
color: '#336699',
Expand Down Expand Up @@ -808,10 +805,7 @@ Telegram.out('message', async function(message) {
const modifyMessageId = param('modifyMessageId') || message.originalMessage.modifyMessageId; // for retro compatibility

let content = message.payload.content;
// convert the different markdown dialect of Telegram
if (param('parseMode', null) === 'Markdown' && !_.isEmpty(content)) {
content = convertMarkdown(content);
}
const richMessaging = param('richMessaging', false);

let result = null;
if (modifyMessageId != null) {
Expand All @@ -820,6 +814,23 @@ Telegram.out('message', async function(message) {
message_id: modifyMessageId,
disable_notification: param('silent', false)
});
} else if (richMessaging) {
// rich messages allow a much larger payload, so chunk accordingly
const chunks = utils.split(content, MESSAGE_RICH_MAX_SIZE);
const parseMode = param('parseMode', null);
for(let idx = 0; idx < chunks.length; idx++) {
// InputRichMessage carries the formatting flavour: HTML or markdown
const richMessage = parseMode === 'HTML'
? { html: chunks[idx] }
: { markdown: chunks[idx] };
result = await connector.sendRichMessage(message.payload.chatId, richMessage, {
disable_notification: param('silent', false),
reply_parameters: param('replyToMessage', false) ? { message_id: getMessageId(message) } : undefined,
protect_content: param('protectContent', false),
allow_paid_broadcast: param('allowPaidBroadcast', false),
message_effect_id: param('messageEffectId', null)
});
}
} else {
const chunks = utils.split(content, MESSAGE_MAX_SIZE);
const hasSpoiler = param('hasSpoiler', false);
Expand Down Expand Up @@ -1132,6 +1143,15 @@ Telegram.registerParam(
'boolean',
{ label: 'Reply to message', default: false }
);
Telegram.registerParam(
'richMessaging',
'boolean',
{
label: 'Rich Messaging',
default: false,
description: 'Send the message with sendRichMessage, allowing bots to send rich messages with highly structured text (tables, headings, code blocks, collapsible details) and a much larger payload (up to 32768 UTF-8 bytes) than regular messages'
}
);
Telegram.registerParam(
'allowPaidBroadcast',
'boolean',
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"node-emoji": "^1.3.1",
"node-fetch": "^2.6.0",
"node-nlp": "^4.26.1",
"node-telegram-bot-api": "^1.1.0",
"node-telegram-bot-api": "^1.1.2",
"notion-to-md": "^2.4.1",
"openai": "^6.36.0",
"passport": "^0.4.1",
Expand Down
48 changes: 28 additions & 20 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2233,15 +2233,10 @@
mkdirp "^1.0.4"
rimraf "^3.0.2"

"@parcel/watcher-linux-x64-glibc@2.5.6":
"@parcel/watcher-darwin-arm64@2.5.6":
version "2.5.6"
resolved "https://registry.npmjs.org/@parcel/watcher-linux-x64-glibc/-/watcher-linux-x64-glibc-2.5.6.tgz"
integrity sha512-kbT5wvNQlx7NaGjzPFu8nVIW1rWqV780O7ZtkjuWaPUgpv2NMFpjYERVi0UYj1msZNyCzGlaCWEtzc+exjMGbQ==

"@parcel/watcher-linux-x64-musl@2.5.6":
version "2.5.6"
resolved "https://registry.npmjs.org/@parcel/watcher-linux-x64-musl/-/watcher-linux-x64-musl-2.5.6.tgz"
integrity sha512-1JRFeC+h7RdXwldHzTsmdtYR/Ku8SylLgTU/reMuqdVD7CtLwf0VR1FqeprZ0eHQkO0vqsbvFLXUmYm/uNKJBg==
resolved "https://registry.npmjs.org/@parcel/watcher-darwin-arm64/-/watcher-darwin-arm64-2.5.6.tgz"
integrity sha512-Z2ZdrnwyXvvvdtRHLmM4knydIdU9adO3D4n/0cVipF3rRiwP+3/sfzpAwA/qKFL6i1ModaabkU7IbpeMBgiVEA==

"@parcel/watcher@^2.4.1":
version "2.5.6"
Expand Down Expand Up @@ -2901,15 +2896,10 @@
resolved "https://registry.npmjs.org/@ungap/structured-clone/-/structured-clone-1.3.1.tgz"
integrity sha512-mUFwbeTqrVgDQxFveS+df2yfap6iuP20NAKAsBt5jDEoOTDew+zwLAOilHCeQJOVSvmgCX4ogqIrA0mnyr08yQ==

"@unrs/resolver-binding-linux-x64-gnu@1.12.2":
version "1.12.2"
resolved "https://registry.npmjs.org/@unrs/resolver-binding-linux-x64-gnu/-/resolver-binding-linux-x64-gnu-1.12.2.tgz"
integrity sha512-mPsUhunKKDih5O96Y6enDQyHc1SqBPlY1E/SfMWDM3EdJ95Z9CArPeCVwCCqbP45ljvivdEk8Fxn+SIb1rDAJQ==

"@unrs/resolver-binding-linux-x64-musl@1.12.2":
"@unrs/resolver-binding-darwin-arm64@1.12.2":
version "1.12.2"
resolved "https://registry.npmjs.org/@unrs/resolver-binding-linux-x64-musl/-/resolver-binding-linux-x64-musl-1.12.2.tgz"
integrity sha512-azrt6+5ydLd8Vt210AAFis/lZevSfPw93EJRIJG+xPu4WCJ8K0kppCTpMyLPcKT7H15M4Jnt2tMp5bOvCkRC6A==
resolved "https://registry.npmjs.org/@unrs/resolver-binding-darwin-arm64/-/resolver-binding-darwin-arm64-1.12.2.tgz"
integrity sha512-u9DiNT1auQMO20A9SyTuG3wUgQWB9Z7KjAg0uFuCDR1FsAY8A0CG2S6JpHS1xwm/w1G08bjXZDcyOCjv1WAm2w==

"@webassemblyjs/ast@1.11.1":
version "1.11.1"
Expand Down Expand Up @@ -7051,6 +7041,19 @@ fs.realpath@^1.0.0:
resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz"
integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8= sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==

fsevents@^1.2.7:
version "1.2.13"
resolved "https://registry.npmjs.org/fsevents/-/fsevents-1.2.13.tgz"
integrity sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==
dependencies:
bindings "^1.5.0"
nan "^2.12.1"

fsevents@^2.3.3, fsevents@~2.3.2:
version "2.3.3"
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz"
integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==

ftp@^0.3.10:
version "0.3.10"
resolved "https://registry.npmjs.org/ftp/-/ftp-0.3.10.tgz"
Expand Down Expand Up @@ -10591,6 +10594,11 @@ mute-stream@0.0.8:
resolved "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.8.tgz"
integrity sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==

nan@^2.12.1:
version "2.16.0"
resolved "https://registry.npmjs.org/nan/-/nan-2.16.0.tgz"
integrity sha512-UdAqHyFngu7TfQKsCBgAA6pWDkT8MAO7d0jyOecVhN5354xbLqdn8mV9Tat9gepAupm0bt2DbeaSC8vS52MuFA==

nanomatch@^1.2.9:
version "1.2.13"
resolved "https://registry.npmjs.org/nanomatch/-/nanomatch-1.2.13.tgz"
Expand Down Expand Up @@ -10780,10 +10788,10 @@ node-releases@^2.0.36:
resolved "https://registry.npmjs.org/node-releases/-/node-releases-2.0.46.tgz"
integrity sha512-GYVXHE2KnrzAfsAjl4uP++evGFCrAU1jta4ubEjIG7YWt/64Gqv66a30yKwWczVjA6j3bM4nBwH7Pk1JmDHaxQ==

node-telegram-bot-api@^1.1.0:
version "1.1.0"
resolved "https://registry.npmjs.org/node-telegram-bot-api/-/node-telegram-bot-api-1.1.0.tgz"
integrity sha512-kNBTBlYVP+OaBb8ffn3zsIC4CTJqpnRniNS9VQRURQrBwYSJE1Ip7yHM/JKw36qUcQtu0bxbXAs2yMNk8HXpdA==
node-telegram-bot-api@^1.1.2:
version "1.1.2"
resolved "https://registry.npmjs.org/node-telegram-bot-api/-/node-telegram-bot-api-1.1.2.tgz"
integrity sha512-PtLczp0WZCQz7LHsObl1WIyfMSTco58cfYXITqB4Fbeuybd/A/sM729h892fbcjz+szJ9lpu+/pfewUs9NObrg==

nopt@^5.0.0:
version "5.0.0"
Expand Down
Loading