@alemonjs/telegram 0.1.1 → 0.2.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/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,8 @@
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 _default: () => alemonjs.ClientAPI;
6
7
 
7
8
  export { type Client, client, _default as default };
package/lib/index.js CHANGED
@@ -1,13 +1,13 @@
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
4
  const client = global.client;
5
5
  var index = defineBot(() => {
6
- //
7
- const cfg = getConfig();
8
- const config = cfg.value?.['telegram'];
6
+ const value = getConfigValue();
7
+ const config = value?.telegram;
9
8
  if (!config)
10
9
  return;
10
+ const Platform = 'telegram';
11
11
  //
12
12
  const client = new TelegramClient(config.token, {
13
13
  polling: true,
@@ -17,6 +17,11 @@ var index = defineBot(() => {
17
17
  proxy: config?.proxy ?? ''
18
18
  }
19
19
  });
20
+ /**
21
+ *
22
+ * @param UserId
23
+ * @returns
24
+ */
20
25
  const getUserProfilePhotosUrl = (UserId) => {
21
26
  return new Promise((resolve, reject) => {
22
27
  if (!UserId) {
@@ -45,31 +50,43 @@ var index = defineBot(() => {
45
50
  .catch(reject);
46
51
  });
47
52
  };
48
- //
49
53
  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
- };
54
+ const UserId = String(event?.from?.id);
55
+ const UserKey = useUserHashKey({
56
+ Platform: Platform,
57
+ UserId: UserId
58
+ });
59
+ const UserAvatar = {
60
+ toBuffer: async () => {
61
+ if (event?.chat.type == 'supergroup' || event?.chat.type == 'private') {
62
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
63
+ if (typeof photo == 'string') {
64
+ const arrayBuffer = await fetch(photo).then(res => res.arrayBuffer());
65
+ return Buffer.from(arrayBuffer);
66
+ }
67
+ }
68
+ return;
69
+ },
70
+ toURL: async () => {
71
+ if (event?.chat.type == 'supergroup' || event?.chat.type == 'private') {
72
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
73
+ if (typeof photo == 'string') {
74
+ return photo;
75
+ }
67
76
  }
77
+ return;
78
+ },
79
+ toBase64: async () => {
80
+ if (event?.chat.type == 'supergroup' || event?.chat.type == 'private') {
81
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
82
+ if (typeof photo == 'string') {
83
+ const arrayBuffer = await fetch(photo).then(res => res.arrayBuffer());
84
+ return Buffer.from(arrayBuffer).toString('base64');
85
+ }
86
+ }
87
+ return;
68
88
  }
69
- }
70
- catch (e) {
71
- console.error(e);
72
- }
89
+ };
73
90
  if (event?.chat.type == 'channel' || event?.chat.type == 'supergroup') {
74
91
  // 机器人消息不处理
75
92
  if (event?.from?.is_bot)
@@ -77,20 +94,19 @@ var index = defineBot(() => {
77
94
  // 定义消
78
95
  const e = {
79
96
  // 事件类型
80
- Platform: 'telegram',
97
+ Platform: Platform,
81
98
  // 频道
82
99
  GuildId: String(event?.chat.id),
83
- GuildName: event?.chat.title,
84
100
  ChannelId: String(event?.chat.id),
85
101
  // user
86
- UserId: String(event?.from?.id),
102
+ UserId: UserId,
87
103
  UserKey: UserKey,
88
104
  UserName: event?.chat.username,
89
105
  UserAvatar: UserAvatar,
90
106
  IsMaster: false,
107
+ IsBot: false,
91
108
  // message
92
109
  MessageId: String(event?.message_id),
93
- MessageBody: [Text(event?.text)],
94
110
  OpenId: String(event?.chat?.id),
95
111
  CreateAt: Date.now(),
96
112
  // other
@@ -111,16 +127,16 @@ var index = defineBot(() => {
111
127
  // 定义消
112
128
  const e = {
113
129
  // 事件类型
114
- Platform: 'telegram',
130
+ Platform: Platform,
115
131
  // 用户Id
116
132
  UserId: String(event?.from.id),
117
133
  UserKey: UserKey,
118
134
  UserName: event?.from?.username,
119
135
  UserAvatar: UserAvatar,
120
136
  IsMaster: false,
137
+ IsBot: false,
121
138
  // message
122
139
  MessageId: String(event?.message_id),
123
- MessageBody: [Text(event?.text)],
124
140
  MessageText: event?.text,
125
141
  OpenId: String(event?.chat?.id),
126
142
  CreateAt: Date.now(),
@@ -142,50 +158,58 @@ var index = defineBot(() => {
142
158
  // 机器人消息不处理
143
159
  if (event?.from.is_bot)
144
160
  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
- };
161
+ const UserId = String(event?.from?.id);
162
+ const UserKey = useUserHashKey({
163
+ Platform: Platform,
164
+ UserId: UserId
165
+ });
166
+ const UserAvatar = {
167
+ toBuffer: async () => {
168
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
169
+ if (typeof photo == 'string') {
170
+ const arrayBuffer = await fetch(photo).then(res => res.arrayBuffer());
171
+ return Buffer.from(arrayBuffer);
172
+ }
173
+ return;
174
+ },
175
+ toURL: async () => {
176
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
177
+ if (typeof photo == 'string') {
178
+ return photo;
179
+ }
180
+ return;
181
+ },
182
+ toBase64: async () => {
183
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
184
+ if (typeof photo == 'string') {
185
+ const arrayBuffer = await fetch(photo).then(res => res.arrayBuffer());
186
+ return Buffer.from(arrayBuffer).toString('base64');
187
+ }
188
+ return;
160
189
  }
161
- }
162
- catch (e) {
163
- console.error(e);
164
- }
165
- const UserKey = createHash(`telegram:${event?.from?.id}`);
190
+ };
166
191
  // 定义消
167
192
  const e = {
168
193
  // 事件类型
169
- Platform: 'telegram',
194
+ Platform: Platform,
170
195
  // guild
171
196
  GuildId: String(event?.chat.id),
172
- GuildName: event?.chat.title,
173
197
  ChannelId: String(event?.chat.id),
174
- // 用户Id
175
- UserId: String(event?.from?.id),
198
+ // GuildName: event?.chat.title,
199
+ // user
200
+ UserId: UserId,
176
201
  UserKey: UserKey,
177
202
  UserName: event?.chat.username,
178
203
  UserAvatar: UserAvatar,
179
204
  IsMaster: false,
205
+ IsBot: false,
180
206
  // message
181
207
  MessageId: String(event?.message_id),
182
- MessageBody: [Text(event?.text)],
183
208
  MessageText: event?.text,
184
209
  OpenId: String(event?.chat?.id),
185
210
  CreateAt: Date.now(),
186
- //
211
+ // othder
187
212
  tag: 'txt',
188
- //
189
213
  value: null
190
214
  };
191
215
  // 当访问的时候获取
@@ -197,73 +221,6 @@ var index = defineBot(() => {
197
221
  //
198
222
  OnProcessor(e, 'member.add');
199
223
  });
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
224
  global.client = client;
268
225
  return {
269
226
  api: {
@@ -271,22 +228,23 @@ var index = defineBot(() => {
271
228
  send: (event, val) => {
272
229
  if (val.length < 0)
273
230
  return Promise.all([]);
274
- if (val.length < 0)
275
- return Promise.all([]);
276
- const content = useParse({
277
- MessageBody: val
278
- }, 'Text');
231
+ const content = val
232
+ .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
233
+ .map(item => item.value)
234
+ .join('');
279
235
  const e = event?.value;
280
236
  if (content) {
281
237
  return Promise.all([content].map(item => client.sendMessage(e.chat.id, item)));
282
238
  }
283
- const images = useParse({
284
- MessageBody: val
285
- }, 'Image');
239
+ const images = val.filter(item => item.type == 'Image').map(item => item.value);
286
240
  if (images) {
287
241
  return Promise.all(images.map(item => client.sendPhoto(e.chat.id, item)));
288
242
  }
289
243
  return Promise.all([]);
244
+ },
245
+ mention: async () => {
246
+ // const event: TelegramClient.Message = e.value
247
+ return [];
290
248
  }
291
249
  }
292
250
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/telegram",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "telegram-bot",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",