mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-24 22:46:41 +07:00
#1 added buttons to MessageNewHandler.ts
This commit is contained in:
parent
eb6a645ad3
commit
b35b5d3731
4 changed files with 73 additions and 12 deletions
|
@ -40,7 +40,21 @@ export class Template<
|
|||
}
|
||||
}
|
||||
|
||||
theme = (values: V) => {
|
||||
/**
|
||||
* Themes the tempalte with values
|
||||
*/
|
||||
public theme = (values: V) => {
|
||||
return hb.compile(this.template)(values);
|
||||
};
|
||||
|
||||
/**
|
||||
* Registers handlebars helpers
|
||||
*/
|
||||
public static registerHelpers() {
|
||||
hb.registerHelper("ifEq", function (arg1, arg2, options) {
|
||||
return arg1 == arg2 ? options.fn(this) : options.inverse(this);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Template.registerHelpers();
|
||||
|
|
|
@ -4,13 +4,17 @@ import { NextMiddleware } from "middleware-io";
|
|||
import logger from "../../logger";
|
||||
import { ContextDefaultState } from "vk-io/lib/structures/contexts/context";
|
||||
import { UsersUserFull } from "vk-io/lib/api/schemas/objects";
|
||||
import { ConfigGroup } from "../types";
|
||||
import { ExtraReplyMessage } from "telegraf/typings/telegram-types";
|
||||
|
||||
interface Fields {
|
||||
buttons: string[];
|
||||
buttons?: string[];
|
||||
link_text?: string;
|
||||
}
|
||||
|
||||
interface Values {
|
||||
user: UsersUserFull;
|
||||
group: ConfigGroup;
|
||||
text: string;
|
||||
}
|
||||
|
||||
|
@ -24,22 +28,44 @@ export class MessageNewHandler extends VkEventHandler<Fields, Values> {
|
|||
return;
|
||||
}
|
||||
|
||||
const users = await this.instance.api.users.get({
|
||||
user_ids: [String(context.senderId)],
|
||||
});
|
||||
const from = users[0];
|
||||
const user = await this.getUserByID(String(context.senderId));
|
||||
|
||||
logger.debug(
|
||||
`received message from ${from.first_name} ${from.last_name}: ${context.text}`
|
||||
logger.info(
|
||||
`received message from ${user.first_name} ${user.last_name}: ${context.text}`
|
||||
);
|
||||
|
||||
const parsed = this.template.theme({
|
||||
user: from,
|
||||
user,
|
||||
group: this.group,
|
||||
text: context.text,
|
||||
});
|
||||
|
||||
await this.telegram.sendMessageToChan(this.channel, parsed).catch(next);
|
||||
const extras: ExtraReplyMessage = {
|
||||
parse_mode: "Markdown",
|
||||
};
|
||||
|
||||
this.appendButtons(extras, user.id);
|
||||
|
||||
await this.telegram
|
||||
.sendMessageToChan(this.channel, parsed, extras)
|
||||
.catch(next);
|
||||
|
||||
await next();
|
||||
};
|
||||
|
||||
/**
|
||||
* Appending buttons (if needed) by mutating original extras
|
||||
*/
|
||||
private appendButtons = (extras: ExtraReplyMessage, userId: number) => {
|
||||
if (!this.template?.fields?.buttons?.includes("link")) {
|
||||
return;
|
||||
}
|
||||
|
||||
const text = this.template?.fields?.link_text || "View dialog";
|
||||
const url = this.makeDialogUrl(this.group.id, userId);
|
||||
|
||||
extras.reply_markup = {
|
||||
inline_keyboard: [[{ text, url }]],
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
|
@ -25,4 +25,23 @@ export class VkEventHandler<
|
|||
console.log(`vk received unknown event`, ctx);
|
||||
await next();
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches user by id
|
||||
* @param id
|
||||
*/
|
||||
protected getUserByID = async (id: string) => {
|
||||
const users = await this.instance.api.users.get({
|
||||
user_ids: [id],
|
||||
fields: ["sex"],
|
||||
});
|
||||
|
||||
return users[0];
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns url for group dialog
|
||||
*/
|
||||
protected makeDialogUrl = (groupId: number, userId: number): string =>
|
||||
`https://vk.com/gim${groupId}?sel=${userId}`;
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
---
|
||||
buttons: [link]
|
||||
link_text: Посмотреть сообщение
|
||||
---
|
||||
{{!--
|
||||
use handlebars template here
|
||||
available variables are: user, text
|
||||
available variables are: user, group, text
|
||||
(see MessageNewHandler)
|
||||
--}}
|
||||
{{user.first_name}} {{user.last_name}} пишет: {{text}}
|
||||
***Новое сообщение:***
|
||||
[{{user.first_name}} {{user.last_name}}](https://vk.com/id{{user.id}}) {{#ifEq user.sex 1}}написала{{else}}написал{{/ifEq}}: {{text}}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue