@alemonjs/telegram 0.0.1 → 0.0.4
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 +8 -7
- package/lib/index.d.ts +1 -3
- package/lib/index.js +130 -44
- package/package.json +14 -10
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# [https://
|
|
1
|
+
# [https://alemonjs.com/](https://alemonjs.com/)
|
|
2
2
|
|
|
3
3
|
跨平台开发的事件驱动机器人
|
|
4
4
|
|
|
@@ -14,14 +14,15 @@ yarn add @alemonjs/telegram
|
|
|
14
14
|
|
|
15
15
|
```sh
|
|
16
16
|
telegram:
|
|
17
|
-
# 令牌
|
|
17
|
+
# 令牌 (必填)
|
|
18
18
|
token: ''
|
|
19
|
-
#
|
|
19
|
+
# 代理地址 (推荐填)
|
|
20
|
+
proxy: 'http://127.0.0.1:7890'
|
|
21
|
+
# 主人编号
|
|
20
22
|
master_id: null
|
|
21
|
-
#
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
shard: null
|
|
23
|
+
# other
|
|
24
|
+
base_api_url: null
|
|
25
|
+
request_url: null
|
|
25
26
|
```
|
|
26
27
|
|
|
27
28
|
## Community
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,55 +1,140 @@
|
|
|
1
|
-
import { defineBot, Text, OnProcessor, useParse } from 'alemonjs';
|
|
1
|
+
import { defineBot, getConfig, Text, OnProcessor, useParse } from 'alemonjs';
|
|
2
2
|
import Client from 'node-telegram-bot-api';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
process.env.NODE_NO_WARNINGS = '1';
|
|
5
|
+
var index = defineBot(() => {
|
|
6
|
+
//
|
|
7
|
+
const cfg = getConfig();
|
|
8
|
+
const config = cfg.value?.['telegram'];
|
|
9
|
+
if (!config)
|
|
10
|
+
return;
|
|
5
11
|
//
|
|
6
12
|
const client = new Client(config.token, {
|
|
7
13
|
polling: true,
|
|
8
|
-
baseApiUrl: '',
|
|
14
|
+
baseApiUrl: config?.base_api_url ?? '',
|
|
9
15
|
request: {
|
|
10
|
-
url: '',
|
|
11
|
-
proxy: '
|
|
16
|
+
url: config?.request_url ?? '',
|
|
17
|
+
proxy: config?.proxy ?? ''
|
|
12
18
|
}
|
|
13
19
|
});
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
Platform: 'telegram',
|
|
20
|
-
// 频道
|
|
21
|
-
GuildId: String(event.chat.id),
|
|
22
|
-
// 子频道
|
|
23
|
-
ChannelId: String(event.chat.id),
|
|
24
|
-
// 是否是主人
|
|
25
|
-
IsMaster: false,
|
|
26
|
-
// 用户ID
|
|
27
|
-
UserId: String(event?.sender_chat?.id),
|
|
28
|
-
// 用户名
|
|
29
|
-
UserName: event.sender_chat.username,
|
|
30
|
-
// 用户头像
|
|
31
|
-
UserAvatar: '',
|
|
32
|
-
// 格式化数据
|
|
33
|
-
MsgId: String(event.message_id),
|
|
34
|
-
// 用户消息
|
|
35
|
-
Megs: [Text(event.text ?? '')],
|
|
36
|
-
// 用户openId
|
|
37
|
-
OpenID: 'test',
|
|
38
|
-
// 创建时间
|
|
39
|
-
CreateAt: Date.now(),
|
|
40
|
-
//
|
|
41
|
-
value: null
|
|
42
|
-
};
|
|
43
|
-
// 当访问的时候获取
|
|
44
|
-
Object.defineProperty(e, 'value', {
|
|
45
|
-
get() {
|
|
46
|
-
return event;
|
|
20
|
+
const getUserProfilePhotosUrl = UserId => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
if (!UserId) {
|
|
23
|
+
reject(new Error('UserId 不能为空'));
|
|
24
|
+
return;
|
|
47
25
|
}
|
|
26
|
+
client
|
|
27
|
+
.getUserProfilePhotos(UserId)
|
|
28
|
+
.then(profilePhotos => {
|
|
29
|
+
if (profilePhotos.total_count > 0) {
|
|
30
|
+
// 获取第一张头像的文件 ID
|
|
31
|
+
const fileId = profilePhotos.photos[0][0].file_id;
|
|
32
|
+
// 获取文件信息以获取下载链接
|
|
33
|
+
client
|
|
34
|
+
.getFile(fileId)
|
|
35
|
+
.then(file => {
|
|
36
|
+
const filePath = file.file_path;
|
|
37
|
+
resolve(`https://api.telegram.org/file/bot${config.token}/${filePath}`);
|
|
38
|
+
})
|
|
39
|
+
.catch(reject);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
reject(new Error('用户没有头像'));
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
.catch(reject);
|
|
48
46
|
});
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
};
|
|
48
|
+
//
|
|
49
|
+
client.on('text', async (event) => {
|
|
50
|
+
let UserAvatar = '';
|
|
51
|
+
if (event?.chat.type == 'channel' || event?.chat.type == 'private') {
|
|
52
|
+
const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
|
|
53
|
+
if (typeof photo == 'string')
|
|
54
|
+
UserAvatar = photo;
|
|
55
|
+
}
|
|
56
|
+
if (event?.chat.type == 'channel' || event.chat.type == 'supergroup') {
|
|
57
|
+
// 机器人消息不处理
|
|
58
|
+
if (event.from?.is_bot)
|
|
59
|
+
return;
|
|
60
|
+
// 定义消
|
|
61
|
+
const e = {
|
|
62
|
+
// 事件类型
|
|
63
|
+
Platform: 'telegram',
|
|
64
|
+
// 频道
|
|
65
|
+
GuildId: String(event?.chat.id),
|
|
66
|
+
// 频道名称
|
|
67
|
+
GuildName: event?.chat.title,
|
|
68
|
+
// 子频道
|
|
69
|
+
ChannelId: String(event?.chat.id),
|
|
70
|
+
// 是否是主人
|
|
71
|
+
IsMaster: false,
|
|
72
|
+
// 用户ID
|
|
73
|
+
UserId: String(event?.from?.id),
|
|
74
|
+
// 用户名
|
|
75
|
+
UserName: event?.chat.username,
|
|
76
|
+
// 用户头像
|
|
77
|
+
UserAvatar: UserAvatar,
|
|
78
|
+
// 格式化数据
|
|
79
|
+
MsgId: String(event?.message_id),
|
|
80
|
+
// 用户消息
|
|
81
|
+
Megs: [Text(event?.text)],
|
|
82
|
+
// 用户openId
|
|
83
|
+
OpenID: String(event.chat?.id),
|
|
84
|
+
// 创建时间
|
|
85
|
+
CreateAt: Date.now(),
|
|
86
|
+
//
|
|
87
|
+
tag: 'txt',
|
|
88
|
+
//
|
|
89
|
+
value: null
|
|
90
|
+
};
|
|
91
|
+
// 当访问的时候获取
|
|
92
|
+
Object.defineProperty(e, 'value', {
|
|
93
|
+
get() {
|
|
94
|
+
return event;
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
//
|
|
98
|
+
OnProcessor(e, 'message.create');
|
|
99
|
+
//
|
|
100
|
+
}
|
|
101
|
+
else if (event?.chat.type == 'private') {
|
|
102
|
+
// 定义消
|
|
103
|
+
const e = {
|
|
104
|
+
// 事件类型
|
|
105
|
+
Platform: 'telegram',
|
|
106
|
+
// 是否是主人
|
|
107
|
+
IsMaster: false,
|
|
108
|
+
// 用户ID
|
|
109
|
+
UserId: String(event?.from.id),
|
|
110
|
+
// 用户名
|
|
111
|
+
UserName: event?.from?.username,
|
|
112
|
+
// 用户头像
|
|
113
|
+
UserAvatar: UserAvatar,
|
|
114
|
+
// 格式化数据
|
|
115
|
+
MsgId: String(event?.message_id),
|
|
116
|
+
// 用户消息
|
|
117
|
+
Megs: [Text(event?.text)],
|
|
118
|
+
// 用户openId
|
|
119
|
+
OpenID: String(event.chat?.id),
|
|
120
|
+
// 创建时间
|
|
121
|
+
CreateAt: Date.now(),
|
|
122
|
+
//
|
|
123
|
+
tag: 'txt',
|
|
124
|
+
//
|
|
125
|
+
value: null
|
|
126
|
+
};
|
|
127
|
+
// 当访问的时候获取
|
|
128
|
+
Object.defineProperty(e, 'value', {
|
|
129
|
+
get() {
|
|
130
|
+
return event;
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
// 处理消息
|
|
134
|
+
OnProcessor(e, 'private.message.create');
|
|
135
|
+
}
|
|
51
136
|
});
|
|
52
|
-
// const eventKeys
|
|
137
|
+
// const eventKeys = [
|
|
53
138
|
// 'message',
|
|
54
139
|
// 'text',
|
|
55
140
|
// 'animation',
|
|
@@ -110,7 +195,7 @@ var index = defineBot(config => {
|
|
|
110
195
|
// 'webhook_error',
|
|
111
196
|
// 'chat_join_request'
|
|
112
197
|
// ]
|
|
113
|
-
// 打印全部的消息
|
|
198
|
+
// // 打印全部的消息
|
|
114
199
|
// for (const key of eventKeys) {
|
|
115
200
|
// client.on(key, event => {
|
|
116
201
|
// console.log(key, event)
|
|
@@ -125,12 +210,13 @@ var index = defineBot(config => {
|
|
|
125
210
|
if (val.length < 0)
|
|
126
211
|
return Promise.all([]);
|
|
127
212
|
const content = useParse(val, 'Text');
|
|
213
|
+
const e = event.value;
|
|
128
214
|
if (content) {
|
|
129
|
-
return Promise.all([content].map(item => client.sendMessage(
|
|
215
|
+
return Promise.all([content].map(item => client.sendMessage(e.chat.id, item)));
|
|
130
216
|
}
|
|
131
217
|
const images = useParse(val, 'Image');
|
|
132
218
|
if (images) {
|
|
133
|
-
return Promise.all(images.map(item => client.sendPhoto(
|
|
219
|
+
return Promise.all(images.map(item => client.sendPhoto(e.chat.id, item)));
|
|
134
220
|
}
|
|
135
221
|
return Promise.all([]);
|
|
136
222
|
}
|
package/package.json
CHANGED
|
@@ -1,27 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alemonjs/telegram",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "telegram-bot",
|
|
5
|
-
"author": "
|
|
5
|
+
"author": "lemonade",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
|
+
"types": "lib",
|
|
9
10
|
"dependencies": {
|
|
10
|
-
"chat-space": "^0.0.4",
|
|
11
11
|
"grammy": "^1.30.0",
|
|
12
12
|
"node-telegram-bot-api": "^0.66.0"
|
|
13
13
|
},
|
|
14
|
-
"
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@types/node-telegram-bot-api": "^0.64.7"
|
|
16
|
+
},
|
|
15
17
|
"exports": {
|
|
16
18
|
".": {
|
|
17
19
|
"import": "./lib/index.js",
|
|
18
20
|
"types": "./lib/index.d.ts"
|
|
19
21
|
}
|
|
20
22
|
},
|
|
21
|
-
"publishConfig": {
|
|
22
|
-
"access": "public",
|
|
23
|
-
"registry": "https://registry.npmjs.org"
|
|
24
|
-
},
|
|
25
23
|
"keywords": [
|
|
26
24
|
"alemonjs",
|
|
27
25
|
"telegram",
|
|
@@ -29,7 +27,13 @@
|
|
|
29
27
|
"bot",
|
|
30
28
|
"chat-bot"
|
|
31
29
|
],
|
|
32
|
-
"
|
|
33
|
-
"
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"registry": "https://registry.npmjs.org",
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"bugs": "https://github.com/ningmengchongshui/alemonjs/issues",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "https://github.com/ningmengchongshui/alemonjs.git"
|
|
34
38
|
}
|
|
35
39
|
}
|