|
1 | 1 | const AppController = require("../../controller/AppController") |
| 2 | +const DatabaseQuerys = require("../../controller/DatabaseQuerys") |
2 | 3 |
|
3 | 4 | async function onCallbackQuery(ctx) { |
| 5 | + |
4 | 6 | //In case that the cancel button is pressed |
5 | | - if (ctx.update.callback_query.data === "cancel_operation") { |
| 7 | + if (ctx.update.callback_query.data.includes("co_")) { |
| 8 | + |
| 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; |
| 19 | + |
| 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 |
| 25 | + |
| 26 | + default: |
| 27 | + break; |
| 28 | + } |
6 | 29 | deleteMessage(ctx, ctx.update.callback_query.message.message_id) |
7 | 30 | ctx.reply(`Operation canceled 👍`, {parse_mode: "HTML"}) |
8 | 31 | return |
9 | 32 | } |
10 | 33 |
|
11 | 34 | //Get database id |
12 | | - const databaseID = extractSubstring(ctx.update.callback_query.data, "database_id", "text_index") |
13 | | - |
14 | | - //Get what text want the user add |
15 | | - const textIndex = parseInt(extractSubstring(ctx.update.callback_query.data, "text_index", "")) |
16 | | - const text = ctx.session.textsForAdd[textIndex] |
17 | | - |
18 | | - // Check if databaseID and text are defined |
19 | | - if (!databaseID || !text) { |
20 | | - reportError(ctx) |
21 | | - return |
| 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_") |
| 39 | + |
| 40 | + let response |
| 41 | + |
| 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] |
| 47 | + |
| 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 |
| 54 | + break; |
| 55 | + |
| 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 |
| 61 | + |
| 62 | + response = await AppController().addImageToDatabase(ctx.from.id, databaseID, `https://api.telegram.org/file/bot${process.env.BOT_TOKEN}/${image.file_path}`, imageTitle) |
| 63 | + |
| 64 | + ctx.reply(`<strong>${imageTitle.length > 20 ? imageTitle + "\n\n</strong>" : imageTitle + "</strong> "}added to <strong>${response.databaseTitle}</strong> database 👍`, {parse_mode: "HTML"}) |
| 65 | + |
| 66 | + // Change this image on array for null |
| 67 | + ctx.session.imagesForAdd[index] = null |
| 68 | + break; |
| 69 | + |
| 70 | + default: |
| 71 | + reportError(ctx) |
| 72 | + break; |
22 | 73 | } |
23 | 74 |
|
24 | | - const response = await AppController().addMessageToNotionDatabase(ctx.from.id, databaseID, text) |
25 | | - |
| 75 | + //Report in error case |
26 | 76 | if (response.status === "error") { |
27 | 77 | deleteMessage(ctx, ctx.update.callback_query.message.message_id) |
28 | 78 | reportError(ctx) |
29 | 79 | return |
30 | 80 | } |
31 | 81 |
|
32 | | - ctx.reply(`<strong>${text.length > 20 ? text + "\n\n</strong>" : text + "</strong> "}added to <strong>${response.databaseTitle}</strong> database 👍`, {parse_mode: "HTML"}) |
| 82 | + //Delete DB selector |
33 | 83 | deleteMessage(ctx, ctx.update.callback_query.message.message_id) |
34 | | - |
35 | | - // Change this text on array for null |
36 | | - ctx.session.textsForAdd[textIndex] = null |
37 | | - |
38 | | - // If all texts on the array are null, clean the array |
39 | | - let allTextsAreNull = true |
40 | | - |
41 | | - ctx.session.textsForAdd.forEach(text => { |
42 | | - if (text !== null) { |
43 | | - allTextsAreNull = false |
44 | | - } |
45 | | - }) |
46 | | - |
47 | | - if (allTextsAreNull) { |
48 | | - ctx.session.textsForAdd = [] |
49 | | - } |
50 | 84 | } |
51 | 85 |
|
52 | 86 | //Extract substring function |
53 | | -function extractSubstring(str, a, b) { |
| 87 | +function extractSubstring(str, start, end) { |
54 | 88 |
|
55 | | - const position = str.indexOf(a) + a.length; |
| 89 | + const position = str.indexOf(start) + start.length; |
56 | 90 |
|
57 | | - if (!b) { |
| 91 | + if (!end) { |
58 | 92 | return str.substring(position, str.length) |
59 | 93 | } |
60 | | - return str.substring(position, str.indexOf(b, position)); |
| 94 | + return str.substring(position, str.indexOf(end, position)); |
61 | 95 | } |
62 | 96 |
|
63 | 97 | // Delete message function |
|
0 commit comments