@alemonjs/telegram 0.1.1 → 0.2.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 CHANGED
@@ -19,7 +19,7 @@ telegram:
19
19
  # 代理地址 (推荐填)
20
20
  proxy: 'http://127.0.0.1:7890'
21
21
  # 主人编号
22
- master_id: null
22
+ master_key: null
23
23
  # other
24
24
  base_api_url: null
25
25
  request_url: null
package/lib/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
+ import * as alemonjs from 'alemonjs';
1
2
  import TelegramClient from 'node-telegram-bot-api';
2
3
 
3
4
  type Client = typeof TelegramClient.prototype;
4
5
  declare const client: Client;
5
- declare const _default: () => typeof global.alemonjs;
6
+ declare const platform = "telegram";
7
+ declare const _default: () => alemonjs.ClientAPI;
6
8
 
7
- export { type Client, client, _default as default };
9
+ export { type Client, client, _default as default, platform };
package/lib/index.js CHANGED
@@ -1,11 +1,18 @@
1
- import { defineBot, getConfig, createHash, Text, OnProcessor, useParse } from 'alemonjs';
1
+ import { defineBot, getConfigValue, useUserHashKey, OnProcessor } from 'alemonjs';
2
2
  import TelegramClient from 'node-telegram-bot-api';
3
3
 
4
- const client = global.client;
4
+ const client = new Proxy({}, {
5
+ get: (_, prop) => {
6
+ if (prop in global.client) {
7
+ return global.client[prop];
8
+ }
9
+ return undefined;
10
+ }
11
+ });
12
+ const platform = 'telegram';
5
13
  var index = defineBot(() => {
6
- //
7
- const cfg = getConfig();
8
- const config = cfg.value?.['telegram'];
14
+ const value = getConfigValue();
15
+ const config = value?.telegram;
9
16
  if (!config)
10
17
  return;
11
18
  //
@@ -17,6 +24,11 @@ var index = defineBot(() => {
17
24
  proxy: config?.proxy ?? ''
18
25
  }
19
26
  });
27
+ /**
28
+ *
29
+ * @param UserId
30
+ * @returns
31
+ */
20
32
  const getUserProfilePhotosUrl = (UserId) => {
21
33
  return new Promise((resolve, reject) => {
22
34
  if (!UserId) {
@@ -45,31 +57,43 @@ var index = defineBot(() => {
45
57
  .catch(reject);
46
58
  });
47
59
  };
48
- //
49
60
  client.on('text', async (event) => {
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
- };
61
+ const UserId = String(event?.from?.id);
62
+ const UserKey = useUserHashKey({
63
+ Platform: platform,
64
+ UserId: UserId
65
+ });
66
+ const UserAvatar = {
67
+ toBuffer: async () => {
68
+ if (event?.chat.type == 'supergroup' || event?.chat.type == 'private') {
69
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
70
+ if (typeof photo == 'string') {
71
+ const arrayBuffer = await fetch(photo).then(res => res.arrayBuffer());
72
+ return Buffer.from(arrayBuffer);
73
+ }
74
+ }
75
+ return;
76
+ },
77
+ toURL: async () => {
78
+ if (event?.chat.type == 'supergroup' || event?.chat.type == 'private') {
79
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
80
+ if (typeof photo == 'string') {
81
+ return photo;
82
+ }
83
+ }
84
+ return;
85
+ },
86
+ toBase64: async () => {
87
+ if (event?.chat.type == 'supergroup' || event?.chat.type == 'private') {
88
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
89
+ if (typeof photo == 'string') {
90
+ const arrayBuffer = await fetch(photo).then(res => res.arrayBuffer());
91
+ return Buffer.from(arrayBuffer).toString('base64');
92
+ }
67
93
  }
94
+ return;
68
95
  }
69
- }
70
- catch (e) {
71
- console.error(e);
72
- }
96
+ };
73
97
  if (event?.chat.type == 'channel' || event?.chat.type == 'supergroup') {
74
98
  // 机器人消息不处理
75
99
  if (event?.from?.is_bot)
@@ -77,20 +101,19 @@ var index = defineBot(() => {
77
101
  // 定义消
78
102
  const e = {
79
103
  // 事件类型
80
- Platform: 'telegram',
104
+ Platform: platform,
81
105
  // 频道
82
106
  GuildId: String(event?.chat.id),
83
- GuildName: event?.chat.title,
84
107
  ChannelId: String(event?.chat.id),
85
108
  // user
86
- UserId: String(event?.from?.id),
109
+ UserId: UserId,
87
110
  UserKey: UserKey,
88
111
  UserName: event?.chat.username,
89
112
  UserAvatar: UserAvatar,
90
113
  IsMaster: false,
114
+ IsBot: false,
91
115
  // message
92
116
  MessageId: String(event?.message_id),
93
- MessageBody: [Text(event?.text)],
94
117
  OpenId: String(event?.chat?.id),
95
118
  CreateAt: Date.now(),
96
119
  // other
@@ -111,16 +134,16 @@ var index = defineBot(() => {
111
134
  // 定义消
112
135
  const e = {
113
136
  // 事件类型
114
- Platform: 'telegram',
137
+ Platform: platform,
115
138
  // 用户Id
116
139
  UserId: String(event?.from.id),
117
140
  UserKey: UserKey,
118
141
  UserName: event?.from?.username,
119
142
  UserAvatar: UserAvatar,
120
143
  IsMaster: false,
144
+ IsBot: false,
121
145
  // message
122
146
  MessageId: String(event?.message_id),
123
- MessageBody: [Text(event?.text)],
124
147
  MessageText: event?.text,
125
148
  OpenId: String(event?.chat?.id),
126
149
  CreateAt: Date.now(),
@@ -142,50 +165,58 @@ var index = defineBot(() => {
142
165
  // 机器人消息不处理
143
166
  if (event?.from.is_bot)
144
167
  return;
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
- };
168
+ const UserId = String(event?.from?.id);
169
+ const UserKey = useUserHashKey({
170
+ Platform: platform,
171
+ UserId: UserId
172
+ });
173
+ const UserAvatar = {
174
+ toBuffer: async () => {
175
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
176
+ if (typeof photo == 'string') {
177
+ const arrayBuffer = await fetch(photo).then(res => res.arrayBuffer());
178
+ return Buffer.from(arrayBuffer);
179
+ }
180
+ return;
181
+ },
182
+ toURL: async () => {
183
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
184
+ if (typeof photo == 'string') {
185
+ return photo;
186
+ }
187
+ return;
188
+ },
189
+ toBase64: async () => {
190
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
191
+ if (typeof photo == 'string') {
192
+ const arrayBuffer = await fetch(photo).then(res => res.arrayBuffer());
193
+ return Buffer.from(arrayBuffer).toString('base64');
194
+ }
195
+ return;
160
196
  }
161
- }
162
- catch (e) {
163
- console.error(e);
164
- }
165
- const UserKey = createHash(`telegram:${event?.from?.id}`);
197
+ };
166
198
  // 定义消
167
199
  const e = {
168
200
  // 事件类型
169
- Platform: 'telegram',
201
+ Platform: platform,
170
202
  // guild
171
203
  GuildId: String(event?.chat.id),
172
- GuildName: event?.chat.title,
173
204
  ChannelId: String(event?.chat.id),
174
- // 用户Id
175
- UserId: String(event?.from?.id),
205
+ // GuildName: event?.chat.title,
206
+ // user
207
+ UserId: UserId,
176
208
  UserKey: UserKey,
177
209
  UserName: event?.chat.username,
178
210
  UserAvatar: UserAvatar,
179
211
  IsMaster: false,
212
+ IsBot: false,
180
213
  // message
181
214
  MessageId: String(event?.message_id),
182
- MessageBody: [Text(event?.text)],
183
215
  MessageText: event?.text,
184
216
  OpenId: String(event?.chat?.id),
185
217
  CreateAt: Date.now(),
186
- //
218
+ // othder
187
219
  tag: 'txt',
188
- //
189
220
  value: null
190
221
  };
191
222
  // 当访问的时候获取
@@ -197,73 +228,6 @@ var index = defineBot(() => {
197
228
  //
198
229
  OnProcessor(e, 'member.add');
199
230
  });
200
- // const eventKeys = [
201
- // 'message',
202
- // 'text',
203
- // 'animation',
204
- // 'audio',
205
- // 'channel_chat_created',
206
- // 'contact',
207
- // 'delete_chat_photo',
208
- // 'document',
209
- // 'game',
210
- // 'group_chat_created',
211
- // 'invoice',
212
- // 'left_chat_member',
213
- // 'location',
214
- // 'migrate_from_chat_id',
215
- // 'migrate_to_chat_id',
216
- // 'new_chat_members',
217
- // 'new_chat_photo',
218
- // 'new_chat_title',
219
- // 'passport_data',
220
- // 'photo',
221
- // 'pinned_message',
222
- // 'sticker',
223
- // 'successful_payment',
224
- // 'supergroup_chat_created',
225
- // 'video',
226
- // 'video_note',
227
- // 'voice',
228
- // 'video_chat_started',
229
- // 'video_chat_ended',
230
- // 'video_chat_participants_invited',
231
- // 'video_chat_scheduled',
232
- // 'message_auto_delete_timer_changed',
233
- // 'chat_invite_link',
234
- // 'chat_member_updated',
235
- // 'web_app_data',
236
- // 'callback_query',
237
- // 'inline_query',
238
- // 'poll',
239
- // 'poll_answer',
240
- // 'chat_member',
241
- // 'my_chat_member',
242
- // 'chosen_inline_result',
243
- // // hannel
244
- // 'channel_post',
245
- // // edited
246
- // 'edited_message',
247
- // 'edited_message_text',
248
- // 'edited_message_caption',
249
- // // 频道消息修改
250
- // 'edited_channel_post',
251
- // // 频道消息修改
252
- // 'edited_channel_post_text',
253
- // // 频道消息修改
254
- // 'edited_channel_post_caption',
255
- // 'shipping_query',
256
- // 'pre_checkout_query',
257
- // 'polling_error',
258
- // 'webhook_error',
259
- // 'chat_join_request'
260
- // ]
261
- // // 打印全部的消息
262
- // for (const key of eventKeys) {
263
- // client.on(key, event => {
264
- // console.log(key, event)
265
- // })
266
- // }
267
231
  global.client = client;
268
232
  return {
269
233
  api: {
@@ -271,26 +235,27 @@ var index = defineBot(() => {
271
235
  send: (event, val) => {
272
236
  if (val.length < 0)
273
237
  return Promise.all([]);
274
- if (val.length < 0)
275
- return Promise.all([]);
276
- const content = useParse({
277
- MessageBody: val
278
- }, 'Text');
238
+ const content = val
239
+ .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
240
+ .map(item => item.value)
241
+ .join('');
279
242
  const e = event?.value;
280
243
  if (content) {
281
244
  return Promise.all([content].map(item => client.sendMessage(e.chat.id, item)));
282
245
  }
283
- const images = useParse({
284
- MessageBody: val
285
- }, 'Image');
246
+ const images = val.filter(item => item.type == 'Image').map(item => item.value);
286
247
  if (images) {
287
248
  return Promise.all(images.map(item => client.sendPhoto(e.chat.id, item)));
288
249
  }
289
250
  return Promise.all([]);
251
+ },
252
+ mention: async () => {
253
+ // const event: TelegramClient.Message = e.value
254
+ return [];
290
255
  }
291
256
  }
292
257
  }
293
258
  };
294
259
  });
295
260
 
296
- export { client, index as default };
261
+ export { client, index as default, platform };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/telegram",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "telegram-bot",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",