@alemonjs/telegram 0.1.0 → 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, 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) {
@@ -27,7 +32,7 @@ var index = defineBot(() => {
27
32
  .getUserProfilePhotos(UserId)
28
33
  .then(profilePhotos => {
29
34
  if (profilePhotos.total_count > 0) {
30
- // 获取第一张头像的文件 ID
35
+ // 获取第一张头像的文件 Id
31
36
  const fileId = profilePhotos.photos[0][0].file_id;
32
37
  // 获取文件信息以获取下载链接
33
38
  client
@@ -45,14 +50,43 @@ var index = defineBot(() => {
45
50
  .catch(reject);
46
51
  });
47
52
  };
48
- //
49
53
  client.on('text', async (event) => {
50
- let UserAvatar = '';
51
- if (event?.chat.type == 'supergroup' || event?.chat.type == 'private') {
52
- const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
53
- if (typeof photo == 'string')
54
- UserAvatar = photo;
55
- }
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
+ }
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;
88
+ }
89
+ };
56
90
  if (event?.chat.type == 'channel' || event?.chat.type == 'supergroup') {
57
91
  // 机器人消息不处理
58
92
  if (event?.from?.is_bot)
@@ -60,34 +94,23 @@ var index = defineBot(() => {
60
94
  // 定义消
61
95
  const e = {
62
96
  // 事件类型
63
- Platform: 'telegram',
97
+ Platform: Platform,
64
98
  // 频道
65
99
  GuildId: String(event?.chat.id),
66
- // 频道名称
67
- GuildName: event?.chat.title,
68
- // 子频道
69
100
  ChannelId: String(event?.chat.id),
70
- GuildIdAvatar: '',
71
- GuildIdName: '',
72
- // 是否是主人
73
- IsMaster: false,
74
- // 用户ID
75
- UserId: String(event?.from?.id),
76
- // 用户名
101
+ // user
102
+ UserId: UserId,
103
+ UserKey: UserKey,
77
104
  UserName: event?.chat.username,
78
- // 用户头像
79
105
  UserAvatar: UserAvatar,
80
- // 格式化数据
81
- MsgId: String(event?.message_id),
82
- // 用户消息
83
- MessageBody: [Text(event?.text)],
84
- // 用户openId
85
- OpenID: String(event?.chat?.id),
86
- // 创建时间
106
+ IsMaster: false,
107
+ IsBot: false,
108
+ // message
109
+ MessageId: String(event?.message_id),
110
+ OpenId: String(event?.chat?.id),
87
111
  CreateAt: Date.now(),
88
- //
112
+ // other
89
113
  tag: 'txt',
90
- //
91
114
  value: null
92
115
  };
93
116
  // 当访问的时候获取
@@ -104,30 +127,21 @@ var index = defineBot(() => {
104
127
  // 定义消
105
128
  const e = {
106
129
  // 事件类型
107
- Platform: 'telegram',
108
- // 是否是主人
109
- IsMaster: false,
110
- // 用户ID
130
+ Platform: Platform,
131
+ // 用户Id
111
132
  UserId: String(event?.from.id),
112
- // 用户名
133
+ UserKey: UserKey,
113
134
  UserName: event?.from?.username,
114
- GuildIdName: '',
115
- GuildIdAvatar: '',
116
- ChannelName: '',
117
- // 用户头像
118
135
  UserAvatar: UserAvatar,
119
- // 格式化数据
120
- MsgId: String(event?.message_id),
121
- // 用户消息
122
- MessageBody: [Text(event?.text)],
136
+ IsMaster: false,
137
+ IsBot: false,
138
+ // message
139
+ MessageId: String(event?.message_id),
123
140
  MessageText: event?.text,
124
- // 用户openId
125
- OpenID: String(event?.chat?.id),
126
- // 创建时间
141
+ OpenId: String(event?.chat?.id),
127
142
  CreateAt: Date.now(),
128
- //
143
+ // other
129
144
  tag: 'txt',
130
- //
131
145
  value: null
132
146
  };
133
147
  // 当访问的时候获取
@@ -144,43 +158,58 @@ var index = defineBot(() => {
144
158
  // 机器人消息不处理
145
159
  if (event?.from.is_bot)
146
160
  return;
147
- let UserAvatar = '';
148
- const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
149
- if (typeof photo == 'string')
150
- UserAvatar = photo;
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;
189
+ }
190
+ };
151
191
  // 定义消
152
192
  const e = {
153
193
  // 事件类型
154
- Platform: 'telegram',
155
- // 频道
194
+ Platform: Platform,
195
+ // guild
156
196
  GuildId: String(event?.chat.id),
157
- // 频道名称
158
- GuildName: event?.chat.title,
159
- // 子频道
160
197
  ChannelId: String(event?.chat.id),
161
- GuildIdAvatar: '',
162
- GuildIdName: '',
163
- // 是否是主人
164
- IsMaster: false,
165
- // 用户ID
166
- UserId: String(event?.from?.id),
167
- // 用户名
198
+ // GuildName: event?.chat.title,
199
+ // user
200
+ UserId: UserId,
201
+ UserKey: UserKey,
168
202
  UserName: event?.chat.username,
169
- // 用户头像
170
203
  UserAvatar: UserAvatar,
171
- // 格式化数据
172
- MsgId: String(event?.message_id),
173
- // 用户消息
174
- MessageBody: [Text(event?.text)],
175
- //
204
+ IsMaster: false,
205
+ IsBot: false,
206
+ // message
207
+ MessageId: String(event?.message_id),
176
208
  MessageText: event?.text,
177
- // 用户openId
178
- OpenID: String(event?.chat?.id),
179
- // 创建时间
209
+ OpenId: String(event?.chat?.id),
180
210
  CreateAt: Date.now(),
181
- //
211
+ // othder
182
212
  tag: 'txt',
183
- //
184
213
  value: null
185
214
  };
186
215
  // 当访问的时候获取
@@ -192,73 +221,6 @@ var index = defineBot(() => {
192
221
  //
193
222
  OnProcessor(e, 'member.add');
194
223
  });
195
- // const eventKeys = [
196
- // 'message',
197
- // 'text',
198
- // 'animation',
199
- // 'audio',
200
- // 'channel_chat_created',
201
- // 'contact',
202
- // 'delete_chat_photo',
203
- // 'document',
204
- // 'game',
205
- // 'group_chat_created',
206
- // 'invoice',
207
- // 'left_chat_member',
208
- // 'location',
209
- // 'migrate_from_chat_id',
210
- // 'migrate_to_chat_id',
211
- // 'new_chat_members',
212
- // 'new_chat_photo',
213
- // 'new_chat_title',
214
- // 'passport_data',
215
- // 'photo',
216
- // 'pinned_message',
217
- // 'sticker',
218
- // 'successful_payment',
219
- // 'supergroup_chat_created',
220
- // 'video',
221
- // 'video_note',
222
- // 'voice',
223
- // 'video_chat_started',
224
- // 'video_chat_ended',
225
- // 'video_chat_participants_invited',
226
- // 'video_chat_scheduled',
227
- // 'message_auto_delete_timer_changed',
228
- // 'chat_invite_link',
229
- // 'chat_member_updated',
230
- // 'web_app_data',
231
- // 'callback_query',
232
- // 'inline_query',
233
- // 'poll',
234
- // 'poll_answer',
235
- // 'chat_member',
236
- // 'my_chat_member',
237
- // 'chosen_inline_result',
238
- // // hannel
239
- // 'channel_post',
240
- // // edited
241
- // 'edited_message',
242
- // 'edited_message_text',
243
- // 'edited_message_caption',
244
- // // 频道消息修改
245
- // 'edited_channel_post',
246
- // // 频道消息修改
247
- // 'edited_channel_post_text',
248
- // // 频道消息修改
249
- // 'edited_channel_post_caption',
250
- // 'shipping_query',
251
- // 'pre_checkout_query',
252
- // 'polling_error',
253
- // 'webhook_error',
254
- // 'chat_join_request'
255
- // ]
256
- // // 打印全部的消息
257
- // for (const key of eventKeys) {
258
- // client.on(key, event => {
259
- // console.log(key, event)
260
- // })
261
- // }
262
224
  global.client = client;
263
225
  return {
264
226
  api: {
@@ -266,22 +228,23 @@ var index = defineBot(() => {
266
228
  send: (event, val) => {
267
229
  if (val.length < 0)
268
230
  return Promise.all([]);
269
- if (val.length < 0)
270
- return Promise.all([]);
271
- const content = useParse({
272
- MessageBody: val
273
- }, 'Text');
231
+ const content = val
232
+ .filter(item => item.type == 'Link' || item.type == 'Mention' || item.type == 'Text')
233
+ .map(item => item.value)
234
+ .join('');
274
235
  const e = event?.value;
275
236
  if (content) {
276
237
  return Promise.all([content].map(item => client.sendMessage(e.chat.id, item)));
277
238
  }
278
- const images = useParse({
279
- MessageBody: val
280
- }, 'Image');
239
+ const images = val.filter(item => item.type == 'Image').map(item => item.value);
281
240
  if (images) {
282
241
  return Promise.all(images.map(item => client.sendPhoto(e.chat.id, item)));
283
242
  }
284
243
  return Promise.all([]);
244
+ },
245
+ mention: async () => {
246
+ // const event: TelegramClient.Message = e.value
247
+ return [];
285
248
  }
286
249
  }
287
250
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/telegram",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "telegram-bot",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",