@nitra/telegram 1.0.0
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.
- package/README.md +1 -0
- package/package.json +29 -0
- package/src/index.js +33 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @nitra/telegram
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nitra/telegram",
|
|
3
|
+
"description": "telegram helper",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"src"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": "./src/index.js",
|
|
12
|
+
"./pubsub": "./src/pubsub.js"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"nitra",
|
|
16
|
+
"telegram"
|
|
17
|
+
],
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/nitra/telegram.git"
|
|
21
|
+
},
|
|
22
|
+
"bugs": "https://github.com/nitra/telegram/issues",
|
|
23
|
+
"homepage": "https://github.com/nitra/telegram",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@nitra/bunyan": "^2.0.0",
|
|
27
|
+
"@nitra/check-env": "^2.0.7"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import log from '@nitra/bunyan'
|
|
2
|
+
import checkEnv from '@nitra/check-env'
|
|
3
|
+
import { env } from 'node:process'
|
|
4
|
+
|
|
5
|
+
checkEnv(['TELEGRAM_BOT_TOKEN', 'TELEGRAM_CHAT_ID'])
|
|
6
|
+
|
|
7
|
+
export const sendMessage = async (text, params) => {
|
|
8
|
+
const currentHour = new Date().getHours()
|
|
9
|
+
if (currentHour >= 8 && currentHour <= 18) {
|
|
10
|
+
// Max length of a Telegram message is 4096 characters
|
|
11
|
+
if (text >= 4096) {
|
|
12
|
+
text = text.slice(0, 4000)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let url = `https://api.telegram.org/bot${env.TELEGRAM_BOT_TOKEN}/sendMessage?chat_id=${env.TELEGRAM_CHAT_ID}&text=${text}`
|
|
16
|
+
|
|
17
|
+
if (params.parse_mode.toLowerCase() === 'html') {
|
|
18
|
+
url += '&parse_mode=HTML'
|
|
19
|
+
}
|
|
20
|
+
let res
|
|
21
|
+
try {
|
|
22
|
+
res = await fetch(url)
|
|
23
|
+
} catch (err) {
|
|
24
|
+
log.error(err)
|
|
25
|
+
return false
|
|
26
|
+
}
|
|
27
|
+
if (res.status > 400) {
|
|
28
|
+
return false
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
log.info('Telegram message skipped, not in working hours: ', text)
|
|
32
|
+
}
|
|
33
|
+
}
|