diff --git a/README.md b/README.md index c31578e..72603cb 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,29 @@ logger.error('test log!'); ``` The extra parameter is optional. Parameters that the method [sendMessage](https://core.telegram.org/bots/api#sendmessage) supports can be passed to it + +--- + +If `verbose = true`, the message will be displayed as +``` +{ + "level": 50, + "time": 1721832322878, + "pid": 13522, + "hostname": "fedora", + "msg": "`inline fixed-width code`" +} +``` + +--- + +If `verbose = true` and `parse_mode = "HTML|Markdown|MarkdownV2`, the message will be displayed as +```json +{ + "level": 50, + "time": 1721832322878, + "pid": 13522, + "hostname": "fedora", + "msg": "`inline fixed-width code`" +} +``` diff --git a/index.js b/index.js index d7ab6fd..b60b563 100644 --- a/index.js +++ b/index.js @@ -2,19 +2,21 @@ import build from "pino-abstract-transport"; const API_URL = "https://api.telegram.org/bot"; -export async function sendMsgToTg(chatId, botToken, text, extra = {}) { +export async function sendMsgToTg(chatId, botToken, message, extra = {}) { const method = "sendMessage"; const url = `${API_URL}${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: JSON.stringify({ - chat_id: chatId, - text: text, - ...extra, - }), + body, }); if (response.ok) { @@ -30,6 +32,28 @@ export async function sendMsgToTg(chatId, botToken, text, extra = {}) { throw new Error(`${status}: ${statusText}`); } +const verboseSerializer = { + html: (string) => `
${string}
`, + markdown: (string) => `\`\`\`json\n${string}\n\`\`\``, + markdownv2: (string) => `\`\`\`json\n${string}\n\`\`\``, +}; + +const prepareMessage = (pinoData, verbose, parseMode) => { + if (verbose) { + const msg = JSON.stringify(pinoData, null, 2); + + if (parseMode) { + const parseModeLC = parseMode.toLowerCase(); + const serializer = verboseSerializer[parseModeLC]; + return serializer(msg); + } + + return msg; + } + + return pinoData.msg; +}; + /** * * @param {object} params - parameters for creating a transport @@ -42,9 +66,11 @@ export async function sendMsgToTg(chatId, botToken, text, extra = {}) { export default function ({ chatId, botToken, verbose = false, extra = {} }) { return build(async function (source) { for await (const obj of source) { - const text = verbose ? JSON.stringify(obj) : obj.msg; + const { parse_mode } = extra; + const message = prepareMessage(obj, verbose, parse_mode); + try { - await sendMsgToTg(chatId, botToken, text, extra); + await sendMsgToTg(chatId, botToken, message, extra); } catch (error) { console.error(error); } diff --git a/package-lock.json b/package-lock.json index 6c3ab09..736dbb7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "pino-telegram-webhook", - "version": "0.0.1", + "version": "0.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "pino-telegram-webhook", - "version": "0.0.1", + "version": "0.2.0", "license": "MIT", "dependencies": { "pino-abstract-transport": "^1.2.0" diff --git a/package.json b/package.json index 698fec7..3515454 100644 --- a/package.json +++ b/package.json @@ -1,18 +1,16 @@ { "name": "pino-telegram-webhook", - "version": "0.1.0", - "type": "module", + "version": "0.2.0", "description": "A [Pino v7+ transport](https://getpino.io/#/docs/transports?id=v7-transports) to send message to [Telegram](https://telegram.org/)", "keywords": [ "pino", "telegram", "webhook", - "telegram webhook", - "transport" + "telegram webhook" ], "main": "index.js", "scripts": { - "test": "node test/transport.test.js" + "test": "echo \"Error: no test specified\" && exit 1" }, "engines": { "node": ">=18.0.0", @@ -21,13 +19,7 @@ "dependencies": { "pino-abstract-transport": "^1.2.0" }, - "devDependencies": { - "undici": "^6.19.2" - }, - "repository": { - "type": "git", - "url": "https://github.com/Jhon-Mosk/pino-telegram-webhook" - }, + "repository": "https://github.com/Jhon-Mosk/pino-telegram-webhook", "bugs": { "url": "https://github.com/Jhon-Mosk/pino-telegram-webhook/issues" },