@alemonjs/telegram 0.0.5 → 0.1.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/lib/index.d.ts +5 -1
- package/lib/index.js +82 -65
- package/package.json +3 -3
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import TelegramClient from 'node-telegram-bot-api';
|
|
2
|
+
|
|
3
|
+
type Client = typeof TelegramClient.prototype;
|
|
4
|
+
declare const client: Client;
|
|
1
5
|
declare const _default: () => typeof global.alemonjs;
|
|
2
6
|
|
|
3
|
-
export { _default as default };
|
|
7
|
+
export { type Client, client, _default as default };
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { defineBot, getConfig, Text, OnProcessor, useParse } from 'alemonjs';
|
|
2
|
-
import
|
|
1
|
+
import { defineBot, getConfig, createHash, Text, OnProcessor, useParse } from 'alemonjs';
|
|
2
|
+
import TelegramClient from 'node-telegram-bot-api';
|
|
3
3
|
|
|
4
|
+
const client = global.client;
|
|
4
5
|
var index = defineBot(() => {
|
|
5
6
|
//
|
|
6
7
|
const cfg = getConfig();
|
|
@@ -8,7 +9,7 @@ var index = defineBot(() => {
|
|
|
8
9
|
if (!config)
|
|
9
10
|
return;
|
|
10
11
|
//
|
|
11
|
-
const client = new
|
|
12
|
+
const client = new TelegramClient(config.token, {
|
|
12
13
|
polling: true,
|
|
13
14
|
baseApiUrl: config?.base_api_url ?? '',
|
|
14
15
|
request: {
|
|
@@ -26,7 +27,7 @@ var index = defineBot(() => {
|
|
|
26
27
|
.getUserProfilePhotos(UserId)
|
|
27
28
|
.then(profilePhotos => {
|
|
28
29
|
if (profilePhotos.total_count > 0) {
|
|
29
|
-
// 获取第一张头像的文件
|
|
30
|
+
// 获取第一张头像的文件 Id
|
|
30
31
|
const fileId = profilePhotos.photos[0][0].file_id;
|
|
31
32
|
// 获取文件信息以获取下载链接
|
|
32
33
|
client
|
|
@@ -46,11 +47,28 @@ var index = defineBot(() => {
|
|
|
46
47
|
};
|
|
47
48
|
//
|
|
48
49
|
client.on('text', async (event) => {
|
|
49
|
-
let UserAvatar =
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if (
|
|
53
|
-
|
|
50
|
+
let UserAvatar = undefined;
|
|
51
|
+
const UserKey = createHash(`telegram:${event?.from?.id}`);
|
|
52
|
+
try {
|
|
53
|
+
if (event?.chat.type == 'supergroup' || event?.chat.type == 'private') {
|
|
54
|
+
const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
|
|
55
|
+
if (typeof photo == 'string') {
|
|
56
|
+
UserAvatar = {
|
|
57
|
+
toBuffer: async () => {
|
|
58
|
+
const arrayBuffer = await fetch(photo).then(res => res.arrayBuffer());
|
|
59
|
+
return Buffer.from(arrayBuffer);
|
|
60
|
+
},
|
|
61
|
+
toURL: Promise.resolve(photo),
|
|
62
|
+
toBase64: async () => {
|
|
63
|
+
const arrayBuffer = await fetch(photo).then(res => res.arrayBuffer());
|
|
64
|
+
return Buffer.from(arrayBuffer).toString('base64');
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
console.error(e);
|
|
54
72
|
}
|
|
55
73
|
if (event?.chat.type == 'channel' || event?.chat.type == 'supergroup') {
|
|
56
74
|
// 机器人消息不处理
|
|
@@ -62,31 +80,21 @@ var index = defineBot(() => {
|
|
|
62
80
|
Platform: 'telegram',
|
|
63
81
|
// 频道
|
|
64
82
|
GuildId: String(event?.chat.id),
|
|
65
|
-
// 频道名称
|
|
66
83
|
GuildName: event?.chat.title,
|
|
67
|
-
// 子频道
|
|
68
84
|
ChannelId: String(event?.chat.id),
|
|
69
|
-
|
|
70
|
-
GuildIdName: '',
|
|
71
|
-
// 是否是主人
|
|
72
|
-
IsMaster: false,
|
|
73
|
-
// 用户ID
|
|
85
|
+
// user
|
|
74
86
|
UserId: String(event?.from?.id),
|
|
75
|
-
|
|
87
|
+
UserKey: UserKey,
|
|
76
88
|
UserName: event?.chat.username,
|
|
77
|
-
// 用户头像
|
|
78
89
|
UserAvatar: UserAvatar,
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
OpenID: String(event?.chat?.id),
|
|
85
|
-
// 创建时间
|
|
90
|
+
IsMaster: false,
|
|
91
|
+
// message
|
|
92
|
+
MessageId: String(event?.message_id),
|
|
93
|
+
MessageBody: [Text(event?.text)],
|
|
94
|
+
OpenId: String(event?.chat?.id),
|
|
86
95
|
CreateAt: Date.now(),
|
|
87
|
-
//
|
|
96
|
+
// other
|
|
88
97
|
tag: 'txt',
|
|
89
|
-
//
|
|
90
98
|
value: null
|
|
91
99
|
};
|
|
92
100
|
// 当访问的时候获取
|
|
@@ -104,25 +112,20 @@ var index = defineBot(() => {
|
|
|
104
112
|
const e = {
|
|
105
113
|
// 事件类型
|
|
106
114
|
Platform: 'telegram',
|
|
107
|
-
//
|
|
108
|
-
IsMaster: false,
|
|
109
|
-
// 用户ID
|
|
115
|
+
// 用户Id
|
|
110
116
|
UserId: String(event?.from.id),
|
|
111
|
-
|
|
117
|
+
UserKey: UserKey,
|
|
112
118
|
UserName: event?.from?.username,
|
|
113
|
-
// 用户头像
|
|
114
119
|
UserAvatar: UserAvatar,
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
// 创建时间
|
|
120
|
+
IsMaster: false,
|
|
121
|
+
// message
|
|
122
|
+
MessageId: String(event?.message_id),
|
|
123
|
+
MessageBody: [Text(event?.text)],
|
|
124
|
+
MessageText: event?.text,
|
|
125
|
+
OpenId: String(event?.chat?.id),
|
|
122
126
|
CreateAt: Date.now(),
|
|
123
|
-
//
|
|
127
|
+
// other
|
|
124
128
|
tag: 'txt',
|
|
125
|
-
//
|
|
126
129
|
value: null
|
|
127
130
|
};
|
|
128
131
|
// 当访问的时候获取
|
|
@@ -139,37 +142,46 @@ var index = defineBot(() => {
|
|
|
139
142
|
// 机器人消息不处理
|
|
140
143
|
if (event?.from.is_bot)
|
|
141
144
|
return;
|
|
142
|
-
let UserAvatar =
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
145
|
+
let UserAvatar = undefined;
|
|
146
|
+
try {
|
|
147
|
+
const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
|
|
148
|
+
if (typeof photo == 'string') {
|
|
149
|
+
UserAvatar = {
|
|
150
|
+
toBuffer: async () => {
|
|
151
|
+
const arrayBuffer = await fetch(photo).then(res => res.arrayBuffer());
|
|
152
|
+
return Buffer.from(arrayBuffer);
|
|
153
|
+
},
|
|
154
|
+
toURL: Promise.resolve(photo),
|
|
155
|
+
toBase64: async () => {
|
|
156
|
+
const arrayBuffer = await fetch(photo).then(res => res.arrayBuffer());
|
|
157
|
+
return Buffer.from(arrayBuffer).toString('base64');
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
catch (e) {
|
|
163
|
+
console.error(e);
|
|
164
|
+
}
|
|
165
|
+
const UserKey = createHash(`telegram:${event?.from?.id}`);
|
|
146
166
|
// 定义消
|
|
147
167
|
const e = {
|
|
148
168
|
// 事件类型
|
|
149
169
|
Platform: 'telegram',
|
|
150
|
-
//
|
|
170
|
+
// guild
|
|
151
171
|
GuildId: String(event?.chat.id),
|
|
152
|
-
// 频道名称
|
|
153
172
|
GuildName: event?.chat.title,
|
|
154
|
-
// 子频道
|
|
155
173
|
ChannelId: String(event?.chat.id),
|
|
156
|
-
|
|
157
|
-
GuildIdName: '',
|
|
158
|
-
// 是否是主人
|
|
159
|
-
IsMaster: false,
|
|
160
|
-
// 用户ID
|
|
174
|
+
// 用户Id
|
|
161
175
|
UserId: String(event?.from?.id),
|
|
162
|
-
|
|
176
|
+
UserKey: UserKey,
|
|
163
177
|
UserName: event?.chat.username,
|
|
164
|
-
// 用户头像
|
|
165
178
|
UserAvatar: UserAvatar,
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
// 创建时间
|
|
179
|
+
IsMaster: false,
|
|
180
|
+
// message
|
|
181
|
+
MessageId: String(event?.message_id),
|
|
182
|
+
MessageBody: [Text(event?.text)],
|
|
183
|
+
MessageText: event?.text,
|
|
184
|
+
OpenId: String(event?.chat?.id),
|
|
173
185
|
CreateAt: Date.now(),
|
|
174
186
|
//
|
|
175
187
|
tag: 'txt',
|
|
@@ -252,6 +264,7 @@ var index = defineBot(() => {
|
|
|
252
264
|
// console.log(key, event)
|
|
253
265
|
// })
|
|
254
266
|
// }
|
|
267
|
+
global.client = client;
|
|
255
268
|
return {
|
|
256
269
|
api: {
|
|
257
270
|
use: {
|
|
@@ -260,12 +273,16 @@ var index = defineBot(() => {
|
|
|
260
273
|
return Promise.all([]);
|
|
261
274
|
if (val.length < 0)
|
|
262
275
|
return Promise.all([]);
|
|
263
|
-
const content = useParse(
|
|
276
|
+
const content = useParse({
|
|
277
|
+
MessageBody: val
|
|
278
|
+
}, 'Text');
|
|
264
279
|
const e = event?.value;
|
|
265
280
|
if (content) {
|
|
266
281
|
return Promise.all([content].map(item => client.sendMessage(e.chat.id, item)));
|
|
267
282
|
}
|
|
268
|
-
const images = useParse(
|
|
283
|
+
const images = useParse({
|
|
284
|
+
MessageBody: val
|
|
285
|
+
}, 'Image');
|
|
269
286
|
if (images) {
|
|
270
287
|
return Promise.all(images.map(item => client.sendPhoto(e.chat.id, item)));
|
|
271
288
|
}
|
|
@@ -276,4 +293,4 @@ var index = defineBot(() => {
|
|
|
276
293
|
};
|
|
277
294
|
});
|
|
278
295
|
|
|
279
|
-
export { index as default };
|
|
296
|
+
export { client, index as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alemonjs/telegram",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "telegram-bot",
|
|
5
5
|
"author": "lemonade",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,9 +31,9 @@
|
|
|
31
31
|
"registry": "https://registry.npmjs.org",
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
34
|
-
"bugs": "https://github.com/
|
|
34
|
+
"bugs": "https://github.com/lemonade-lab/alemonjs/issues",
|
|
35
35
|
"repository": {
|
|
36
36
|
"type": "git",
|
|
37
|
-
"url": "https://github.com/
|
|
37
|
+
"url": "https://github.com/lemonade-lab/alemonjs.git"
|
|
38
38
|
}
|
|
39
39
|
}
|