@alemonjs/telegram 0.0.3 → 0.0.5

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 (3) hide show
  1. package/README.md +4 -3
  2. package/lib/index.js +151 -20
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -16,12 +16,13 @@ yarn add @alemonjs/telegram
16
16
  telegram:
17
17
  # 令牌 (必填)
18
18
  token: ''
19
+ # 代理地址 (推荐填)
20
+ proxy: 'http://127.0.0.1:7890'
19
21
  # 主人编号
20
22
  master_id: null
23
+ # other
21
24
  base_api_url: null
22
- # 代理地址
23
- # proxy: 'http://127.0.0.1:7890'
24
- proxy: null
25
+ request_url: null
25
26
  ```
26
27
 
27
28
  ## Community
package/lib/index.js CHANGED
@@ -2,6 +2,7 @@ import { defineBot, getConfig, Text, OnProcessor, useParse } from 'alemonjs';
2
2
  import Client from 'node-telegram-bot-api';
3
3
 
4
4
  var index = defineBot(() => {
5
+ //
5
6
  const cfg = getConfig();
6
7
  const config = cfg.value?.['telegram'];
7
8
  if (!config)
@@ -9,40 +10,170 @@ var index = defineBot(() => {
9
10
  //
10
11
  const client = new Client(config.token, {
11
12
  polling: true,
12
- baseApiUrl: '',
13
+ baseApiUrl: config?.base_api_url ?? '',
13
14
  request: {
14
- url: '',
15
- proxy: config?.proxy ?? 'http://127.0.0.1:7890'
15
+ url: config?.request_url ?? '',
16
+ proxy: config?.proxy ?? ''
16
17
  }
17
18
  });
19
+ const getUserProfilePhotosUrl = (UserId) => {
20
+ return new Promise((resolve, reject) => {
21
+ if (!UserId) {
22
+ reject(new Error('UserId 不能为空'));
23
+ return;
24
+ }
25
+ client
26
+ .getUserProfilePhotos(UserId)
27
+ .then(profilePhotos => {
28
+ if (profilePhotos.total_count > 0) {
29
+ // 获取第一张头像的文件 ID
30
+ const fileId = profilePhotos.photos[0][0].file_id;
31
+ // 获取文件信息以获取下载链接
32
+ client
33
+ .getFile(fileId)
34
+ .then(file => {
35
+ const filePath = file.file_path;
36
+ resolve(`https://api.telegram.org/file/bot${config.token}/${filePath}`);
37
+ })
38
+ .catch(reject);
39
+ }
40
+ else {
41
+ reject(new Error('用户没有头像'));
42
+ }
43
+ })
44
+ .catch(reject);
45
+ });
46
+ };
18
47
  //
19
- //
20
- client.on('message', event => {
48
+ client.on('text', async (event) => {
49
+ let UserAvatar = '';
50
+ if (event?.chat.type == 'supergroup' || event?.chat.type == 'private') {
51
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
52
+ if (typeof photo == 'string')
53
+ UserAvatar = photo;
54
+ }
55
+ if (event?.chat.type == 'channel' || event?.chat.type == 'supergroup') {
56
+ // 机器人消息不处理
57
+ if (event?.from?.is_bot)
58
+ return;
59
+ // 定义消
60
+ const e = {
61
+ // 事件类型
62
+ Platform: 'telegram',
63
+ // 频道
64
+ GuildId: String(event?.chat.id),
65
+ // 频道名称
66
+ GuildName: event?.chat.title,
67
+ // 子频道
68
+ ChannelId: String(event?.chat.id),
69
+ GuildIdAvatar: '',
70
+ GuildIdName: '',
71
+ // 是否是主人
72
+ IsMaster: false,
73
+ // 用户ID
74
+ UserId: String(event?.from?.id),
75
+ // 用户名
76
+ UserName: event?.chat.username,
77
+ // 用户头像
78
+ UserAvatar: UserAvatar,
79
+ // 格式化数据
80
+ MsgId: String(event?.message_id),
81
+ // 用户消息
82
+ Megs: [Text(event?.text)],
83
+ // 用户openId
84
+ OpenID: String(event?.chat?.id),
85
+ // 创建时间
86
+ CreateAt: Date.now(),
87
+ //
88
+ tag: 'txt',
89
+ //
90
+ value: null
91
+ };
92
+ // 当访问的时候获取
93
+ Object.defineProperty(e, 'value', {
94
+ get() {
95
+ return event;
96
+ }
97
+ });
98
+ //
99
+ OnProcessor(e, 'message.create');
100
+ //
101
+ }
102
+ else if (event?.chat.type == 'private') {
103
+ // 定义消
104
+ const e = {
105
+ // 事件类型
106
+ Platform: 'telegram',
107
+ // 是否是主人
108
+ IsMaster: false,
109
+ // 用户ID
110
+ UserId: String(event?.from.id),
111
+ // 用户名
112
+ UserName: event?.from?.username,
113
+ // 用户头像
114
+ UserAvatar: UserAvatar,
115
+ // 格式化数据
116
+ MsgId: String(event?.message_id),
117
+ // 用户消息
118
+ Megs: [Text(event?.text)],
119
+ // 用户openId
120
+ OpenID: String(event?.chat?.id),
121
+ // 创建时间
122
+ CreateAt: Date.now(),
123
+ //
124
+ tag: 'txt',
125
+ //
126
+ value: null
127
+ };
128
+ // 当访问的时候获取
129
+ Object.defineProperty(e, 'value', {
130
+ get() {
131
+ return event;
132
+ }
133
+ });
134
+ // 处理消息
135
+ OnProcessor(e, 'private.message.create');
136
+ }
137
+ });
138
+ client.on('new_chat_members', async (event) => {
139
+ // 机器人消息不处理
140
+ if (event?.from.is_bot)
141
+ return;
142
+ let UserAvatar = '';
143
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
144
+ if (typeof photo == 'string')
145
+ UserAvatar = photo;
21
146
  // 定义消
22
147
  const e = {
23
148
  // 事件类型
24
149
  Platform: 'telegram',
25
150
  // 频道
26
- GuildId: String(event.chat.id),
151
+ GuildId: String(event?.chat.id),
152
+ // 频道名称
153
+ GuildName: event?.chat.title,
27
154
  // 子频道
28
- ChannelId: String(event.chat.id),
155
+ ChannelId: String(event?.chat.id),
156
+ GuildIdAvatar: '',
157
+ GuildIdName: '',
29
158
  // 是否是主人
30
159
  IsMaster: false,
31
160
  // 用户ID
32
- UserId: String(event?.sender_chat?.id),
161
+ UserId: String(event?.from?.id),
33
162
  // 用户名
34
- UserName: event.sender_chat.username,
163
+ UserName: event?.chat.username,
35
164
  // 用户头像
36
- UserAvatar: '',
165
+ UserAvatar: UserAvatar,
37
166
  // 格式化数据
38
- MsgId: String(event.message_id),
167
+ MsgId: String(event?.message_id),
39
168
  // 用户消息
40
- Megs: [Text(event.text ?? '')],
169
+ Megs: [Text(event?.text)],
41
170
  // 用户openId
42
- OpenID: 'test',
171
+ OpenID: String(event?.chat?.id),
43
172
  // 创建时间
44
173
  CreateAt: Date.now(),
45
174
  //
175
+ tag: 'txt',
176
+ //
46
177
  value: null
47
178
  };
48
179
  // 当访问的时候获取
@@ -51,10 +182,10 @@ var index = defineBot(() => {
51
182
  return event;
52
183
  }
53
184
  });
54
- // 处理消息
55
- OnProcessor(e, 'private.message.create');
185
+ //
186
+ OnProcessor(e, 'member.add');
56
187
  });
57
- // const eventKeys: string[] = [
188
+ // const eventKeys = [
58
189
  // 'message',
59
190
  // 'text',
60
191
  // 'animation',
@@ -115,13 +246,12 @@ var index = defineBot(() => {
115
246
  // 'webhook_error',
116
247
  // 'chat_join_request'
117
248
  // ]
118
- // 打印全部的消息
249
+ // // 打印全部的消息
119
250
  // for (const key of eventKeys) {
120
251
  // client.on(key, event => {
121
252
  // console.log(key, event)
122
253
  // })
123
254
  // }
124
- //
125
255
  return {
126
256
  api: {
127
257
  use: {
@@ -131,12 +261,13 @@ var index = defineBot(() => {
131
261
  if (val.length < 0)
132
262
  return Promise.all([]);
133
263
  const content = useParse(val, 'Text');
264
+ const e = event?.value;
134
265
  if (content) {
135
- return Promise.all([content].map(item => client.sendMessage(event.GuildId, item)));
266
+ return Promise.all([content].map(item => client.sendMessage(e.chat.id, item)));
136
267
  }
137
268
  const images = useParse(val, 'Image');
138
269
  if (images) {
139
- return Promise.all(images.map(item => client.sendPhoto(event.GuildId, item)));
270
+ return Promise.all(images.map(item => client.sendPhoto(e.chat.id, item)));
140
271
  }
141
272
  return Promise.all([]);
142
273
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/telegram",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "telegram-bot",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
@@ -8,7 +8,6 @@
8
8
  "main": "lib/index.js",
9
9
  "types": "lib",
10
10
  "dependencies": {
11
- "chat-space": "^0.0.4",
12
11
  "grammy": "^1.30.0",
13
12
  "node-telegram-bot-api": "^0.66.0"
14
13
  },
@@ -29,7 +28,8 @@
29
28
  "chat-bot"
30
29
  ],
31
30
  "publishConfig": {
32
- "registry": "https://registry.npmjs.org"
31
+ "registry": "https://registry.npmjs.org",
32
+ "access": "public"
33
33
  },
34
34
  "bugs": "https://github.com/ningmengchongshui/alemonjs/issues",
35
35
  "repository": {