@alemonjs/telegram 0.1.0 → 0.1.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.
Files changed (2) hide show
  1. package/lib/index.js +66 -61
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { defineBot, getConfig, Text, OnProcessor, useParse } from 'alemonjs';
1
+ import { defineBot, getConfig, createHash, Text, OnProcessor, useParse } from 'alemonjs';
2
2
  import TelegramClient from 'node-telegram-bot-api';
3
3
 
4
4
  const client = global.client;
@@ -27,7 +27,7 @@ var index = defineBot(() => {
27
27
  .getUserProfilePhotos(UserId)
28
28
  .then(profilePhotos => {
29
29
  if (profilePhotos.total_count > 0) {
30
- // 获取第一张头像的文件 ID
30
+ // 获取第一张头像的文件 Id
31
31
  const fileId = profilePhotos.photos[0][0].file_id;
32
32
  // 获取文件信息以获取下载链接
33
33
  client
@@ -47,11 +47,28 @@ var index = defineBot(() => {
47
47
  };
48
48
  //
49
49
  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;
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
+ };
67
+ }
68
+ }
69
+ }
70
+ catch (e) {
71
+ console.error(e);
55
72
  }
56
73
  if (event?.chat.type == 'channel' || event?.chat.type == 'supergroup') {
57
74
  // 机器人消息不处理
@@ -63,31 +80,21 @@ var index = defineBot(() => {
63
80
  Platform: 'telegram',
64
81
  // 频道
65
82
  GuildId: String(event?.chat.id),
66
- // 频道名称
67
83
  GuildName: event?.chat.title,
68
- // 子频道
69
84
  ChannelId: String(event?.chat.id),
70
- GuildIdAvatar: '',
71
- GuildIdName: '',
72
- // 是否是主人
73
- IsMaster: false,
74
- // 用户ID
85
+ // user
75
86
  UserId: String(event?.from?.id),
76
- // 用户名
87
+ UserKey: UserKey,
77
88
  UserName: event?.chat.username,
78
- // 用户头像
79
89
  UserAvatar: UserAvatar,
80
- // 格式化数据
81
- MsgId: String(event?.message_id),
82
- // 用户消息
90
+ IsMaster: false,
91
+ // message
92
+ MessageId: String(event?.message_id),
83
93
  MessageBody: [Text(event?.text)],
84
- // 用户openId
85
- OpenID: String(event?.chat?.id),
86
- // 创建时间
94
+ OpenId: String(event?.chat?.id),
87
95
  CreateAt: Date.now(),
88
- //
96
+ // other
89
97
  tag: 'txt',
90
- //
91
98
  value: null
92
99
  };
93
100
  // 当访问的时候获取
@@ -105,29 +112,20 @@ var index = defineBot(() => {
105
112
  const e = {
106
113
  // 事件类型
107
114
  Platform: 'telegram',
108
- // 是否是主人
109
- IsMaster: false,
110
- // 用户ID
115
+ // 用户Id
111
116
  UserId: String(event?.from.id),
112
- // 用户名
117
+ UserKey: UserKey,
113
118
  UserName: event?.from?.username,
114
- GuildIdName: '',
115
- GuildIdAvatar: '',
116
- ChannelName: '',
117
- // 用户头像
118
119
  UserAvatar: UserAvatar,
119
- // 格式化数据
120
- MsgId: String(event?.message_id),
121
- // 用户消息
120
+ IsMaster: false,
121
+ // message
122
+ MessageId: String(event?.message_id),
122
123
  MessageBody: [Text(event?.text)],
123
124
  MessageText: event?.text,
124
- // 用户openId
125
- OpenID: String(event?.chat?.id),
126
- // 创建时间
125
+ OpenId: String(event?.chat?.id),
127
126
  CreateAt: Date.now(),
128
- //
127
+ // other
129
128
  tag: 'txt',
130
- //
131
129
  value: null
132
130
  };
133
131
  // 当访问的时候获取
@@ -144,39 +142,46 @@ var index = defineBot(() => {
144
142
  // 机器人消息不处理
145
143
  if (event?.from.is_bot)
146
144
  return;
147
- let UserAvatar = '';
148
- const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
149
- if (typeof photo == 'string')
150
- UserAvatar = photo;
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
+ };
160
+ }
161
+ }
162
+ catch (e) {
163
+ console.error(e);
164
+ }
165
+ const UserKey = createHash(`telegram:${event?.from?.id}`);
151
166
  // 定义消
152
167
  const e = {
153
168
  // 事件类型
154
169
  Platform: 'telegram',
155
- // 频道
170
+ // guild
156
171
  GuildId: String(event?.chat.id),
157
- // 频道名称
158
172
  GuildName: event?.chat.title,
159
- // 子频道
160
173
  ChannelId: String(event?.chat.id),
161
- GuildIdAvatar: '',
162
- GuildIdName: '',
163
- // 是否是主人
164
- IsMaster: false,
165
- // 用户ID
174
+ // 用户Id
166
175
  UserId: String(event?.from?.id),
167
- // 用户名
176
+ UserKey: UserKey,
168
177
  UserName: event?.chat.username,
169
- // 用户头像
170
178
  UserAvatar: UserAvatar,
171
- // 格式化数据
172
- MsgId: String(event?.message_id),
173
- // 用户消息
179
+ IsMaster: false,
180
+ // message
181
+ MessageId: String(event?.message_id),
174
182
  MessageBody: [Text(event?.text)],
175
- //
176
183
  MessageText: event?.text,
177
- // 用户openId
178
- OpenID: String(event?.chat?.id),
179
- // 创建时间
184
+ OpenId: String(event?.chat?.id),
180
185
  CreateAt: Date.now(),
181
186
  //
182
187
  tag: 'txt',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/telegram",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "telegram-bot",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",