@nitra/telegram 1.5.4 → 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.
- package/package.json +1 -1
- package/src/index.js +44 -0
package/package.json
CHANGED
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
|
+
}
|