tgbox 1.0.0 → 1.0.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.
- package/README.md +172 -0
- package/package.json +11 -2
package/README.md
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# TgBox
|
|
2
|
+
|
|
3
|
+
TgBox is a lightweight cloud storage service for Node.js that uses Telegram as
|
|
4
|
+
its storage medium. It lets you use Telegram chats and channels as personal or
|
|
5
|
+
shared cloud storage for uploading, downloading, searching, and managing files.
|
|
6
|
+
|
|
7
|
+
Telegram provides the storage infrastructure, while TgBox provides a simple
|
|
8
|
+
programmatic interface for using it like a cloud drive.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
[](https://www.npmjs.com/package/tgbox)
|
|
13
|
+
|
|
14
|
+
The package is available on [npm](https://www.npmjs.com/package/tgbox).
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install tgbox
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## How it works
|
|
21
|
+
|
|
22
|
+
TgBox stores files as Telegram document messages in a chat or channel. The
|
|
23
|
+
Telegram message ID acts as the file ID, so files can later be listed, searched,
|
|
24
|
+
downloaded, or deleted through the TgBox API.
|
|
25
|
+
|
|
26
|
+
The target can be:
|
|
27
|
+
|
|
28
|
+
- `me` for Telegram Saved Messages
|
|
29
|
+
- a username such as `@mychannel`
|
|
30
|
+
- another Telegram chat or channel the account can access
|
|
31
|
+
|
|
32
|
+
## Telegram API credentials
|
|
33
|
+
|
|
34
|
+
You need a Telegram `apiId` and `apiHash` to connect. You can get both values
|
|
35
|
+
from [my.telegram.org](https://my.telegram.org).
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
const TgBox = require('tgbox');
|
|
39
|
+
|
|
40
|
+
const storage = new TgBox(
|
|
41
|
+
process.env.TELEGRAM_API_ID,
|
|
42
|
+
process.env.TELEGRAM_API_HASH,
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
await storage.init();
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
On the first start, TgBox asks for your phone number, login code, and, if
|
|
49
|
+
configured, your 2FA password. The session is then stored locally in
|
|
50
|
+
`session.txt`. Never publish or share this file.
|
|
51
|
+
|
|
52
|
+
## Upload files
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
const result = await storage.upload('./documents/report.pdf', '@mychannel');
|
|
56
|
+
|
|
57
|
+
if (result.success) {
|
|
58
|
+
console.log(result.messageId);
|
|
59
|
+
console.log(result.fileName);
|
|
60
|
+
} else {
|
|
61
|
+
console.error(result.error);
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
The default chat is `me` (Saved Messages). Telegram file sizes are returned in
|
|
66
|
+
`size` as bytes.
|
|
67
|
+
|
|
68
|
+
## List files
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
const result = await storage.listFiles('@mychannel');
|
|
72
|
+
|
|
73
|
+
if (result.success) {
|
|
74
|
+
result.files.forEach((file) => {
|
|
75
|
+
console.log(file.messageId, file.fileName, file.size, file.mimeType);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Each file object contains, among other fields:
|
|
81
|
+
|
|
82
|
+
- `messageId`
|
|
83
|
+
- `fileName`
|
|
84
|
+
- `size` in bytes
|
|
85
|
+
- `mimeType`
|
|
86
|
+
- `date`
|
|
87
|
+
- `caption`
|
|
88
|
+
- `telegram` with the complete Telegram message and document data
|
|
89
|
+
|
|
90
|
+
## Search files
|
|
91
|
+
|
|
92
|
+
The search checks the file name, caption, and message ID:
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
const result = await storage.searchFiles('report', '@mychannel');
|
|
96
|
+
|
|
97
|
+
result.files.forEach((file) => {
|
|
98
|
+
console.log(file);
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
For an exact search by ID or file name:
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
const result = await storage.findFile('report.pdf', '@mychannel');
|
|
106
|
+
|
|
107
|
+
result.files.forEach((file) => {
|
|
108
|
+
console.log(file.messageId, file.fileName);
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
All files with the same name are returned.
|
|
113
|
+
|
|
114
|
+
## Download files
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
const result = await storage.download(
|
|
118
|
+
123,
|
|
119
|
+
'./downloads/report.pdf',
|
|
120
|
+
'@mychannel',
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
if (!result.success) {
|
|
124
|
+
console.error(result.error);
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Delete files
|
|
129
|
+
|
|
130
|
+
You can delete one or more files by their Telegram message IDs:
|
|
131
|
+
|
|
132
|
+
```js
|
|
133
|
+
await storage.deleteFile(123, '@mychannel');
|
|
134
|
+
await storage.deleteFile([123, 124], '@mychannel');
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Deletion is permanent and uses `revoke: true`. Your Telegram account needs the
|
|
138
|
+
required permissions in the target chat or channel.
|
|
139
|
+
|
|
140
|
+
## Send messages
|
|
141
|
+
|
|
142
|
+
```js
|
|
143
|
+
const result = await storage.writeMessage(
|
|
144
|
+
'Backup completed successfully.',
|
|
145
|
+
'@mychannel',
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
console.log(result.messageId);
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Error handling
|
|
152
|
+
|
|
153
|
+
File operations return a result object:
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
{
|
|
157
|
+
success: true,
|
|
158
|
+
files: []
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
If an operation fails, `success` is set to `false` and the error message is
|
|
163
|
+
available in `error`.
|
|
164
|
+
|
|
165
|
+
## Security
|
|
166
|
+
|
|
167
|
+
Never commit your API hash, API ID, or `session.txt` to Git or publish them in
|
|
168
|
+
an npm package. Use environment variables and add local files to `.gitignore`.
|
|
169
|
+
|
|
170
|
+
## License
|
|
171
|
+
|
|
172
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tgbox",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "A lightweight free cloud storage solution using Telegram as backend.",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/johanneshehl/tgBox.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/johanneshehl/tgBox#readme",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/johanneshehl/tgBox/issues"
|
|
13
|
+
},
|
|
6
14
|
"files": [
|
|
7
15
|
"index.js",
|
|
8
16
|
"fileService.js"
|
|
@@ -23,6 +31,7 @@
|
|
|
23
31
|
"axios": "^1.7.9",
|
|
24
32
|
"form-data": "^4.0.1",
|
|
25
33
|
"input": "^1.0.1",
|
|
26
|
-
"telegram": "^2.26.22"
|
|
34
|
+
"telegram": "^2.26.22",
|
|
35
|
+
"tgbox": "^1.0.0"
|
|
27
36
|
}
|
|
28
37
|
}
|