@nitra/telegram 1.5.3 → 1.6.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.
Files changed (2) hide show
  1. package/package.json +2 -3
  2. package/src/index.js +44 -0
package/package.json CHANGED
@@ -1,15 +1,14 @@
1
1
  {
2
2
  "name": "@nitra/telegram",
3
3
  "description": "telegram helper",
4
- "version": "1.5.3",
4
+ "version": "1.6.0",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
7
7
  "files": [
8
8
  "src"
9
9
  ],
10
10
  "exports": {
11
- ".": "./src/index.js",
12
- "./pubsub": "./src/pubsub.js"
11
+ ".": "./src/index.js"
13
12
  },
14
13
  "keywords": [
15
14
  "nitra",
package/src/index.js CHANGED
@@ -42,3 +42,47 @@ export const sendMessage = async (text, params) => {
42
42
  return false
43
43
  }
44
44
  }
45
+
46
+ export const sendDocument = async (document, params = {}) => {
47
+ const currentHour = new Date().getHours()
48
+
49
+ const formData = new FormData()
50
+ formData.append('chat_id', env.TELEGRAM_CHAT_ID)
51
+
52
+ // Додаємо документ як Blob з параметрами
53
+ const blob = new Blob([document], { type: params.contentType || 'application/octet-stream' })
54
+ formData.append('document', blob, params.filename || 'document.txt')
55
+
56
+ // Додаємо опціональні параметри
57
+ if (params.caption) {
58
+ formData.append('caption', params.caption)
59
+ }
60
+
61
+ if (params.parse_mode) {
62
+ formData.append('parse_mode', params.parse_mode)
63
+ }
64
+
65
+ // Якщо в неробочий час або відключено сповіщення, то додаємо параметр disable_notification
66
+ if (!(currentHour >= 8 && currentHour <= 18) || params?.disable_notification === true) {
67
+ formData.append('disable_notification', 'true')
68
+ }
69
+
70
+ const url = `https://api.telegram.org/bot${env.TELEGRAM_BOT_TOKEN}/sendDocument`
71
+
72
+ let res
73
+ try {
74
+ res = await fetch(url, {
75
+ method: 'POST',
76
+ body: formData
77
+ })
78
+ } catch (error) {
79
+ log.error(error)
80
+ return false
81
+ }
82
+
83
+ if (res.status >= 400) {
84
+ const data = await res.json()
85
+ log.error(data.description)
86
+ return false
87
+ }
88
+ }