1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.sendTelegramMessage = void 0;
|
9 |
|
10 | var _requestPromiseNative = _interopRequireDefault(require("request-promise-native"));
|
11 |
|
12 | var _utils = require("@auto/utils");
|
13 |
|
14 | var _utils2 = require("./utils");
|
15 |
|
16 | const sendTelegramMessage = async (logs, prefixes, workspacesOptions, telegramOptions) => {
|
17 | if (typeof telegramOptions.token !== 'string') {
|
18 | throw new Error('Telegram token is required');
|
19 | }
|
20 |
|
21 | let data = logs.reduce((result, log) => {
|
22 | const name = (0, _utils.removeAutoNamePrefix)(log.name, workspacesOptions.autoNamePrefix);
|
23 | let text = '';
|
24 |
|
25 | if (result.length > 0) {
|
26 | text += '\n\n';
|
27 | }
|
28 |
|
29 | text += `*${name} v${log.version}*\n`;
|
30 | log.messages.forEach(message => {
|
31 | text += `\n${prefixes.required[message.type].value} ${message.value}`;
|
32 | });
|
33 | return result + text;
|
34 | }, '');
|
35 |
|
36 | if (data.length > _utils2.TELEGRAM_MESSAGE_MAX_LENGTH) {
|
37 | data = `${data.slice(0, _utils2.TELEGRAM_MESSAGE_MAX_LENGTH - 1)}…`;
|
38 | }
|
39 |
|
40 | const response = JSON.parse((await (0, _requestPromiseNative.default)({
|
41 | url: `${_utils2.TELEGRAM_API_URL}bot${telegramOptions.token}/sendMessage`,
|
42 | headers: {
|
43 | 'Content-Type': 'application/json'
|
44 | },
|
45 | body: JSON.stringify({
|
46 | chat_id: telegramOptions.chatId,
|
47 | parse_mode: 'markdown',
|
48 | text: data
|
49 | })
|
50 | })));
|
51 |
|
52 | if (!response.ok) {
|
53 | throw new Error(response.description);
|
54 | }
|
55 | };
|
56 |
|
57 | exports.sendTelegramMessage = sendTelegramMessage; |
\ | No newline at end of file |