1
0
Fork 0
mirror of https://github.com/muerwre/vk-tg-bot.git synced 2025-04-24 22:46:41 +07:00

made plaintext posts

This commit is contained in:
Fedor Katurov 2021-05-21 10:48:44 +07:00
parent 26fea2541e
commit ba2832ca34
7 changed files with 67 additions and 30 deletions

View file

@ -2,7 +2,7 @@ import { TelegramConfig, WebhookConfig } from "./types";
import { Telegraf } from "telegraf"; import { Telegraf } from "telegraf";
import logger from "../logger"; import logger from "../logger";
import { Response } from "express"; import { Response } from "express";
import { Update } from "typegram"; import { InputMediaPhoto, Update } from "typegram";
import loggerTgMiddleware from "../logger/tg"; import loggerTgMiddleware from "../logger/tg";
import { ExtraReplyMessage } from "telegraf/typings/telegram-types"; import { ExtraReplyMessage } from "telegraf/typings/telegram-types";
@ -80,7 +80,7 @@ export class TelegramService {
}; };
/** /**
* Sends simple message to channel * Sends message with photo to channel
*/ */
public sendPhotoToChan = async ( public sendPhotoToChan = async (
channel: string, channel: string,
@ -95,6 +95,27 @@ export class TelegramService {
}); });
}; };
/**
* Sends simple message to channel
*/
public sendPhotoGroupToChan = async (
channel: string,
caption: string,
src: string[],
extra?: ExtraReplyMessage
) => {
logger.debug(`sending photo message "${caption}" to chan "${channel}"`);
const group: InputMediaPhoto[] = src.map((media, i) => ({
type: "photo",
media,
caption: i === 0 ? caption : undefined,
}));
return await this.bot.telegram.sendMediaGroup(channel, group, {
...extra,
});
};
/** /**
* Stops service * Stops service
* @param signal * @param signal

View file

@ -31,7 +31,8 @@ export class Template<
.use(frontmatter) .use(frontmatter)
.use(extract, { yaml: parse }) .use(extract, { yaml: parse })
.use(removeFrontmatter) .use(removeFrontmatter)
.use(parser); .use(parser)
.use(strip);
const file = toVFile.readSync(path.join(__dirname, "../../", filename)); const file = toVFile.readSync(path.join(__dirname, "../../", filename));
const result = processor.processSync(file); const result = processor.processSync(file);
@ -47,7 +48,7 @@ export class Template<
* Themes the template with values * Themes the template with values
*/ */
public theme = (values: V) => { public theme = (values: V) => {
return hb.compile(this.template)(values); return hb.compile(this.template)(values).replace(/\n/g, "\n\n");
}; };
/** /**

View file

@ -17,6 +17,8 @@ import logger from "../../logger";
import Composer from "telegraf"; import Composer from "telegraf";
import CallbackQueryUpdate = Update.CallbackQueryUpdate; import CallbackQueryUpdate = Update.CallbackQueryUpdate;
import { Template } from "../../template"; import { Template } from "../../template";
import { getAttachment } from "../../../utils/attachment";
import PhotoMessage = Message.PhotoMessage;
type Button = "links" | "likes" | "more"; type Button = "links" | "likes" | "more";
type UrlPrefix = string; type UrlPrefix = string;
@ -93,30 +95,38 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
const text = context.wall?.text?.trim() || ""; const text = context.wall?.text?.trim() || "";
const parsed = this.themeText(text, postType, user);
const extras: ExtraReplyMessage = { const extras: ExtraReplyMessage = {
disable_web_page_preview: true, disable_web_page_preview: true,
parse_mode: "Markdown",
reply_markup: await this.createKeyboard(text, undefined, context.wall.id), reply_markup: await this.createKeyboard(text, undefined, context.wall.id),
}; };
let msg: Message; let msg: Message | PhotoMessage;
const images = context.wall.getAttachments("photo"); 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 = const hasThumb =
this.template.fields.image && this.template.fields.image && this.template.fields.images_limit;
images.length &&
images.some((img) => img.mediumSizeUrl);
if (hasThumb) { if (hasThumb) {
const thumb = await images.find((img) => img.mediumSizeUrl); if (this.template.fields.images_limit! <= 1) {
msg = await this.telegram.sendPhotoToChan( msg = await this.telegram.sendPhotoToChan(
this.channel.id, this.channel.id,
this.trimTextForPhoto(text, PHOTO_CAPTION_LIMIT, postType, user), this.trimTextForPhoto(text, PHOTO_CAPTION_LIMIT, postType, user),
thumb?.mediumSizeUrl!, thumbs[0]!,
extras extras
); );
} else {
msg = (await this.telegram.sendPhotoGroupToChan(
this.channel.id,
this.trimTextForPhoto(text, PHOTO_CAPTION_LIMIT, postType, user),
thumbs,
extras
)) as any;
}
} else { } else {
msg = await this.telegram.sendMessageToChan( msg = await this.telegram.sendMessageToChan(
this.channel.id, this.channel.id,
@ -409,7 +419,9 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
const suffix = "..."; const suffix = "...";
const trimmed = text.slice(0, limit - withoutText.length - suffix.length); const trimmed = text.slice(0, limit - withoutText.length - suffix.length);
return this.themeText(`${trimmed}${suffix}`, type, user); const txt = this.themeText(`${trimmed}${suffix}`, type, user);
return txt;
}; };
/** /**

9
src/utils/attachment.ts Normal file
View file

@ -0,0 +1,9 @@
import { PhotoAttachment } from "vk-io";
export const getAttachment = (img: PhotoAttachment): string | undefined => {
try {
return img.mediumSizeUrl;
} catch (e) {
return undefined;
}
};

View file

@ -5,5 +5,5 @@
available variables are: user, group, text, isJoined, isLeave, count available variables are: user, group, text, isJoined, isLeave, count
(see JoinLeaveHandler) (see JoinLeaveHandler)
--}} --}}
😃 [{{user.first_name}} {{user.last_name}}](https://vk.com/id{{user.id}}) {{#ifEq user.sex 1}}присоединилась{{else}}присоединился{{/ifEq}} 😃 [{{user.first_name}} {{user.last_name}}](https://vk.com/id{{user.id}}) https://vk.com/id{{user.id}} {{#ifEq user.sex 1}}присоединилась{{else}}присоединился{{/ifEq}}
к группе. Участники: {{count}}. к группе. Участники: {{count}}.

View file

@ -5,5 +5,5 @@
available variables are: user, group, text, isJoined, isLeave, count available variables are: user, group, text, isJoined, isLeave, count
(see JoinLeaveHandler) (see JoinLeaveHandler)
--}} --}}
😡 [{{user.first_name}} {{user.last_name}}](https://vk.com/id{{user.id}}) {{#ifEq user.sex 1}}свалила{{else}}свалил{{/ifEq}} 😡 [{{user.first_name}} {{user.last_name}}](https://vk.com/id{{user.id}}) https://vk.com/id{{user.id}} {{#ifEq user.sex 1}}свалила{{else}}свалил{{/ifEq}}
из группы. Участники: {{count}}. из группы. Участники: {{count}}.

View file

@ -9,7 +9,7 @@
http://vk.com/album-: Альбом поката http://vk.com/album-: Альбом поката
likes: ['😱','🤔','😃'] likes: ['😱','🤔','😃']
char_limit: 0 char_limit: 0
images_limit: 2 images_limit: 1
--- ---
{{!-- {{!--
@ -18,12 +18,6 @@
(see PostNewHandler) (see PostNewHandler)
--}} --}}
{{#ifEq type 'suggest'}} {{#ifEq type 'suggest'}}Предложка:{{/ifEq}}
Предложка:
{{/ifEq}}
{{text}} {{text}}
{{#if user}}[{{user.first_name}} {{user.last_name}}](https://vk.com/id{{user.id}}){{/if}}
{{#if user}}
[{{user.first_name}} {{user.last_name}}](https://vk.com/id{{user.id}})
{{/if}}