-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (92 loc) · 2.75 KB
/
Copy pathindex.js
File metadata and controls
107 lines (92 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import build from 'pino-abstract-transport';
const API_URL = 'https://api.telegram.org';
export async function sendMsgToTg(
chatId,
botToken,
message,
extra = {},
apiUrl = API_URL,
) {
const method = 'sendMessage';
const baseUrl = apiUrl.replace(/\/+$/, '');
const url = `${baseUrl}/bot${botToken}/${method}`;
const body = JSON.stringify({
chat_id: chatId,
text: message,
...extra,
});
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body,
});
if (response.ok) {
const data = await response.json();
const { ok, result, error_code, description } = data;
if (ok) return result;
throw new Error(`${error_code}: ${description}`);
}
const { status, statusText } = response;
throw new Error(`${status}: ${statusText}`);
}
const verboseSerializer = {
html: (string) => `<pre><code class="language-json">${string}</code></pre>`,
markdown: (string) => `\`\`\`json\n${string}\n\`\`\``,
markdownv2: (string) => `\`\`\`json\n${string}\n\`\`\``,
};
const prepareMessage = ({ pinoData, verbose, parseMode, messageKey }) => {
if (!verbose) return pinoData[messageKey];
const msg = JSON.stringify(pinoData, null, 2);
if (!parseMode) return msg;
const parseModeLC = parseMode.toLowerCase();
const serializer = verboseSerializer[parseModeLC];
return serializer(msg);
};
/**
*
* @param {object} params - parameters for creating a transport
* @param {number} params.chatId - chat ID
* @param {string} params.botToken - bot token
* @param {boolean} [params.verbose] - send debugging information
* @param {string} [params.messageKey] - key for message. Default is 'msg'
* @param {string} [params.apiUrl] - Telegram Bot API URL. Default is 'https://api.telegram.org'
* @param {object} [params.extra] - additional parameters for sending a message https://core.telegram.org/bots/api#sendmessage
* @returns {Promise}
*/
export default function ({
chatId,
botToken,
verbose = false,
messageKey = 'msg',
apiUrl = API_URL,
extra = {},
}) {
const pendingPromises = new Set();
return build(
async (source) => {
for await (const pinoData of source) {
const { parse_mode } = extra;
const message = prepareMessage({
pinoData,
verbose,
parseMode: parse_mode,
messageKey,
});
const promise = sendMsgToTg(chatId, botToken, message, extra, apiUrl)
.catch((reason) => console.error(reason))
.finally(() => {
pendingPromises.delete(promise);
});
pendingPromises.add(promise);
}
},
{
async close(err, cb) {
await Promise.allSettled([...pendingPromises]);
cb(err, 'close');
},
},
);
}