@alemonjs/telegram 0.0.4 → 0.1.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/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,7 +1,7 @@
1
1
  import { defineBot, getConfig, Text, OnProcessor, useParse } from 'alemonjs';
2
- import Client from 'node-telegram-bot-api';
2
+ import TelegramClient from 'node-telegram-bot-api';
3
3
 
4
- process.env.NODE_NO_WARNINGS = '1';
4
+ const client = global.client;
5
5
  var index = defineBot(() => {
6
6
  //
7
7
  const cfg = getConfig();
@@ -9,7 +9,7 @@ var index = defineBot(() => {
9
9
  if (!config)
10
10
  return;
11
11
  //
12
- const client = new Client(config.token, {
12
+ const client = new TelegramClient(config.token, {
13
13
  polling: true,
14
14
  baseApiUrl: config?.base_api_url ?? '',
15
15
  request: {
@@ -17,7 +17,7 @@ var index = defineBot(() => {
17
17
  proxy: config?.proxy ?? ''
18
18
  }
19
19
  });
20
- const getUserProfilePhotosUrl = UserId => {
20
+ const getUserProfilePhotosUrl = (UserId) => {
21
21
  return new Promise((resolve, reject) => {
22
22
  if (!UserId) {
23
23
  reject(new Error('UserId 不能为空'));
@@ -48,14 +48,14 @@ var index = defineBot(() => {
48
48
  //
49
49
  client.on('text', async (event) => {
50
50
  let UserAvatar = '';
51
- if (event?.chat.type == 'channel' || event?.chat.type == 'private') {
51
+ if (event?.chat.type == 'supergroup' || event?.chat.type == 'private') {
52
52
  const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
53
53
  if (typeof photo == 'string')
54
54
  UserAvatar = photo;
55
55
  }
56
- if (event?.chat.type == 'channel' || event.chat.type == 'supergroup') {
56
+ if (event?.chat.type == 'channel' || event?.chat.type == 'supergroup') {
57
57
  // 机器人消息不处理
58
- if (event.from?.is_bot)
58
+ if (event?.from?.is_bot)
59
59
  return;
60
60
  // 定义消
61
61
  const e = {
@@ -67,6 +67,8 @@ var index = defineBot(() => {
67
67
  GuildName: event?.chat.title,
68
68
  // 子频道
69
69
  ChannelId: String(event?.chat.id),
70
+ GuildIdAvatar: '',
71
+ GuildIdName: '',
70
72
  // 是否是主人
71
73
  IsMaster: false,
72
74
  // 用户ID
@@ -78,9 +80,9 @@ var index = defineBot(() => {
78
80
  // 格式化数据
79
81
  MsgId: String(event?.message_id),
80
82
  // 用户消息
81
- Megs: [Text(event?.text)],
83
+ MessageBody: [Text(event?.text)],
82
84
  // 用户openId
83
- OpenID: String(event.chat?.id),
85
+ OpenID: String(event?.chat?.id),
84
86
  // 创建时间
85
87
  CreateAt: Date.now(),
86
88
  //
@@ -109,14 +111,18 @@ var index = defineBot(() => {
109
111
  UserId: String(event?.from.id),
110
112
  // 用户名
111
113
  UserName: event?.from?.username,
114
+ GuildIdName: '',
115
+ GuildIdAvatar: '',
116
+ ChannelName: '',
112
117
  // 用户头像
113
118
  UserAvatar: UserAvatar,
114
119
  // 格式化数据
115
120
  MsgId: String(event?.message_id),
116
121
  // 用户消息
117
- Megs: [Text(event?.text)],
122
+ MessageBody: [Text(event?.text)],
123
+ MessageText: event?.text,
118
124
  // 用户openId
119
- OpenID: String(event.chat?.id),
125
+ OpenID: String(event?.chat?.id),
120
126
  // 创建时间
121
127
  CreateAt: Date.now(),
122
128
  //
@@ -134,6 +140,58 @@ var index = defineBot(() => {
134
140
  OnProcessor(e, 'private.message.create');
135
141
  }
136
142
  });
143
+ client.on('new_chat_members', async (event) => {
144
+ // 机器人消息不处理
145
+ if (event?.from.is_bot)
146
+ return;
147
+ let UserAvatar = '';
148
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
149
+ if (typeof photo == 'string')
150
+ UserAvatar = photo;
151
+ // 定义消
152
+ const e = {
153
+ // 事件类型
154
+ Platform: 'telegram',
155
+ // 频道
156
+ GuildId: String(event?.chat.id),
157
+ // 频道名称
158
+ GuildName: event?.chat.title,
159
+ // 子频道
160
+ ChannelId: String(event?.chat.id),
161
+ GuildIdAvatar: '',
162
+ GuildIdName: '',
163
+ // 是否是主人
164
+ IsMaster: false,
165
+ // 用户ID
166
+ UserId: String(event?.from?.id),
167
+ // 用户名
168
+ UserName: event?.chat.username,
169
+ // 用户头像
170
+ UserAvatar: UserAvatar,
171
+ // 格式化数据
172
+ MsgId: String(event?.message_id),
173
+ // 用户消息
174
+ MessageBody: [Text(event?.text)],
175
+ //
176
+ MessageText: event?.text,
177
+ // 用户openId
178
+ OpenID: String(event?.chat?.id),
179
+ // 创建时间
180
+ CreateAt: Date.now(),
181
+ //
182
+ tag: 'txt',
183
+ //
184
+ value: null
185
+ };
186
+ // 当访问的时候获取
187
+ Object.defineProperty(e, 'value', {
188
+ get() {
189
+ return event;
190
+ }
191
+ });
192
+ //
193
+ OnProcessor(e, 'member.add');
194
+ });
137
195
  // const eventKeys = [
138
196
  // 'message',
139
197
  // 'text',
@@ -201,6 +259,7 @@ var index = defineBot(() => {
201
259
  // console.log(key, event)
202
260
  // })
203
261
  // }
262
+ global.client = client;
204
263
  return {
205
264
  api: {
206
265
  use: {
@@ -209,12 +268,16 @@ var index = defineBot(() => {
209
268
  return Promise.all([]);
210
269
  if (val.length < 0)
211
270
  return Promise.all([]);
212
- const content = useParse(val, 'Text');
213
- const e = event.value;
271
+ const content = useParse({
272
+ MessageBody: val
273
+ }, 'Text');
274
+ const e = event?.value;
214
275
  if (content) {
215
276
  return Promise.all([content].map(item => client.sendMessage(e.chat.id, item)));
216
277
  }
217
- const images = useParse(val, 'Image');
278
+ const images = useParse({
279
+ MessageBody: val
280
+ }, 'Image');
218
281
  if (images) {
219
282
  return Promise.all(images.map(item => client.sendPhoto(e.chat.id, item)));
220
283
  }
@@ -225,4 +288,4 @@ var index = defineBot(() => {
225
288
  };
226
289
  });
227
290
 
228
- export { index as default };
291
+ export { client, index as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/telegram",
3
- "version": "0.0.4",
3
+ "version": "0.1.0",
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/ningmengchongshui/alemonjs/issues",
34
+ "bugs": "https://github.com/lemonade-lab/alemonjs/issues",
35
35
  "repository": {
36
36
  "type": "git",
37
- "url": "https://github.com/ningmengchongshui/alemonjs.git"
37
+ "url": "https://github.com/lemonade-lab/alemonjs.git"
38
38
  }
39
39
  }