Skip to content

Commit bcfe209

Browse files
committed
Propierties set support added
1 parent 62dd510 commit bcfe209

File tree

11 files changed

+684
-128
lines changed

11 files changed

+684
-128
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"axios": "^0.27.2",
2121
"dotenv": "^16.0.0",
2222
"grammy": "^1.7.2",
23+
"moment": "^2.29.3",
2324
"mongoose": "^6.2.10"
2425
},
2526
"devDependencies": {

src/app/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ function initialSesionValues() {
3232
return {
3333
waitingForAuthCode: false,
3434
waitingForAnnouncementMessage: false,
35-
textsForAdd: [],
36-
imagesForAdd: []
35+
dataForAdd: [],
36+
waitingForPropiertyValue: false
3737
};
3838
}
3939

src/app/events/onCallbackQuery.js

Lines changed: 184 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,210 @@
11
const AppController = require("../../controller/AppController")
2-
const DatabaseQuerys = require("../../controller/DatabaseQuerys")
2+
3+
const extractSubstring = require("../../scripts/extractSubstring")
4+
const deleteMessage = require("../../scripts/deleteMessage")
35

46
async function onCallbackQuery(ctx) {
57

6-
//In case that the cancel button is pressed
7-
if (ctx.update.callback_query.data.includes("co_")) {
8+
const userID = ctx.from.id
89

9-
//Get dataType
10-
const dataType = extractSubstring(ctx.update.callback_query.data, "dt_", "i_")
11-
12-
switch (dataType) {
13-
case "text":
14-
//Get text position
15-
const textIndex = parseInt(extractSubstring(ctx.update.callback_query.data, "i_", ""))
16-
//Make it null
17-
ctx.session.textsForAdd[textIndex] = null
18-
break;
10+
const prefix = ctx.update.callback_query.data.substring(0, 3)
11+
/**
12+
* * "db_" = database selection
13+
* * "pr_" = propierty selection
14+
* * "vl_" = value selection
15+
*/
16+
17+
switch (prefix) {
18+
case "db_": {
19+
await AppController().t_responses(ctx).respondWithOfPropierties(userID)
20+
21+
//Delete the previous message
22+
deleteMessage(ctx, ctx.update.callback_query.message.message_id)
1923

20-
case "image":
21-
//Get image position
22-
const position = parseInt(extractSubstring(ctx.update.callback_query.data, "i_", ""))
23-
//Make it null
24-
ctx.session.imagesForAdd[position] = null
24+
break;
25+
}
2526

26-
default:
27-
break;
27+
case "pr_": {
28+
29+
//Check if cancel operation button is pressed
30+
if (extractSubstring(ctx.update.callback_query.data, "pr_", "in_") === "co_") {
31+
ctx.reply("Operation canceled", {parse_mode: "HTML"})
32+
//Delete the previous message
33+
deleteMessage(ctx, ctx.update.callback_query.message.message_id)
34+
return
2835
}
29-
deleteMessage(ctx, ctx.update.callback_query.message.message_id)
30-
ctx.reply(`Operation canceled 👍`, {parse_mode: "HTML"})
31-
return
32-
}
3336

34-
//Get database id
35-
const databaseID = extractSubstring(ctx.update.callback_query.data, "db_", "dt_")
36-
37-
//Get dataType
38-
const dataType = extractSubstring(ctx.update.callback_query.data, "dt_", "i_")
37+
//Check if send button is pressed
38+
if (extractSubstring(ctx.update.callback_query.data, "pr_", "in_") === "sd_") {
39+
40+
//Get data
41+
const index = extractSubstring(ctx.update.callback_query.data, "in_", false)
42+
const data = ctx.session.dataForAdd[index]
43+
44+
let response
45+
46+
switch (data.type) {
47+
case "text": {
48+
//Get what text want the user add
49+
const text = data.data.title
50+
51+
response = await AppController().addMessageToNotionDatabase(userID, data.databaseID, data)
52+
53+
if (response.status !== "error") {
54+
ctx.reply(`<strong>${text.length > 20 ? text + "\n\n</strong>" : text + "</strong> "}added to <strong>${response.databaseTitle}</strong> database 👍`, {parse_mode: "HTML"})
55+
}
56+
57+
break;
58+
}
59+
60+
case "image":
61+
//Get what image want the user add
62+
const image = data.data
63+
image.title = image.title ? image.title : image.file_path
64+
65+
response = await AppController().addImageToDatabase(userID, data.databaseID, `https://api.telegram.org/file/bot${process.env.BOT_TOKEN}/${image.file_path}`, image.title, data.propiertiesValues)
66+
67+
if (response.status !== "error") {
68+
ctx.reply(`<strong>${image.title.length > 20 ? image.title + "\n\n</strong>" : image.title + "</strong> "}added to <strong>${response.databaseTitle}</strong> database 👍`, {parse_mode: "HTML"})
69+
}
70+
71+
break;
72+
73+
default:
74+
reportError(ctx)
75+
break;
76+
}
77+
78+
// Change this data on array for null
79+
ctx.session.dataForAdd[index] = null
80+
81+
//Report in error case
82+
if (response.status === "error") {
83+
deleteMessage(ctx, ctx.update.callback_query.message.message_id)
84+
reportError(ctx)
85+
return
86+
}
87+
88+
//Delete DB selector
89+
deleteMessage(ctx, ctx.update.callback_query.message.message_id)
90+
91+
return
92+
}
3993

40-
let response
94+
await AppController().t_responses(ctx).respondWithPropiertyValues()
4195

42-
switch (dataType) {
43-
case "text":
44-
//Get what text want the user add
45-
const textIndex = parseInt(extractSubstring(ctx.update.callback_query.data, "i_", ""))
46-
const text = ctx.session.textsForAdd[textIndex]
96+
//Delete the previous message
97+
deleteMessage(ctx, ctx.update.callback_query.message.message_id)
4798

48-
response = await AppController().addMessageToNotionDatabase(ctx.from.id, databaseID, text)
49-
50-
ctx.reply(`<strong>${text.length > 20 ? text + "\n\n</strong>" : text + "</strong> "}added to <strong>${response.databaseTitle}</strong> database 👍`, {parse_mode: "HTML"})
51-
52-
// Change this text on array for null
53-
ctx.session.textsForAdd[textIndex] = null
5499
break;
100+
}
55101

56-
case "image":
57-
//Get what image want the user add
58-
const index = parseInt(extractSubstring(ctx.update.callback_query.data, "i_", ""))
59-
const image = ctx.session.imagesForAdd[index]
60-
const imageTitle = image.title ? image.title : image.file_path
102+
case "vl_": {
103+
//Get data index
104+
const index = parseInt(extractSubstring(ctx.update.callback_query.data, "pi_", ""))
105+
106+
//If done or cancel operation button pressed
107+
if (extractSubstring(ctx.update.callback_query.data, "vl_", "pi_") === "dn_") {
61108

62-
response = await AppController().addImageToDatabase(ctx.from.id, databaseID, `https://api.telegram.org/file/bot${process.env.BOT_TOKEN}/${image.file_path}`, imageTitle)
109+
//Set false in the case that the app was waiting for a value
110+
ctx.session.waitingForPropiertyValue = false
63111

64-
ctx.reply(`<strong>${imageTitle.length > 20 ? imageTitle + "\n\n</strong>" : imageTitle + "</strong> "}added to <strong>${response.databaseTitle}</strong> database 👍`, {parse_mode: "HTML"})
112+
//Delete the previous message
113+
await deleteMessage(ctx, ctx.update.callback_query.message.message_id)
65114

66-
// Change this image on array for null
67-
ctx.session.imagesForAdd[index] = null
68-
break;
69-
70-
default:
71-
reportError(ctx)
72-
break;
73-
}
115+
AppController().t_responses(ctx).respondWithOfPropierties(userID, ctx.session.dataForAdd[index].listOfPropiertiesQuery)
116+
return
117+
}
74118

75-
//Report in error case
76-
if (response.status === "error") {
77-
deleteMessage(ctx, ctx.update.callback_query.message.message_id)
78-
reportError(ctx)
79-
return
80-
}
119+
//Get propierty id
120+
const propiertyID = extractSubstring(ctx.update.callback_query.data, "pr_", "pi_")
81121

82-
//Delete DB selector
83-
deleteMessage(ctx, ctx.update.callback_query.message.message_id)
84-
}
122+
//Get propierty data
123+
const propierty = Object.values(ctx.session.dataForAdd[index].propierties).find(prop => {
124+
return prop.id === propiertyID
125+
})
85126

86-
//Extract substring function
87-
function extractSubstring(str, start, end) {
127+
//Get optionID
128+
const optionID = extractSubstring(ctx.update.callback_query.data, "vl_", "pr_")
88129

89-
const position = str.indexOf(start) + start.length;
130+
//If not exists the propierties values propierty, create it
131+
if (!ctx.session.dataForAdd[index].propiertiesValues) {
132+
ctx.session.dataForAdd[index].propiertiesValues = {}
133+
}
90134

91-
if (!end) {
92-
return str.substring(position, str.length)
93-
}
94-
return str.substring(position, str.indexOf(end, position));
95-
}
135+
const propiertyValue = ctx.session.dataForAdd[index].propiertiesValues[propiertyID]
136+
137+
switch (propierty.type) {
138+
case "multi_select": {
139+
//Get data
140+
const data = propierty.multi_select.options.find(option => {
141+
return option.id === optionID
142+
})
143+
144+
if (propiertyValue) {
145+
// If the array don't include the propierty id, add it
146+
if (!Object.keys(ctx.session.dataForAdd[index].propiertiesValues[propiertyID]).includes(data)) {
147+
ctx.session.dataForAdd[index].propiertiesValues[propiertyID] = [...propiertyValue, data]
148+
}
149+
} else {
150+
ctx.session.dataForAdd[index].propiertiesValues[propiertyID] = [data]
151+
}
152+
153+
ctx.reply(`<strong>${data.name}</strong> value added`, {parse_mode: "HTML"})
154+
155+
break;
156+
}
157+
158+
case "checkbox": {
159+
// Get the boolean of the optionID
160+
const p_data = JSON.parse(optionID)
161+
162+
//Add it to the values
163+
ctx.session.dataForAdd[index].propiertiesValues[propiertyID] = p_data
164+
165+
//Reply
166+
await ctx.reply(`<strong>${propierty.name}</strong> is <strong>${p_data ? "checked" : "unchecked"}</strong>`, {parse_mode: "HTML"})
167+
168+
//Delete the checked selector
169+
await deleteMessage(ctx, ctx.update.callback_query.message.message_id)
170+
171+
//Reply with propierties list
172+
AppController().t_responses(ctx).respondWithOfPropierties(userID, ctx.session.dataForAdd[index].listOfPropiertiesQuery)
173+
174+
break;
175+
}
176+
177+
case "select": {
178+
//Get data
179+
const data = propierty.select.options.find(option => {
180+
return option.id === optionID
181+
})
182+
183+
ctx.session.dataForAdd[index].propiertiesValues[propiertyID] = data
184+
185+
//Reply
186+
await ctx.reply(`<strong>${data.name}</strong> value added`, {parse_mode: "HTML"})
187+
188+
//Delete the checked selector
189+
await deleteMessage(ctx, ctx.update.callback_query.message.message_id)
190+
191+
//Reply with propierties list
192+
AppController().t_responses(ctx).respondWithOfPropierties(userID, ctx.session.dataForAdd[index].listOfPropiertiesQuery)
193+
break;
194+
195+
}
96196

97-
// Delete message function
98-
async function deleteMessage(ctx, messageId) {
99-
try {
100-
await ctx.api.deleteMessage(ctx.chat.id, messageId)
101-
} catch (error) {
102-
console.log(error)
197+
default:
198+
reportError(ctx)
199+
break;
200+
}
201+
202+
break;
203+
}
204+
205+
default:
206+
reportError(ctx)
207+
break;
103208
}
104209
}
105210

src/app/events/onPhoto.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,21 @@ async function onPhoto(ctx) {
2525
}
2626

2727
//Add image to array of texts
28-
ctx.session.imagesForAdd.push(data)
28+
const obj = {type: "image", data}
29+
ctx.session.dataForAdd.push(obj)
2930

3031
//If pass some time delete the text on the array
3132
function cleanArray() {
32-
if (ctx.session.imagesForAdd.includes(data)) {
33-
const index = ctx.session.imagesForAdd.indexOf(data)
34-
ctx.session.imagesForAdd.splice(index, 1)
33+
if (ctx.session.dataForAdd.includes(obj)) {
34+
const index = ctx.session.dataForAdd.indexOf(obj)
35+
ctx.session.dataForAdd.splice(index, 1)
3536
}
3637
}
3738

38-
//Delete the item in the imagesForAdd in...
39-
setTimeout(cleanArray, 5 * 60 * 1000) //5 Minutes
39+
//Delete the item in the dataForAdd in...
40+
setTimeout(cleanArray, 15 * 60 * 1000) //15 Minutes
4041

41-
const keyboard = await AppController().getKeyboardOfDatabases(databases.results, "❌ Don't upload image", "image", ctx.session.imagesForAdd)
42+
const keyboard = await AppController().getKeyboardOfDatabases(databases.results, "❌ Don't upload image", "image", ctx.session.dataForAdd)
4243

4344
ctx.reply(`Select the <strong>database</strong> to save this image\n\nIf you atached an caption to this image, <strong>it's gonna be the title of the new page</strong> in the Database\n\n<strong>Remember that this image keeps public</strong>\nDon't upload sensitive data!`, {...keyboard, parse_mode: "HTML"})
4445
}

0 commit comments

Comments
 (0)