这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,29 @@ logger.error('<b>test log!</b>');
```

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`"
}
```
42 changes: 34 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -30,6 +32,28 @@ export async function sendMsgToTg(chatId, botToken, text, extra = {}) {
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) => {
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
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
},
Expand Down