Skip to content
This repository was archived by the owner on Dec 24, 2024. It is now read-only.

Commit 5fa5c8d

Browse files
authored
Create messages object (#12)
1 parent f8247d1 commit 5fa5c8d

File tree

1 file changed

+94
-48
lines changed

1 file changed

+94
-48
lines changed

src/bot.ts

+94-48
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,47 @@ function clearState(chatId) {
3636
};
3737
}
3838

39+
const messages = {
40+
start: "Вітаю! \nЦей Бот допомагає створити службову записку, подання або звернення. Для створення натисніть /createfile",
41+
selectDocumentType: "Будь ласка, виберіть тип документа:",
42+
serviceMemo: "Службова записка",
43+
submission: "Подання",
44+
appeal: "Звернення",
45+
noEntries: "Файлів нема",
46+
errorRetrievingRecords: "Помилка при отриманні файлів",
47+
errorChangingFileType: "Помилка при зміні типу файлу на",
48+
errorCreatingRecord: "Помилка при створенні файлу",
49+
errorRetrievingRecord: "Помилка при отриманні файлу",
50+
errorCheckingId: "Помилка при перевірці id",
51+
errorDeletingRecord: "Помилка при видаленні файлу",
52+
errorChangingReceiver: "Помилка при зміні одержувача на",
53+
errorChangingTitle: "Помилка при зміні назви на",
54+
errorChangingContent: "Помилка при зміні тексту на",
55+
enterIdFileToRetrieve: "Введіть id файлу, який ви хочете отримати:",
56+
enterIdFileToEdit: "Введіть id файлу, який ви хочете змінити:",
57+
enterIdFileToDelete: "Введіть id файлу, який ви хочете видалити:",
58+
enterReceiver: "Введіть одержувача:",
59+
whatDoYouWantToChange: "Що ви хочете змінити?",
60+
fileType: "Тип файлу",
61+
receiver: "Одержувача",
62+
title: "Назву",
63+
content: "Текст",
64+
back: "Повернутись",
65+
selectNewFileType: "Оберіть новий тип файлу:",
66+
enterNewReceiver: "Введіть нового одержувача:",
67+
enterNewTitle: "Введіть нову назву:",
68+
enterNewContent: "Введіть новий текст:",
69+
enterValidId: "Такого файлу не існує\\. Введіть правильний id одного з цих файлів\\:",
70+
fileNo: "Файл №",
71+
wasSuccessfullyDeleted: "було успішно видалено",
72+
enterTitle: "Введіть тему файлу:",
73+
enterContent: "Введіть текст файлу:",
74+
memoCreated: "Файл створено!",
75+
};
76+
77+
const messagesInOneString = Object.values(messages).join("\n");
78+
console.log(messagesInOneString);
79+
3980
const startDescription = "Перезапустити бота";
4081
const helpDescription = "Переглянути список команд";
4182
const createfileDescription = "Створити файл";
@@ -59,7 +100,7 @@ bot.command("start", async (ctx) => {
59100
const chatId = ctx.chat.id;
60101
clearState(chatId);
61102

62-
await ctx.reply("Вітаю! \nЦей Бот допомагає створити службову записку. Для створення натисніть /createfile");
103+
await ctx.reply(messages.start);
63104
});
64105

65106
bot.command("help", async (ctx) => {
@@ -78,9 +119,9 @@ bot.command("createfile", async (ctx) => {
78119
const chatId = ctx.chat.id;
79120
clearState(chatId);
80121

81-
const keyboard = new InlineKeyboard().text("Службова записка", "service_memo").text("Подання", "submission").text("Звернення", "appeal");
122+
const keyboard = new InlineKeyboard().text(messages.serviceMemo, "service_memo").text(messages.submission, "submission").text(messages.appeal, "appeal");
82123

83-
await ctx.reply("Будь ласка, виберіть тип документу:", { reply_markup: keyboard });
124+
await ctx.reply(messages.selectDocumentType, { reply_markup: keyboard });
84125
states[chatId].step = "choose_type";
85126
});
86127

@@ -93,7 +134,7 @@ bot.command("listfiles", async (ctx) => {
93134
const responseData = response.data;
94135

95136
if (Array.isArray(responseData) && responseData.length === 0) {
96-
await ctx.reply("Записів нема");
137+
await ctx.reply(messages.noEntries);
97138
} else {
98139
const formattedData = formatFilesList(responseData);
99140

@@ -102,7 +143,7 @@ bot.command("listfiles", async (ctx) => {
102143
});
103144
}
104145
} catch (error) {
105-
await ctx.reply("Помилка при отриманні записів");
146+
await ctx.reply(messages.errorRetrievingRecords);
106147
await ctx.reply(error);
107148
}
108149
});
@@ -112,126 +153,127 @@ bot.command("getfile", async (ctx) => {
112153
clearState(chatId);
113154

114155
states[chatId].isGetFile = true;
115-
await ctx.reply("Введіть id файлу який ви хочете отримати:");
156+
await ctx.reply(messages.enterIdFileToRetrieve);
116157
});
117158

118159
bot.command("editfile", async (ctx) => {
119160
const chatId = ctx.chat.id;
120161
clearState(chatId);
121162

122163
states[chatId].isEditFile = true;
123-
await ctx.reply("Введіть id файлу який ви хочете змінити:");
164+
await ctx.reply(messages.enterIdFileToEdit);
124165
});
125166

126167
bot.command("deletefile", async (ctx) => {
127168
const chatId = ctx.chat.id;
128169
clearState(chatId);
129170

130171
states[chatId].isDeleteFile = true;
131-
await ctx.reply("Введіть id файлу який ви хочете видалити:");
172+
await ctx.reply(messages.enterIdFileToDelete);
132173
});
133174

134175
bot.callbackQuery("service_memo", async (ctx) => {
135176
const chatId = ctx.chat.id;
136177

137-
states[chatId].type = "СЛУЖБОВА ЗАПИСКА";
178+
states[chatId].type = messages.serviceMemo.toUpperCase();
138179
states[chatId].step = "receiver";
139-
await ctx.reply("Введіть одержувача:");
180+
await ctx.reply(messages.enterReceiver);
140181
});
141182

142183
bot.callbackQuery("submission", async (ctx) => {
143184
const chatId = ctx.chat.id;
144185

145-
states[chatId].type = "ПОДАННЯ";
186+
states[chatId].type = messages.submission.toUpperCase();
146187
states[chatId].step = "receiver";
147-
await ctx.reply("Введіть одержувача:");
188+
await ctx.reply(messages.enterReceiver);
148189
});
149190

150191
bot.callbackQuery("appeal", async (ctx) => {
151192
const chatId = ctx.chat.id;
152193

153-
states[chatId].type = "ЗВЕРНЕННЯ";
194+
states[chatId].type = messages.appeal.toUpperCase();
154195
states[chatId].step = "receiver";
155-
await ctx.reply("Введіть одержувача:");
196+
await ctx.reply(messages.enterReceiver);
156197
});
157198

158199
bot.callbackQuery("back", async (ctx) => {
159-
const keyboard = new InlineKeyboard().text("Тип файлу", "type").text("Одержувача", "receiver").text("Назву", "title").text("Текст", "content");
160-
await ctx.reply("Що ви хочете змінити?", { reply_markup: keyboard });
200+
const keyboard = new InlineKeyboard()
201+
.text(messages.fileType, "type")
202+
.text(messages.receiver, "receiver")
203+
.text(messages.title, "title")
204+
.text(messages.content, "content");
205+
await ctx.reply(messages.whatDoYouWantToChange, { reply_markup: keyboard });
161206
});
162207

163208
bot.callbackQuery("type", async (ctx) => {
164209
const keyboard = new InlineKeyboard()
165-
.text("Службова записка", "newServiceMemo")
166-
.text("Подання", "newSubmission")
167-
.text("Звернення", "newAppeal")
168-
.text("Повернутись", "back");
169-
await ctx.reply("Оберіть новий тип документу:", { reply_markup: keyboard });
210+
.text(messages.serviceMemo, "newServiceMemo")
211+
.text(messages.submission, "newSubmission")
212+
.text(messages.appeal, "newAppeal")
213+
.text(messages.back, "back");
214+
await ctx.reply(messages.selectNewFileType, { reply_markup: keyboard });
170215
});
171216

172217
bot.callbackQuery("newServiceMemo", async (ctx) => {
173218
const chatId = ctx.chat.id;
174219

175-
const serviceMemoString = "Службова записка";
176220
try {
177-
const { stringToReply, inputFile } = await changeFileType(states[chatId].editFileId, serviceMemoString);
221+
const { stringToReply, inputFile } = await changeFileType(states[chatId].editFileId, messages.serviceMemo);
178222

179223
await ctx.reply(stringToReply);
180224
await ctx.replyWithDocument(inputFile);
181225
} catch (error) {
182-
await ctx.reply(`Помилка при зміні типу файлу на «${serviceMemoString}»`);
226+
await ctx.reply(`${messages.errorChangingFileType} «${messages.serviceMemo}»`);
183227
await ctx.reply(error);
184228
}
185229
});
186230

187231
bot.callbackQuery("newSubmission", async (ctx) => {
188232
const chatId = ctx.chat.id;
189233

190-
const submissionString = "Подання";
191234
try {
192-
const { stringToReply, inputFile } = await changeFileType(states[chatId].editFileId, submissionString);
235+
const { stringToReply, inputFile } = await changeFileType(states[chatId].editFileId, messages.submission);
193236

194237
await ctx.reply(stringToReply);
195238
await ctx.replyWithDocument(inputFile);
196239
} catch (error) {
197-
await ctx.reply(`Помилка при зміні типу файлу на «${submissionString}»`);
240+
await ctx.reply(`${messages.errorChangingFileType} «${messages.submission}»`);
198241
await ctx.reply(error);
199242
}
200243
});
201244

202245
bot.callbackQuery("newAppeal", async (ctx) => {
203246
const chatId = ctx.chat.id;
204247

205-
const appealString = "Звернення";
206248
try {
207-
const { stringToReply, inputFile } = await changeFileType(states[chatId].editFileId, appealString);
249+
const { stringToReply, inputFile } = await changeFileType(states[chatId].editFileId, messages.appeal);
208250

209251
await ctx.reply(stringToReply);
210252
await ctx.replyWithDocument(inputFile);
211253
} catch (error) {
212-
await ctx.reply(`Помилка при зміні типу файлу на «${appealString}»`);
254+
await ctx.reply(`${messages.errorChangingFileType} «${messages.appeal}»`);
213255
await ctx.reply(error);
214256
}
215257
});
216258

217259
bot.callbackQuery("receiver", async (ctx) => {
218260
const chatId = ctx.chat.id;
219261

220-
await ctx.reply("Введіть нового одержувача:");
262+
await ctx.reply(messages.enterNewReceiver);
221263
states[chatId].step = "newReceiver";
222264
});
223265

224266
bot.callbackQuery("title", async (ctx) => {
225267
const chatId = ctx.chat.id;
226268

227-
await ctx.reply("Введіть нову назву:");
269+
await ctx.reply(messages.enterNewTitle);
228270
states[chatId].step = "newTitle";
229271
});
230272

231273
bot.callbackQuery("content", async (ctx) => {
232274
const chatId = ctx.chat.id;
233275

234-
await ctx.reply("Введіть новий текст:");
276+
await ctx.reply(messages.enterNewContent);
235277
states[chatId].step = "newContent";
236278
});
237279

@@ -250,7 +292,7 @@ bot.on("message:text", async (ctx) => {
250292

251293
await ctx.replyWithDocument(new InputFile(Buffer.from(response.data), filename));
252294
} catch (error) {
253-
await ctx.reply("Помилка при створенні запису");
295+
await ctx.reply(messages.errorCreatingRecord);
254296
await ctx.reply(error);
255297
}
256298
}
@@ -266,7 +308,7 @@ bot.on("message:text", async (ctx) => {
266308

267309
await ctx.replyWithDocument(new InputFile(Buffer.from(response.data), filename));
268310
} catch (error) {
269-
await ctx.reply("Помилка при отриманні запису");
311+
await ctx.reply(messages.errorRetrievingRecord);
270312
}
271313
}
272314

@@ -281,12 +323,12 @@ bot.on("message:text", async (ctx) => {
281323
const responseData = response.data;
282324

283325
if (Array.isArray(responseData) && responseData.length === 0) {
284-
await ctx.reply("Записів нема");
326+
await ctx.reply(messages.noEntries);
285327
} else {
286328
if (!responseData.some((item) => item.id == text)) {
287329
const formattedData = formatFilesList(responseData);
288330

289-
await ctx.reply(`Такого файлу не існує\\. Введіть правильний id одного з цих файлів\\:\n${formattedData}`, {
331+
await ctx.reply(`${messages.enterValidId}\n${formattedData}`, {
290332
parse_mode: "MarkdownV2",
291333
});
292334

@@ -295,12 +337,16 @@ bot.on("message:text", async (ctx) => {
295337
}
296338
}
297339
} catch (error) {
298-
await ctx.reply("Помилка при перевірці id");
340+
await ctx.reply(messages.errorCheckingId);
299341
await ctx.reply(error);
300342
}
301343

302-
const keyboard = new InlineKeyboard().text("Тип файлу", "type").text("Одержувача", "receiver").text("Назву", "title").text("Текст", "content");
303-
await ctx.reply("Що ви хочете змінити?", { reply_markup: keyboard });
344+
const keyboard = new InlineKeyboard()
345+
.text(messages.fileType, "type")
346+
.text(messages.receiver, "receiver")
347+
.text(messages.title, "title")
348+
.text(messages.content, "content");
349+
await ctx.reply(messages.whatDoYouWantToChange, { reply_markup: keyboard });
304350
}
305351

306352
if (states[chatId].isDeleteFile) {
@@ -309,9 +355,9 @@ bot.on("message:text", async (ctx) => {
309355
try {
310356
await deleteDocumentRequest(text);
311357

312-
await ctx.reply(`Файл №${text} було успішно видалено`);
358+
await ctx.reply(`${messages.fileNo}${text} ${messages.wasSuccessfullyDeleted}`);
313359
} catch (error) {
314-
await ctx.reply("Помилка при видаленні запису");
360+
await ctx.reply(messages.errorDeletingRecord);
315361
}
316362
}
317363

@@ -321,17 +367,17 @@ bot.on("message:text", async (ctx) => {
321367
case "receiver":
322368
states[chatId].receiver = text;
323369
states[chatId].step = "title";
324-
await ctx.reply("Введіть тему записки:");
370+
await ctx.reply(messages.enterTitle);
325371
break;
326372
case "title":
327373
states[chatId].title = text;
328374
states[chatId].step = "content";
329-
await ctx.reply("Введіть текст записки:");
375+
await ctx.reply(messages.enterContent);
330376
break;
331377
case "content":
332378
states[chatId].content = text;
333379
await createDocument(chatId, ctx);
334-
await ctx.reply("Записка створена!");
380+
await ctx.reply(messages.memoCreated);
335381
clearState(chatId);
336382
break;
337383
case "newReceiver":
@@ -342,7 +388,7 @@ bot.on("message:text", async (ctx) => {
342388
await ctx.reply(stringToReply);
343389
await ctx.replyWithDocument(inputFile);
344390
} catch (error) {
345-
await ctx.reply(`Помилка при зміні одержувача на «${text}»`);
391+
await ctx.reply(`${messages.errorChangingReceiver} «${text}»`);
346392
await ctx.reply(error);
347393
}
348394

@@ -355,7 +401,7 @@ bot.on("message:text", async (ctx) => {
355401
await ctx.reply(stringToReply);
356402
await ctx.replyWithDocument(inputFile);
357403
} catch (error) {
358-
await ctx.reply(`Помилка при зміні назви на «${text}»`);
404+
await ctx.reply(`${messages.errorChangingTitle} «${text}»`);
359405
await ctx.reply(error);
360406
}
361407

@@ -368,7 +414,7 @@ bot.on("message:text", async (ctx) => {
368414
await ctx.reply(stringToReply);
369415
await ctx.replyWithDocument(inputFile);
370416
} catch (error) {
371-
await ctx.reply(`Помилка при зміні тексту на «${text}»`);
417+
await ctx.reply(`${messages.errorChangingContent} «${text}»`);
372418
await ctx.reply(error);
373419
}
374420

0 commit comments

Comments
 (0)