@nitra/telegram 1.3.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/src/index.js +28 -24
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nitra/telegram",
3
3
  "description": "telegram helper",
4
- "version": "1.3.0",
4
+ "version": "1.4.1",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
7
7
  "files": [
@@ -24,7 +24,7 @@
24
24
  "license": "MIT",
25
25
  "dependencies": {
26
26
  "@nitra/check-env": "^3.2.0",
27
- "@nitra/pino": "^1.6.0"
27
+ "@nitra/pino": "^1.6.2"
28
28
  },
29
29
  "engines": {
30
30
  "node": ">=22.0.0"
package/src/index.js CHANGED
@@ -8,31 +8,35 @@ export const MAX_TELEGRAM_MSG_LENGTH = 4096
8
8
 
9
9
  export const sendMessage = async (text, params) => {
10
10
  const currentHour = new Date().getHours()
11
- if (process.env.TELEGRAM_ROUND_CLOCK || (currentHour >= 8 && currentHour <= 18)) {
12
- // Max length of a Telegram message is 4096 characters
13
- if (text >= MAX_TELEGRAM_MSG_LENGTH) {
14
- text = text.slice(0, MAX_TELEGRAM_MSG_LENGTH)
15
- }
11
+ // if (process.env.TELEGRAM_ROUND_CLOCK || (currentHour >= 8 && currentHour <= 18)) {
12
+ // Max length of a Telegram message is 4096 characters
13
+ if (text >= MAX_TELEGRAM_MSG_LENGTH) {
14
+ text = text.slice(0, MAX_TELEGRAM_MSG_LENGTH)
15
+ }
16
+
17
+ let url = `https://api.telegram.org/bot${env.TELEGRAM_BOT_TOKEN}/sendMessage?chat_id=${
18
+ env.TELEGRAM_CHAT_ID
19
+ }&text=${encodeURIComponent(text)}`
16
20
 
17
- let url = `https://api.telegram.org/bot${env.TELEGRAM_BOT_TOKEN}/sendMessage?chat_id=${
18
- env.TELEGRAM_CHAT_ID
19
- }&text=${encodeURIComponent(text)}`
21
+ if (params?.parse_mode?.toLowerCase() === 'html') {
22
+ url += '&parse_mode=HTML'
23
+ }
20
24
 
21
- if (params?.parse_mode?.toLowerCase() === 'html') {
22
- url += '&parse_mode=HTML'
23
- }
24
- let res
25
- try {
26
- res = await fetch(url)
27
- } catch (error) {
28
- log.error(error)
29
- return false
30
- }
31
- if (res.status >= 400) {
32
- log.error('Telegram message skipped, not sent ', text)
33
- return false
34
- }
35
- } else {
36
- log.info('Telegram message skipped, not in working hours: ', text)
25
+ if (!(currentHour >= 8 && currentHour <= 18) || params?.disable_notification === true) {
26
+ url += '&disable_notification=true'
27
+ }
28
+ let res
29
+ try {
30
+ res = await fetch(url)
31
+ } catch (error) {
32
+ log.error(error)
33
+ return false
34
+ }
35
+ if (res.status >= 400) {
36
+ log.error('Telegram message skipped, not sent ', text)
37
+ return false
37
38
  }
39
+ // } else {
40
+ // log.info('Telegram message skipped, not in working hours: ', text)
41
+ // }
38
42
  }