mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-24 22:46:41 +07:00
added plaintext fallback for markdown
This commit is contained in:
parent
1b0a1f20d5
commit
ef4794a33f
8 changed files with 161 additions and 68 deletions
|
@ -73,10 +73,14 @@ export class TelegramService {
|
|||
public sendMessageToChan = async (
|
||||
channel: string,
|
||||
message: string,
|
||||
markdown?: boolean,
|
||||
extra?: ExtraReplyMessage
|
||||
) => {
|
||||
logger.debug(`sending message "${message}" to chan "${channel}"`);
|
||||
return await this.bot.telegram.sendMessage(channel, message, extra);
|
||||
return await this.bot.telegram.sendMessage(channel, message, {
|
||||
...extra,
|
||||
...(markdown ? { parse_mode: "Markdown" } : {}),
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -86,12 +90,14 @@ export class TelegramService {
|
|||
channel: string,
|
||||
caption: string,
|
||||
src: string,
|
||||
markdown?: boolean,
|
||||
extra?: ExtraReplyMessage
|
||||
) => {
|
||||
logger.debug(`sending photo message "${caption}" to chan "${channel}"`);
|
||||
return await this.bot.telegram.sendPhoto(channel, src, {
|
||||
...extra,
|
||||
caption,
|
||||
...(markdown ? { parse_mode: "Markdown" } : {}),
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ import toVFile from "to-vfile";
|
|||
import path from "path";
|
||||
import hb from "handlebars";
|
||||
import strip from "strip-markdown";
|
||||
import { VFileCompatible } from "vfile";
|
||||
|
||||
const removeFrontmatter = () => (tree) => {
|
||||
tree.children = tree.children.filter((item) => item.type !== "yaml");
|
||||
|
@ -20,25 +21,26 @@ export class Template<
|
|||
public fields: F = {} as F;
|
||||
public template: string = "";
|
||||
|
||||
private readonly file: VFileCompatible = "";
|
||||
|
||||
constructor(filename: string) {
|
||||
try {
|
||||
if (!filename) {
|
||||
return;
|
||||
}
|
||||
|
||||
// read file and fields from it to this.fields
|
||||
|
||||
const processor = unified()
|
||||
.use(stringify)
|
||||
.use(frontmatter)
|
||||
.use(extract, { yaml: parse })
|
||||
.use(removeFrontmatter)
|
||||
.use(parser)
|
||||
.use(strip);
|
||||
|
||||
const file = toVFile.readSync(path.join(__dirname, "../../", filename));
|
||||
const result = processor.processSync(file);
|
||||
.use(parser);
|
||||
|
||||
this.file = toVFile.readSync(path.join(__dirname, "../../", filename));
|
||||
const result = processor.processSync(this.file);
|
||||
this.fields = result.data as F;
|
||||
this.template = result.contents.toString().trim();
|
||||
} catch (e) {
|
||||
throw new Error(`Template: ${e.toString()}`);
|
||||
}
|
||||
|
@ -47,8 +49,23 @@ export class Template<
|
|||
/**
|
||||
* Themes the template with values
|
||||
*/
|
||||
public theme = (values: V) => {
|
||||
return hb.compile(this.template)(values).replace(/\n+/g, "\n\n");
|
||||
public theme = (values: V, markdown?: boolean) => {
|
||||
const processor = unified()
|
||||
.use(stringify)
|
||||
.use(frontmatter)
|
||||
.use(removeFrontmatter)
|
||||
.use(parser);
|
||||
|
||||
if (!markdown) {
|
||||
processor.use(strip);
|
||||
}
|
||||
|
||||
const result = processor.processSync(this.file);
|
||||
const template = result.contents.toString().trim();
|
||||
|
||||
return hb
|
||||
.compile(template)(values)
|
||||
.replace(/\n{2,}/g, "\n\n");
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -62,15 +79,12 @@ export class Template<
|
|||
}
|
||||
|
||||
public static cleanText(text: string) {
|
||||
const processor = unified()
|
||||
return unified()
|
||||
.use(stringify)
|
||||
.use(frontmatter)
|
||||
.use(extract, { yaml: parse })
|
||||
.use(removeFrontmatter)
|
||||
.use(parser)
|
||||
.use(strip);
|
||||
|
||||
return processor.processSync(text).contents.toString();
|
||||
.use(strip)
|
||||
.processSync(text)
|
||||
.contents.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -29,20 +29,27 @@ export class JoinLeaveHandler extends VkEventHandler<Fields, Values> {
|
|||
`vk, group ${this.group.name}: ${user.first_name} ${user.last_name} ${dir} the group`
|
||||
);
|
||||
|
||||
const parsed = this.template.theme({
|
||||
const parsed = this.template.theme(
|
||||
{
|
||||
user,
|
||||
group: this.group,
|
||||
isJoined: context.isJoin,
|
||||
isLeave: context.isLeave,
|
||||
count,
|
||||
});
|
||||
},
|
||||
!!this.channel.markdown
|
||||
);
|
||||
|
||||
const extras: ExtraReplyMessage = {
|
||||
parse_mode: "Markdown",
|
||||
disable_web_page_preview: true,
|
||||
};
|
||||
|
||||
await this.telegram.sendMessageToChan(this.channel.id, parsed, extras);
|
||||
await this.telegram.sendMessageToChan(
|
||||
this.channel.id,
|
||||
parsed,
|
||||
!!this.channel.markdown,
|
||||
extras
|
||||
);
|
||||
|
||||
await next();
|
||||
};
|
||||
|
|
|
@ -34,20 +34,27 @@ export class MessageNewHandler extends VkEventHandler<Fields, Values> {
|
|||
`vk, group ${this.group.name} received message from ${user.first_name} ${user.last_name}: "${context.text}"`
|
||||
);
|
||||
|
||||
const parsed = this.template.theme({
|
||||
const parsed = this.template.theme(
|
||||
{
|
||||
user,
|
||||
group: this.group,
|
||||
text: context?.text || "",
|
||||
});
|
||||
},
|
||||
!!this.channel.markdown
|
||||
);
|
||||
|
||||
const extras: ExtraReplyMessage = {
|
||||
parse_mode: "Markdown",
|
||||
disable_web_page_preview: true,
|
||||
};
|
||||
|
||||
this.appendButtons(extras, user.id);
|
||||
|
||||
await this.telegram.sendMessageToChan(this.channel.id, parsed, extras);
|
||||
await this.telegram.sendMessageToChan(
|
||||
this.channel.id,
|
||||
parsed,
|
||||
!!this.channel.markdown,
|
||||
extras
|
||||
);
|
||||
|
||||
await next();
|
||||
};
|
||||
|
|
|
@ -100,33 +100,20 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
|||
reply_markup: await this.createKeyboard(text, undefined, context.wall.id),
|
||||
};
|
||||
|
||||
let msg: Message | PhotoMessage;
|
||||
|
||||
const images = context.wall.getAttachments("photo");
|
||||
const thumbs = images
|
||||
.map(getAttachment)
|
||||
.filter((el) => el)
|
||||
.slice(0, this.template.fields.images_limit) as string[];
|
||||
|
||||
const hasThumb =
|
||||
!!this.template.fields.image &&
|
||||
!!this.template.fields.images_limit &&
|
||||
thumbs.length > 0;
|
||||
|
||||
if (hasThumb) {
|
||||
msg = await this.telegram.sendPhotoToChan(
|
||||
this.channel.id,
|
||||
this.trimTextForPhoto(text, PHOTO_CAPTION_LIMIT, postType, user),
|
||||
thumbs[0]!,
|
||||
extras
|
||||
const msg = await this.sendMessage(
|
||||
text,
|
||||
thumbs,
|
||||
extras,
|
||||
postType,
|
||||
user,
|
||||
this.channel.markdown
|
||||
);
|
||||
} else {
|
||||
msg = await this.telegram.sendMessageToChan(
|
||||
this.channel.id,
|
||||
this.trimTextForPhoto(text, POST_TEXT_LIMIT, postType, user),
|
||||
extras
|
||||
);
|
||||
}
|
||||
|
||||
const event = await this.createEvent(
|
||||
id,
|
||||
|
@ -380,14 +367,18 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
|||
private themeText = (
|
||||
text: string,
|
||||
type?: string,
|
||||
user?: UsersUserFull
|
||||
user?: UsersUserFull,
|
||||
markdown?: boolean
|
||||
): string => {
|
||||
return this.template.theme({
|
||||
return this.template.theme(
|
||||
{
|
||||
user,
|
||||
group: this.group,
|
||||
type,
|
||||
text: Template.cleanText(text),
|
||||
});
|
||||
},
|
||||
markdown
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -397,9 +388,10 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
|||
text: string,
|
||||
maxChars: number,
|
||||
type?: string,
|
||||
user?: UsersUserFull
|
||||
user?: UsersUserFull,
|
||||
markdown?: boolean
|
||||
): string => {
|
||||
const withText = this.themeText(text, type, user);
|
||||
const withText = this.themeText(text, type, user, markdown);
|
||||
const limit = this.template.fields.char_limit
|
||||
? Math.min(this.template.fields.char_limit, maxChars)
|
||||
: maxChars;
|
||||
|
@ -408,13 +400,11 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
|||
return withText;
|
||||
}
|
||||
|
||||
const withoutText = this.themeText("", type, user);
|
||||
const withoutText = this.themeText("", type, user, markdown);
|
||||
const suffix = "...";
|
||||
const trimmed = text.slice(0, limit - withoutText.length - suffix.length);
|
||||
|
||||
const txt = this.themeText(`${trimmed}${suffix}`, type, user);
|
||||
|
||||
return txt;
|
||||
return this.themeText(`${trimmed}${suffix}`, type, user, markdown);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -431,4 +421,69 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
|||
.filter((el) => el)
|
||||
.join(" ")
|
||||
.trim() || "someone";
|
||||
|
||||
/**
|
||||
* Sends message
|
||||
*/
|
||||
sendMessage = async (
|
||||
text: string,
|
||||
thumbs: string[],
|
||||
extras?: ExtraReplyMessage,
|
||||
postType?: string,
|
||||
user?: UsersUserFull,
|
||||
markdown?: boolean
|
||||
) => {
|
||||
try {
|
||||
const hasThumb =
|
||||
!!this.template.fields.image &&
|
||||
!!this.template.fields.images_limit &&
|
||||
thumbs.length > 0;
|
||||
|
||||
if (hasThumb) {
|
||||
const trimmed = this.trimTextForPhoto(
|
||||
text,
|
||||
PHOTO_CAPTION_LIMIT,
|
||||
postType,
|
||||
user,
|
||||
markdown
|
||||
);
|
||||
|
||||
return await this.telegram.sendPhotoToChan(
|
||||
this.channel.id,
|
||||
trimmed,
|
||||
thumbs[0]!,
|
||||
markdown,
|
||||
extras
|
||||
);
|
||||
} else {
|
||||
const trimmed = this.trimTextForPhoto(
|
||||
text,
|
||||
POST_TEXT_LIMIT,
|
||||
postType,
|
||||
user,
|
||||
markdown
|
||||
);
|
||||
|
||||
return await this.telegram.sendMessageToChan(
|
||||
this.channel.id,
|
||||
trimmed,
|
||||
markdown,
|
||||
extras
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (!markdown) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
// Try to send as plain text
|
||||
|
||||
logger.warn(
|
||||
`telegram: failed to send markdown, falling back to plaintext: ${e}`,
|
||||
e
|
||||
);
|
||||
|
||||
return this.sendMessage(text, thumbs, extras, postType, user, false);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ export interface GroupChannel {
|
|||
events: VkEvent[];
|
||||
post_types: WallPostType[];
|
||||
templates: Partial<TemplateConfig>;
|
||||
markdown?: boolean;
|
||||
}
|
||||
|
||||
export enum VkEvent {
|
||||
|
|
|
@ -14,6 +14,7 @@ const vkChannelSchema = yup
|
|||
.matches(/^@/, ({ path }) => `${path} should start with "@"`),
|
||||
events: yup.array().of(vkChannelEventSchema),
|
||||
templates: templateOptionalSchema,
|
||||
markdown: yup.boolean().optional(),
|
||||
});
|
||||
|
||||
export const vkConfigSchema = yup
|
||||
|
|
|
@ -19,5 +19,7 @@
|
|||
|
||||
--}}
|
||||
{{#ifEq type 'suggest'}}Предложка:{{/ifEq}}
|
||||
{{text}}
|
||||
{{#if user}}[{{user.first_name}} {{user.last_name}}](https://vk.com/id{{user.id}}){{/if}}
|
||||
|
||||
{{{text}}}
|
||||
{{#if user}}
|
||||
— [{{user.first_name}} {{user.last_name}}](https://vk.com/id{{user.id}}){{/if}}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue