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:
parent
26fea2541e
commit
ba2832ca34
7 changed files with 67 additions and 30 deletions
|
@ -2,7 +2,7 @@ import { TelegramConfig, WebhookConfig } from "./types";
|
|||
import { Telegraf } from "telegraf";
|
||||
import logger from "../logger";
|
||||
import { Response } from "express";
|
||||
import { Update } from "typegram";
|
||||
import { InputMediaPhoto, Update } from "typegram";
|
||||
import loggerTgMiddleware from "../logger/tg";
|
||||
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 (
|
||||
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
|
||||
* @param signal
|
||||
|
|
|
@ -31,7 +31,8 @@ export class Template<
|
|||
.use(frontmatter)
|
||||
.use(extract, { yaml: parse })
|
||||
.use(removeFrontmatter)
|
||||
.use(parser);
|
||||
.use(parser)
|
||||
.use(strip);
|
||||
|
||||
const file = toVFile.readSync(path.join(__dirname, "../../", filename));
|
||||
const result = processor.processSync(file);
|
||||
|
@ -47,7 +48,7 @@ export class Template<
|
|||
* Themes the template with values
|
||||
*/
|
||||
public theme = (values: V) => {
|
||||
return hb.compile(this.template)(values);
|
||||
return hb.compile(this.template)(values).replace(/\n/g, "\n\n");
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -17,6 +17,8 @@ import logger from "../../logger";
|
|||
import Composer from "telegraf";
|
||||
import CallbackQueryUpdate = Update.CallbackQueryUpdate;
|
||||
import { Template } from "../../template";
|
||||
import { getAttachment } from "../../../utils/attachment";
|
||||
import PhotoMessage = Message.PhotoMessage;
|
||||
|
||||
type Button = "links" | "likes" | "more";
|
||||
type UrlPrefix = string;
|
||||
|
@ -93,30 +95,38 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
|||
|
||||
const text = context.wall?.text?.trim() || "";
|
||||
|
||||
const parsed = this.themeText(text, postType, user);
|
||||
|
||||
const extras: ExtraReplyMessage = {
|
||||
disable_web_page_preview: true,
|
||||
parse_mode: "Markdown",
|
||||
reply_markup: await this.createKeyboard(text, undefined, context.wall.id),
|
||||
};
|
||||
|
||||
let msg: Message;
|
||||
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 &&
|
||||
images.length &&
|
||||
images.some((img) => img.mediumSizeUrl);
|
||||
this.template.fields.image && this.template.fields.images_limit;
|
||||
|
||||
if (hasThumb) {
|
||||
const thumb = await images.find((img) => img.mediumSizeUrl);
|
||||
if (this.template.fields.images_limit! <= 1) {
|
||||
msg = await this.telegram.sendPhotoToChan(
|
||||
this.channel.id,
|
||||
this.trimTextForPhoto(text, PHOTO_CAPTION_LIMIT, postType, user),
|
||||
thumb?.mediumSizeUrl!,
|
||||
thumbs[0]!,
|
||||
extras
|
||||
);
|
||||
} else {
|
||||
msg = (await this.telegram.sendPhotoGroupToChan(
|
||||
this.channel.id,
|
||||
this.trimTextForPhoto(text, PHOTO_CAPTION_LIMIT, postType, user),
|
||||
thumbs,
|
||||
extras
|
||||
)) as any;
|
||||
}
|
||||
} else {
|
||||
msg = await this.telegram.sendMessageToChan(
|
||||
this.channel.id,
|
||||
|
@ -409,7 +419,9 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
|
|||
const suffix = "...";
|
||||
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
9
src/utils/attachment.ts
Normal 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;
|
||||
}
|
||||
};
|
|
@ -5,5 +5,5 @@
|
|||
available variables are: user, group, text, isJoined, isLeave, count
|
||||
(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}}.
|
||||
|
|
|
@ -5,5 +5,5 @@
|
|||
available variables are: user, group, text, isJoined, isLeave, count
|
||||
(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}}.
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
http://vk.com/album-: Альбом поката
|
||||
likes: ['😱','🤔','😃']
|
||||
char_limit: 0
|
||||
images_limit: 2
|
||||
images_limit: 1
|
||||
---
|
||||
{{!--
|
||||
|
||||
|
@ -18,12 +18,6 @@
|
|||
(see PostNewHandler)
|
||||
|
||||
--}}
|
||||
{{#ifEq type 'suggest'}}
|
||||
Предложка:
|
||||
|
||||
{{/ifEq}}
|
||||
{{#ifEq type 'suggest'}}Предложка:{{/ifEq}}
|
||||
{{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}}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue