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

#4 added photo thumbs for posts

This commit is contained in:
Fedor Katurov 2021-05-06 10:47:06 +07:00
parent 5535a36cb8
commit c8c5c7bff1
5 changed files with 107 additions and 18 deletions

View file

@ -2,9 +2,10 @@ import { TelegramConfig, WebhookConfig } from "./types";
import { Telegraf } from "telegraf";
import logger from "../logger";
import { Response } from "express";
import { Update } from "typegram";
import { InputFile, Update } from "typegram";
import loggerTgMiddleware from "../logger/tg";
import { ExtraReplyMessage } from "telegraf/typings/telegram-types";
import axios from "axios";
// import SocksProxyAgent from 'socks-proxy-agent';
@ -87,4 +88,20 @@ export class TelegramService {
logger.debug(`sending message "${message}" to chan "${channel}"`);
return await this.bot.telegram.sendMessage(channel, message, extra);
};
/**
* Sends simple message to channel
*/
public sendPhotoToChan = async (
channel: string,
caption: string,
src: string,
extra?: ExtraReplyMessage
) => {
logger.debug(`sending photo message "${caption}" to chan "${channel}"`);
return await this.bot.telegram.sendPhoto(channel, src, {
...extra,
caption,
});
};
}

View file

@ -4,7 +4,12 @@ import { NextMiddleware } from "middleware-io";
import { UsersUserFull } from "vk-io/lib/api/schemas/objects";
import { ConfigGroup } from "../types";
import { ExtraReplyMessage } from "telegraf/typings/telegram-types";
import { InlineKeyboardButton, InlineKeyboardMarkup, Update } from "typegram";
import {
InlineKeyboardButton,
InlineKeyboardMarkup,
Message,
Update,
} from "typegram";
import { keys } from "ramda";
import { extractURLs } from "../../../utils/extract";
import logger from "../../logger";
@ -34,6 +39,8 @@ interface Values {
type LikeCtx = Composer.Context<CallbackQueryUpdate> & { match: string[] };
const PHOTO_CAPTION_LIMIT = 1000;
export class PostNewHandler extends VkEventHandler<Fields, Values> {
constructor(...props: any) {
// @ts-ignore
@ -70,22 +77,32 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
const text = context.wall.text.trim();
const parsed = this.template.theme({
user,
group: this.group,
text,
});
const parsed = this.themeText(text, user);
const extras: ExtraReplyMessage = {
disable_web_page_preview: true,
reply_markup: await this.createKeyboard(text),
};
const msg = await this.telegram.sendMessageToChan(
this.channel,
parsed,
extras
);
let msg: Message;
const images = context.wall.getAttachments("photo");
const hasThumb =
this.template.fields.image &&
images.length &&
images.some((img) => img.mediumSizeUrl);
if (hasThumb) {
const thumb = await images.find((img) => img.mediumSizeUrl);
msg = await this.telegram.sendPhotoToChan(
this.channel,
this.trimTextForPhoto(text, user),
thumb.mediumSizeUrl,
extras
);
} else {
msg = await this.telegram.sendMessageToChan(this.channel, parsed, extras);
}
const event = await this.createEvent(
id,
@ -209,7 +226,7 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
/**
* Reacts to like button press
*/
onLikeAction = async (ctx: LikeCtx, next) => {
private onLikeAction = async (ctx: LikeCtx, next) => {
const id = ctx.update.callback_query.message.message_id;
const author = ctx.update.callback_query.from.id;
const [, channel, emo] = ctx.match;
@ -257,7 +274,7 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
next();
};
createOrUpdateLike = async (
private createOrUpdateLike = async (
author: number,
messageId: number,
emo: string
@ -270,7 +287,33 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
);
};
getLike = async (author: number, messageId: number) => {
private getLike = async (author: number, messageId: number) => {
return await this.db.getLikeBy(this.channel, messageId, author);
};
/**
* Applies template theming to photos
*/
private themeText = (text: string, user?: UsersUserFull): string => {
return this.template.theme({
user,
group: this.group,
text,
});
};
/**
* Calculates, how much should we cut off the text to match photo caption limitations
*/
private trimTextForPhoto = (text: string, user: UsersUserFull): string => {
// Full markup
const full = this.themeText(text, user);
// Rest info except text
const others = this.themeText("", user);
// How much rest markup takes
const diff = full.length - others.length;
return full.slice(0, PHOTO_CAPTION_LIMIT - diff);
};
}