Skip to content

Commit

Permalink
Add fieldId middleware to get video fileId's
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantGeekDev committed Jan 5, 2024
1 parent e2f88e9 commit 2418f61
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
15 changes: 9 additions & 6 deletions propertiesSampleData.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
"name": "Villa 6",
"price": 670000,
"availability": true,
"videoUrl": "https://www.dropbox.com/sh/t6330gcog0lz7dh/AADqYOkfEnvIQ0RF6Lr0eRBja?dl=0&preview=Video+1+SaliSol+Hills+Showhouse+2023.mp4",
"videoFileId": "https://www.dropbox.com/sh/t6330gcog0lz7dh/AADqYOkfEnvIQ0RF6Lr0eRBja?dl=0&preview=Video+1+SaliSol+Hills+Showhouse+2023.mp4",
"thumbnailUrl": "https://uc470239d7d0c1c41f915323d5c8.previews.dropboxusercontent.com/p/thumb/ACLJ7D6O6bqDe2gRpcdVMDPaxNwZYWAenzJD8IQuZKGoaWdojbcZHMHHw0X-6CUmisfriGocNXvxH1tLSty6kEh3PftuhgeMPVUlQuhD7KmGYWfmqnWQKPY9bwnKjU-qxJJv7c2StyA_xn6H4l4MCTF0dWVHalbKeTT5aW5ucsm5f6raAcqCXx7mRQto9AkM3lHzDi8dw7imp65me_EFKUmEziFK108IecRwKQ1KZdNWrjVIuAcaIyGzq51l--Kphv6rh9tHaSXx0DMv_MX0vtYbL751cve0h9YGrvGPnFUMKhP6oPqXAFzLnSJCc3qnbzfaGuaWeI7sjBLh5-MXtt8JTZfsU8-om8Kv1k0gtPF4dg/p.png",
"albumUrls": [
"https://uc470239d7d0c1c41f915323d5c8.previews.dropboxusercontent.com/p/thumb/ACLJ7D6O6bqDe2gRpcdVMDPaxNwZYWAenzJD8IQuZKGoaWdojbcZHMHHw0X-6CUmisfriGocNXvxH1tLSty6kEh3PftuhgeMPVUlQuhD7KmGYWfmqnWQKPY9bwnKjU-qxJJv7c2StyA_xn6H4l4MCTF0dWVHalbKeTT5aW5ucsm5f6raAcqCXx7mRQto9AkM3lHzDi8dw7imp65me_EFKUmEziFK108IecRwKQ1KZdNWrjVIuAcaIyGzq51l--Kphv6rh9tHaSXx0DMv_MX0vtYbL751cve0h9YGrvGPnFUMKhP6oPqXAFzLnSJCc3qnbzfaGuaWeI7sjBLh5-MXtt8JTZfsU8-om8Kv1k0gtPF4dg/p.png",
"https://uc470239d7d0c1c41f915323d5c8.previews.dropboxusercontent.com/p/thumb/ACLJ7D6O6bqDe2gRpcdVMDPaxNwZYWAenzJD8IQuZKGoaWdojbcZHMHHw0X-6CUmisfriGocNXvxH1tLSty6kEh3PftuhgeMPVUlQuhD7KmGYWfmqnWQKPY9bwnKjU-qxJJv7c2StyA_xn6H4l4MCTF0dWVHalbKeTT5aW5ucsm5f6raAcqCXx7mRQto9AkM3lHzDi8dw7imp65me_EFKUmEziFK108IecRwKQ1KZdNWrjVIuAcaIyGzq51l--Kphv6rh9tHaSXx0DMv_MX0vtYbL751cve0h9YGrvGPnFUMKhP6oPqXAFzLnSJCc3qnbzfaGuaWeI7sjBLh5-MXtt8JTZfsU8-om8Kv1k0gtPF4dg/p.png"
],
"builtMetersSquared": 168,
"plotMetersSquared": 532,
"dropboxUrl": "https://www.dropbox.com/sh/t6330gcog0lz7dh/AADqYOkfEnvIQ0RF6Lr0eRBja?dl=0",
"telegramContactUrl": "https://t.me/PropertyGram_bot",
"websiteUrl": "https://salisolpark.com/salisol-hills"
"websiteUrl": "https://salisolpark.com/salisol-hills",
"albumUrls": [
"https://uc470239d7d0c1c41f915323d5c8.previews.dropboxusercontent.com/p/thumb/ACLJ7D6O6bqDe2gRpcdVMDPaxNwZYWAenzJD8IQuZKGoaWdojbcZHMHHw0X-6CUmisfriGocNXvxH1tLSty6kEh3PftuhgeMPVUlQuhD7KmGYWfmqnWQKPY9bwnKjU-qxJJv7c2StyA_xn6H4l4MCTF0dWVHalbKeTT5aW5ucsm5f6raAcqCXx7mRQto9AkM3lHzDi8dw7imp65me_EFKUmEziFK108IecRwKQ1KZdNWrjVIuAcaIyGzq51l--Kphv6rh9tHaSXx0DMv_MX0vtYbL751cve0h9YGrvGPnFUMKhP6oPqXAFzLnSJCc3qnbzfaGuaWeI7sjBLh5-MXtt8JTZfsU8-om8Kv1k0gtPF4dg/p.png",
"https://ucd3b2b2169690f13f7943ad683e.previews.dropboxusercontent.com/p/thumb/ACK1cGb1225TlUB5zrve84aiZ03Wx-1D7ic-toemLMfr70PYmbUBZTH073DcYCAjm9GiO5PcOf_4zl7fRaasaRcQQpiTsvXvd6qjROClxeyZ9Z40yil08MzpFMz0IHtScRDg6cPA2ppVRD_DlNfPhFj-OfBc3MX3d5kJOQPZs9xnJWn5PrIhJ48dhAycsox1PuRe2pFV0p5-SfftP0VZjkDSr2Hd50R9n0s34J_boekj_cUy68YI4lmGBlpNpdW44e1fMoLDZmwuV1owDdSotaqGDxUD9U03isNh69FfVpinLzw9y34gcHozq0xZ-VextAtrtrt2bbaQMuJDI-N7t42qGYbMKOEA_uaZYekRDCeJAg/p.png",
"https://ucbda0eb239a53969d8a8e7f5475.previews.dropboxusercontent.com/p/thumb/ACLqnVpH6F7yEf4Nlojv_K5vcRsq2MF8n-mRdf5IUMXsv3_Y5poMtlbty1VBRyg214Wqsyh-JjFUv_6tk1h5pe8Dn3c6BwqydRNKYW9o4gO0aMzvq3G13DZ-y1hkPFifyAzYlG_ibSv0Xmrcwvv5kAzs9raGFhe7dMMLm0j4oVbThEgDVOcVRRkLyp0mQl825lH-v57ZP_J71izkP_Dxdi3hyUWxp2FuOq7ZtWGJuub5S7uBmxiSjenOzBe0PNthPGjoG5K3sJFr_KynvwRtYXummX8APLR5ezOWf2M8mNcXRcOI2nH7DcyzZIYgsVlsaxCs-oS2I89wAOLog_PcJB4eqGvmbBe5R0iW8bWQd0c6GA/p.jpeg",
"https://uc177e23be7b8c96b07f3e30647c.previews.dropboxusercontent.com/p/thumb/ACIFhDABAj7th24mAYq_nS0PzZSnryTlkBQL_74SBQf2mIFgbBsn-ZI_s-dLHAqtdkAJZxIGq0NZZ0XGeW2aRR9MN6OaJZ8xU5VLnFW_57kNtaOgasmmtl13Jg9SdFeu_SaEh2w2sDfqeieB4vJlapSkGqQaVZgv7p7uyNqf39g5gce2rnxvSCuTocQs52hpfWqlvXNT35XUb5Tw2f-wx6MZXFWX0cVPI5FxVAyPV2EDgFdE_rgSQ0vPaCENjJAix7WXajPI2Df8tCXntoSBEY8gDZYy4T7Maz_kjBSR5euIgKngdRLF2iHtLwgIbSfQyKxGxm2x3crjUJv8zmRCNRFVSxiJL_nJzySHumYgGxzO_g/p.jpeg",
"https://ucdc0769904db036def229337527.previews.dropboxusercontent.com/p/thumb/ACKJuGcDqfCB039AMybN4Lbog87xyqBblw-rnUyll17XVCZRHe-P-6r81amFKJEU-CX5hsgP4fJQo-9nl5OOqq3c_DN5xsPG2p4TKPvhmkDfzgNQ_ok78aroz4JaWsybLknnE3mbr7Yc2jbx88Ocnp1Z37rLi3NPbjoJKCJzOnb-OlKKPAa-tLZDP4Sy3xxsXzD6ENaRmFQSqtDw4dPIqouajTChREApsVCVEyz6aXNw4YryfScBGOmWSMpOiAZp7BAOOAPIDGBqSlvVJQ0pTkLOXKdXqlk14S1W9o2q5X8c7V425Em7nMH8ie3N0j_y0XfCpVSepKUVJBYofn2sY5BV/p.jpeg"
]
}
2 changes: 2 additions & 0 deletions src/config/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { Bot } from "../types/telegram.js";
import { buildName, getOrCreatePlayer } from "../services/user.js";
import { getOrCreateChat } from "../services/chat.js";
import { propertiesController } from "../controllers/properties.js";
import { fileIdController } from "../controllers/fileId.js";

function extendContext(bot: Bot, database: Database) {
bot.use(async (ctx, next) => {
Expand Down Expand Up @@ -54,6 +55,7 @@ function setupControllers(bot: Bot) {
bot.use(startController);
bot.use(stopController);
bot.use(propertiesController);
bot.use(fileIdController);
}

export async function startBot(database: Database) {
Expand Down
11 changes: 11 additions & 0 deletions src/controllers/fileId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Composer } from "grammy";
import type { CustomContext } from "../types/context.js";

export const fileIdController = new Composer<CustomContext>();
fileIdController.on("message", async ctx => {
const fileId = ctx.update.message?.video?.file_id;
if (!fileId) {
return;
}
await ctx.reply(`File ID: ${fileId}`);
});
16 changes: 9 additions & 7 deletions src/controllers/properties.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Composer } from "grammy";
import type { CustomContext } from "../types/context.js";
import type { Property } from "../types/database.js";
import { generatePropertyDescription } from "../services/Property/property.service.js";
import {
generatePropertyDescription,
generatePropertyPhotoAlbum
} from "../services/Property/property.service.js";

export const propertiesController = new Composer<CustomContext>();

Expand All @@ -10,17 +13,16 @@ propertiesController.command("properties", async ctx => {
const currentPropertyIndex = 0;

const currentProperty = properties[currentPropertyIndex];
const totalProperties = properties.length;

const { thumbnailUrl, name, collectionName, videoUrl } = currentProperty;
const { videoFileId: videoUrl, albumUrls } = currentProperty;

const propertyDescription = generatePropertyDescription(currentProperty);

const propertyPhotoAlbum = generatePropertyPhotoAlbum(albumUrls);
await ctx.reply(`Property ${currentPropertyIndex + 1}/${totalProperties}`);
await ctx.reply(propertyDescription, {
parse_mode: "MarkdownV2"
});
await ctx.replyWithPhoto(thumbnailUrl, {
caption: `${collectionName}: ${name}`
});

await ctx.replyWithVideo(videoUrl);
await ctx.replyWithMediaGroup(propertyPhotoAlbum);
});
10 changes: 6 additions & 4 deletions src/services/Property/property.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InputFile, type InputMediaPhoto } from "grammy/types";
import type { Property } from "../../types/database";
import { type InputMediaPhoto } from "grammy/types";
import { InputMediaBuilder } from "grammy";
import type { Property } from "../../types/database.js";

export const generatePropertyDescription = (property: Property): string => {
const {
Expand All @@ -13,7 +13,9 @@ export const generatePropertyDescription = (property: Property): string => {
} = property;

// Property description uses Telegram's Markdown V2
const propertyDescription = `*${collectionName}: ${name}*\nPlot Size:${plotMetersSquared}\nBuilt Meters:${builtMetersSquared}Price: ${price}${
const propertyDescription = `*🏠 ${collectionName}: ${name}*\n\nPlot Size: ${plotMetersSquared}m2 \nBuilt Meters: ${builtMetersSquared}m2\nPrice: ${price
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ",")}${
availability ? "" : "Reserved"
}\n`;

Expand All @@ -26,7 +28,7 @@ export const generatePropertyPhotoAlbum = (
const photoAlbum: InputMediaPhoto[] = [];

for (const photoUrl of albumUrls) {
const photo = InputMediaBuilder.photo(new InputFile(photoUrl));
const photo = InputMediaBuilder.photo(photoUrl);
photoAlbum.push(photo);
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface Property {
name: string;
price: number;
availability: boolean;
videoUrl: string;
videoFileId: string;
thumbnailUrl: string;
albumUrls: string[];
builtMetersSquared: number;
Expand Down

0 comments on commit 2418f61

Please sign in to comment.