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

made optional likes

This commit is contained in:
Fedor Katurov 2021-05-20 17:49:28 +07:00
parent 3f2e07b391
commit 26fea2541e
2 changed files with 17 additions and 9 deletions

View file

@ -32,6 +32,8 @@ interface Fields {
link_text?: string; link_text?: string;
links: Record<UrlPrefix, string>; links: Record<UrlPrefix, string>;
likes?: string[]; likes?: string[];
char_limit?: number;
images_limit?: number;
} }
interface Values { interface Values {
@ -44,6 +46,7 @@ interface Values {
type LikeCtx = Composer.Context<CallbackQueryUpdate> & { match: string[] }; type LikeCtx = Composer.Context<CallbackQueryUpdate> & { match: string[] };
const PHOTO_CAPTION_LIMIT = 1000; const PHOTO_CAPTION_LIMIT = 1000;
const POST_TEXT_LIMIT = 4096;
export class PostNewHandler extends VkEventHandler<Fields, Values> { export class PostNewHandler extends VkEventHandler<Fields, Values> {
constructor(...props: any) { constructor(...props: any) {
@ -52,7 +55,7 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
this.onInit(); this.onInit();
} }
private likes: string[] = ["👎", "👍"]; private likes?: string[];
public execute = async (context: WallPostContext, next: NextMiddleware) => { public execute = async (context: WallPostContext, next: NextMiddleware) => {
const id = context?.wall?.id; const id = context?.wall?.id;
@ -110,14 +113,14 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
const thumb = await images.find((img) => img.mediumSizeUrl); const thumb = await images.find((img) => img.mediumSizeUrl);
msg = await this.telegram.sendPhotoToChan( msg = await this.telegram.sendPhotoToChan(
this.channel.id, this.channel.id,
this.trimTextForPhoto(text, postType, user), this.trimTextForPhoto(text, PHOTO_CAPTION_LIMIT, postType, user),
thumb?.mediumSizeUrl!, thumb?.mediumSizeUrl!,
extras extras
); );
} else { } else {
msg = await this.telegram.sendMessageToChan( msg = await this.telegram.sendMessageToChan(
this.channel.id, this.channel.id,
parsed, this.trimTextForPhoto(text, POST_TEXT_LIMIT, postType, user),
extras extras
); );
} }
@ -225,6 +228,8 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
* Generates like button * Generates like button
*/ */
private generateLikes: ExtraGenerator = async (text, eventId) => { private generateLikes: ExtraGenerator = async (text, eventId) => {
if (!this.likes) return [];
if (eventId) { if (eventId) {
const event = await this.getEventById(eventId); const event = await this.getEventById(eventId);
if (!event) { if (!event) {
@ -300,7 +305,7 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
!emo || !emo ||
!id || !id ||
channel != this.channel.id || channel != this.channel.id ||
!this.likes.includes(emo) !this.likes?.includes(emo)
) { ) {
await next(); await next();
return; return;
@ -387,21 +392,22 @@ export class PostNewHandler extends VkEventHandler<Fields, Values> {
*/ */
private trimTextForPhoto = ( private trimTextForPhoto = (
text: string, text: string,
maxChars: number,
type?: string, type?: string,
user?: UsersUserFull user?: UsersUserFull
): string => { ): string => {
const withText = this.themeText(text, type, user); const withText = this.themeText(text, type, user);
const limit = this.template.fields.char_limit
? Math.min(this.template.fields.char_limit, maxChars)
: maxChars;
if (withText.length < PHOTO_CAPTION_LIMIT) { if (withText.length < limit) {
return withText; return withText;
} }
const withoutText = this.themeText("", type, user); const withoutText = this.themeText("", type, user);
const suffix = "..."; const suffix = "...";
const trimmed = text.slice( const trimmed = text.slice(0, limit - withoutText.length - suffix.length);
0,
PHOTO_CAPTION_LIMIT - withoutText.length - suffix.length
);
return this.themeText(`${trimmed}${suffix}`, type, user); return this.themeText(`${trimmed}${suffix}`, type, user);
}; };

View file

@ -8,6 +8,8 @@
https://vk.com/album-: Альбом поката https://vk.com/album-: Альбом поката
http://vk.com/album-: Альбом поката http://vk.com/album-: Альбом поката
likes: ['😱','🤔','😃'] likes: ['😱','🤔','😃']
char_limit: 0
images_limit: 2
--- ---
{{!-- {{!--