@alemonjs/telegram 0.0.3 → 0.0.4

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 (3) hide show
  1. package/README.md +4 -3
  2. package/lib/index.js +124 -44
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -16,12 +16,13 @@ yarn add @alemonjs/telegram
16
16
  telegram:
17
17
  # 令牌 (必填)
18
18
  token: ''
19
+ # 代理地址 (推荐填)
20
+ proxy: 'http://127.0.0.1:7890'
19
21
  # 主人编号
20
22
  master_id: null
23
+ # other
21
24
  base_api_url: null
22
- # 代理地址
23
- # proxy: 'http://127.0.0.1:7890'
24
- proxy: null
25
+ request_url: null
25
26
  ```
26
27
 
27
28
  ## Community
package/lib/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import { defineBot, getConfig, Text, OnProcessor, useParse } from 'alemonjs';
2
2
  import Client from 'node-telegram-bot-api';
3
3
 
4
+ process.env.NODE_NO_WARNINGS = '1';
4
5
  var index = defineBot(() => {
6
+ //
5
7
  const cfg = getConfig();
6
8
  const config = cfg.value?.['telegram'];
7
9
  if (!config)
@@ -9,52 +11,130 @@ var index = defineBot(() => {
9
11
  //
10
12
  const client = new Client(config.token, {
11
13
  polling: true,
12
- baseApiUrl: '',
14
+ baseApiUrl: config?.base_api_url ?? '',
13
15
  request: {
14
- url: '',
15
- proxy: config?.proxy ?? 'http://127.0.0.1:7890'
16
+ url: config?.request_url ?? '',
17
+ proxy: config?.proxy ?? ''
16
18
  }
17
19
  });
18
- //
19
- //
20
- client.on('message', event => {
21
- // 定义消
22
- const e = {
23
- // 事件类型
24
- Platform: 'telegram',
25
- // 频道
26
- GuildId: String(event.chat.id),
27
- // 子频道
28
- ChannelId: String(event.chat.id),
29
- // 是否是主人
30
- IsMaster: false,
31
- // 用户ID
32
- UserId: String(event?.sender_chat?.id),
33
- // 用户名
34
- UserName: event.sender_chat.username,
35
- // 用户头像
36
- UserAvatar: '',
37
- // 格式化数据
38
- MsgId: String(event.message_id),
39
- // 用户消息
40
- Megs: [Text(event.text ?? '')],
41
- // 用户openId
42
- OpenID: 'test',
43
- // 创建时间
44
- CreateAt: Date.now(),
45
- //
46
- value: null
47
- };
48
- // 当访问的时候获取
49
- Object.defineProperty(e, 'value', {
50
- get() {
51
- return event;
20
+ const getUserProfilePhotosUrl = UserId => {
21
+ return new Promise((resolve, reject) => {
22
+ if (!UserId) {
23
+ reject(new Error('UserId 不能为空'));
24
+ return;
52
25
  }
26
+ client
27
+ .getUserProfilePhotos(UserId)
28
+ .then(profilePhotos => {
29
+ if (profilePhotos.total_count > 0) {
30
+ // 获取第一张头像的文件 ID
31
+ const fileId = profilePhotos.photos[0][0].file_id;
32
+ // 获取文件信息以获取下载链接
33
+ client
34
+ .getFile(fileId)
35
+ .then(file => {
36
+ const filePath = file.file_path;
37
+ resolve(`https://api.telegram.org/file/bot${config.token}/${filePath}`);
38
+ })
39
+ .catch(reject);
40
+ }
41
+ else {
42
+ reject(new Error('用户没有头像'));
43
+ }
44
+ })
45
+ .catch(reject);
53
46
  });
54
- // 处理消息
55
- OnProcessor(e, 'private.message.create');
47
+ };
48
+ //
49
+ client.on('text', async (event) => {
50
+ let UserAvatar = '';
51
+ if (event?.chat.type == 'channel' || event?.chat.type == 'private') {
52
+ const photo = await getUserProfilePhotosUrl(event?.from?.id).catch(console.error);
53
+ if (typeof photo == 'string')
54
+ UserAvatar = photo;
55
+ }
56
+ if (event?.chat.type == 'channel' || event.chat.type == 'supergroup') {
57
+ // 机器人消息不处理
58
+ if (event.from?.is_bot)
59
+ return;
60
+ // 定义消
61
+ const e = {
62
+ // 事件类型
63
+ Platform: 'telegram',
64
+ // 频道
65
+ GuildId: String(event?.chat.id),
66
+ // 频道名称
67
+ GuildName: event?.chat.title,
68
+ // 子频道
69
+ ChannelId: String(event?.chat.id),
70
+ // 是否是主人
71
+ IsMaster: false,
72
+ // 用户ID
73
+ UserId: String(event?.from?.id),
74
+ // 用户名
75
+ UserName: event?.chat.username,
76
+ // 用户头像
77
+ UserAvatar: UserAvatar,
78
+ // 格式化数据
79
+ MsgId: String(event?.message_id),
80
+ // 用户消息
81
+ Megs: [Text(event?.text)],
82
+ // 用户openId
83
+ OpenID: String(event.chat?.id),
84
+ // 创建时间
85
+ CreateAt: Date.now(),
86
+ //
87
+ tag: 'txt',
88
+ //
89
+ value: null
90
+ };
91
+ // 当访问的时候获取
92
+ Object.defineProperty(e, 'value', {
93
+ get() {
94
+ return event;
95
+ }
96
+ });
97
+ //
98
+ OnProcessor(e, 'message.create');
99
+ //
100
+ }
101
+ else if (event?.chat.type == 'private') {
102
+ // 定义消
103
+ const e = {
104
+ // 事件类型
105
+ Platform: 'telegram',
106
+ // 是否是主人
107
+ IsMaster: false,
108
+ // 用户ID
109
+ UserId: String(event?.from.id),
110
+ // 用户名
111
+ UserName: event?.from?.username,
112
+ // 用户头像
113
+ UserAvatar: UserAvatar,
114
+ // 格式化数据
115
+ MsgId: String(event?.message_id),
116
+ // 用户消息
117
+ Megs: [Text(event?.text)],
118
+ // 用户openId
119
+ OpenID: String(event.chat?.id),
120
+ // 创建时间
121
+ CreateAt: Date.now(),
122
+ //
123
+ tag: 'txt',
124
+ //
125
+ value: null
126
+ };
127
+ // 当访问的时候获取
128
+ Object.defineProperty(e, 'value', {
129
+ get() {
130
+ return event;
131
+ }
132
+ });
133
+ // 处理消息
134
+ OnProcessor(e, 'private.message.create');
135
+ }
56
136
  });
57
- // const eventKeys: string[] = [
137
+ // const eventKeys = [
58
138
  // 'message',
59
139
  // 'text',
60
140
  // 'animation',
@@ -115,13 +195,12 @@ var index = defineBot(() => {
115
195
  // 'webhook_error',
116
196
  // 'chat_join_request'
117
197
  // ]
118
- // 打印全部的消息
198
+ // // 打印全部的消息
119
199
  // for (const key of eventKeys) {
120
200
  // client.on(key, event => {
121
201
  // console.log(key, event)
122
202
  // })
123
203
  // }
124
- //
125
204
  return {
126
205
  api: {
127
206
  use: {
@@ -131,12 +210,13 @@ var index = defineBot(() => {
131
210
  if (val.length < 0)
132
211
  return Promise.all([]);
133
212
  const content = useParse(val, 'Text');
213
+ const e = event.value;
134
214
  if (content) {
135
- return Promise.all([content].map(item => client.sendMessage(event.GuildId, item)));
215
+ return Promise.all([content].map(item => client.sendMessage(e.chat.id, item)));
136
216
  }
137
217
  const images = useParse(val, 'Image');
138
218
  if (images) {
139
- return Promise.all(images.map(item => client.sendPhoto(event.GuildId, item)));
219
+ return Promise.all(images.map(item => client.sendPhoto(e.chat.id, item)));
140
220
  }
141
221
  return Promise.all([]);
142
222
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/telegram",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "telegram-bot",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
@@ -8,7 +8,6 @@
8
8
  "main": "lib/index.js",
9
9
  "types": "lib",
10
10
  "dependencies": {
11
- "chat-space": "^0.0.4",
12
11
  "grammy": "^1.30.0",
13
12
  "node-telegram-bot-api": "^0.66.0"
14
13
  },
@@ -29,7 +28,8 @@
29
28
  "chat-bot"
30
29
  ],
31
30
  "publishConfig": {
32
- "registry": "https://registry.npmjs.org"
31
+ "registry": "https://registry.npmjs.org",
32
+ "access": "public"
33
33
  },
34
34
  "bugs": "https://github.com/ningmengchongshui/alemonjs/issues",
35
35
  "repository": {