Skip to content

Commit dd61e0e

Browse files
committed
feat: telegram formatted text
1 parent bff204f commit dd61e0e

File tree

5 files changed

+46
-36
lines changed

5 files changed

+46
-36
lines changed

apps/frontend/src/components/new-launch/editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ export const Editor: FC<{
524524
editor={editorRef?.current?.editor}
525525
currentValue={props.value!}
526526
/>
527-
{(editorType === 'markdown' || editorType === 'html') && (
527+
{(editorType === 'markdown' || editorType === 'html') && identifier !== 'telegram' && (
528528
<>
529529
<Bullets
530530
editor={editorRef?.current?.editor}

libraries/helpers/src/utils/strip.html.validation.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import striptags from 'striptags';
2-
import { NodeHtmlMarkdown } from 'node-html-markdown';
2+
import TurndownService from 'turndown';
3+
const turndownService = new TurndownService();
34

45
const bold = {
56
a: '𝗮',
@@ -153,7 +154,7 @@ export const stripHtmlValidation = (
153154
}
154155

155156
if (type === 'markdown') {
156-
return NodeHtmlMarkdown.translate(value);
157+
return turndownService.turndown(value);
157158
}
158159

159160
if (value.indexOf('<p>') === -1 && !none) {

libraries/nestjs-libraries/src/integrations/social/telegram.provider.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { SocialAbstract } from '@gitroom/nestjs-libraries/integrations/social.ab
1111
import mime from 'mime';
1212
import TelegramBot from 'node-telegram-bot-api';
1313
import { Integration } from '@prisma/client';
14+
import striptags from 'striptags';
1415

1516
const telegramBot = new TelegramBot(process.env.TELEGRAM_TOKEN!);
1617
// Added to support local storage posting
@@ -23,7 +24,7 @@ export class TelegramProvider extends SocialAbstract implements SocialProvider {
2324
isBetweenSteps = false;
2425
isWeb3 = true;
2526
scopes = [] as string[];
26-
editor = 'markdown' as const;
27+
editor = 'html' as const;
2728

2829
async refreshToken(refresh_token: string): Promise<AuthTokenDetails> {
2930
return {
@@ -145,7 +146,14 @@ export class TelegramProvider extends SocialAbstract implements SocialProvider {
145146
for (const message of postDetails) {
146147
let messageId: number | null = null;
147148
const mediaFiles = message.media || [];
148-
const text = message.message || '';
149+
const text = striptags(message.message || '', [
150+
'u',
151+
'strong',
152+
'p',
153+
])
154+
.replace(/<strong>/g, '<b>')
155+
.replace(/<\/strong>/g, '</b>')
156+
.replace(/<p>(.*?)<\/p>/g, '$1\n')
149157
// check if media is local to modify url
150158
const processedMedia = mediaFiles.map((media) => {
151159
let mediaUrl = media.path;
@@ -176,7 +184,9 @@ export class TelegramProvider extends SocialAbstract implements SocialProvider {
176184
});
177185
// if there's no media, bot sends a text message only
178186
if (processedMedia.length === 0) {
179-
const response = await telegramBot.sendMessage(accessToken, text);
187+
const response = await telegramBot.sendMessage(accessToken, text, {
188+
parse_mode: 'HTML',
189+
});
180190
messageId = response.message_id;
181191
}
182192
// if there's only one media, bot sends the media with the text message as caption
@@ -187,20 +197,20 @@ export class TelegramProvider extends SocialAbstract implements SocialProvider {
187197
? await telegramBot.sendVideo(
188198
accessToken,
189199
media.media,
190-
{ caption: text, parse_mode: 'Markdown' },
200+
{ caption: text, parse_mode: 'HTML' },
191201
media.fileOptions
192202
)
193203
: media.type === 'photo'
194204
? await telegramBot.sendPhoto(
195205
accessToken,
196206
media.media,
197-
{ caption: text, parse_mode: 'Markdown' },
207+
{ caption: text, parse_mode: 'HTML' },
198208
media.fileOptions
199209
)
200210
: await telegramBot.sendDocument(
201211
accessToken,
202212
media.media,
203-
{ caption: text, parse_mode: 'Markdown' },
213+
{ caption: text, parse_mode: 'HTML' },
204214
media.fileOptions
205215
);
206216
messageId = response.message_id;
@@ -213,7 +223,7 @@ export class TelegramProvider extends SocialAbstract implements SocialProvider {
213223
type: m.type === 'document' ? 'document' : m.type, // Documents are not allowed in media groups
214224
media: m.media,
215225
caption: i === 0 && index === 0 ? text : undefined,
216-
parse_mode: 'Markdown'
226+
parse_mode: 'HTML',
217227
}));
218228

219229
const response = await telegramBot.sendMediaGroup(

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
"@types/sha256": "^0.2.2",
110110
"@types/stripe": "^8.0.417",
111111
"@types/striptags": "^0.0.5",
112+
"@types/turndown": "^5.0.5",
112113
"@types/yup": "^0.32.0",
113114
"@uidotdev/usehooks": "^2.4.1",
114115
"@uiw/react-md-editor": "^4.0.3",
@@ -167,7 +168,6 @@
167168
"next": "^14.2.30",
168169
"next-plausible": "^3.12.0",
169170
"node-fetch": "^3.3.2",
170-
"node-html-markdown": "^1.3.0",
171171
"node-telegram-bot-api": "^0.66.0",
172172
"nodemailer": "^6.9.15",
173173
"nostr-tools": "^2.10.4",
@@ -213,6 +213,7 @@
213213
"tldts": "^6.1.47",
214214
"transloadit": "^3.0.2",
215215
"tslib": "^2.3.0",
216+
"turndown": "^7.2.0",
216217
"tweetnacl": "^1.0.3",
217218
"twitter-api-v2": "^1.24.0",
218219
"twitter-text": "^3.1.0",

pnpm-lock.yaml

Lines changed: 23 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)