UNPKG

1.59 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.sendTelegramMessage = void 0;
9
10var _requestPromiseNative = _interopRequireDefault(require("request-promise-native"));
11
12var _utils = require("@auto/utils");
13
14var _utils2 = require("./utils");
15
16const 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
57exports.sendTelegramMessage = sendTelegramMessage;
\No newline at end of file