halbot 1995.1.9 → 1995.1.11

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "halbot",
3
3
  "description": "Just another AI powered Telegram bot, which is simple design, easy to use, extendable and fun.",
4
- "version": "1995.1.9",
4
+ "version": "1995.1.11",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/halbot",
7
7
  "type": "module",
@@ -141,6 +141,11 @@ const ctxExt = ctx => {
141
141
  if (lastMsgId && pageMap[lastMsgId]?.raw === pages[i]) {
142
142
  continue;
143
143
  }
144
+ if (!options?.processing && !_extra?.buttons?.length
145
+ && !_extra.keyboards && !lastMsgId && ctx.getKeyboard) {
146
+ delete _extra?.buttons;
147
+ _extra.keyboards = ctx.getKeyboard();
148
+ }
144
149
  await (lastMsgId ? ctx.edit(lastMsgId, pages[i], _extra) // ongoing, edit
145
150
  : ctx.resp(pages[i], _extra)); // new page, reply
146
151
  await ctx.timeout();
@@ -195,10 +200,14 @@ const action = async (ctx, next) => {
195
200
  + ctx._.message.new_chat_member.user.id + ' => '
196
201
  + ctx._.message.new_chat_member.status
197
202
  );
198
- if (ctx._.message.new_chat_member.user.id !== ctx.botInfo.id
199
- || ctx._.message.new_chat_member.status === 'left') {
203
+ if (ctx._.message.new_chat_member.user.id === ctx.botInfo.id
204
+ && ctx._.message.new_chat_member.status === 'left') {
200
205
  return ctx.finish();
201
- } else { ctx.hello(); }
206
+ } else if (ctx._.message.new_chat_member.user.id === ctx.botInfo.id) {
207
+ ctx.hello();
208
+ }
209
+ } else if (ctx._.message.group_chat_created) {
210
+ return ctx.finish();
202
211
  } else if (!ctx._.type) { return log(`Unsupported message type.`); }
203
212
  // get chat metadata
204
213
  ctx._.chatId = ctx._.message.chat.id;
@@ -6,24 +6,22 @@ const log = (c, o) => utilitas.log(c, _name, { time: 1, ...o || {} });
6
6
 
7
7
  // https://stackoverflow.com/questions/69924954/an-error-is-issued-when-opening-the-telebot-keyboard
8
8
  const keyboards = [[
9
- { text: `/ai ${bot.BOT}` },
10
- { text: '/help 🛟' },
9
+ { text: `/ai ${bot.BOT}` }, { text: '/help 🛟' },
11
10
  ], [
12
- { text: '/set --tts=🔊' },
13
- { text: '/set --tts=🔇' },
14
- ], [
15
- { text: '/set --chatty=🐵' },
16
- { text: '/set --chatty=🙊' },
11
+ { text: '/set --tts=🔊' }, { text: '/set --tts=🔇' },
17
12
  ]];
18
13
 
14
+ const getKeyboard = ctx => ctx._.chatType === hal.GROUP ? [
15
+ ...keyboards, [{ text: '/set --chatty=🐵' }, { text: '/set --chatty=🙊' }]
16
+ ] : keyboards;
17
+
18
+ const ctxExt = ctx => {
19
+ ctx.getKeyboard = () => getKeyboard(ctx);
20
+ };
21
+
19
22
  const action = async (ctx, next) => {
20
- // reload functions
21
- const _ok = ctx.ok;
22
- // @TODO: this will case keyboard showup everytime
23
- // ctx.ok = async (message, options) => await _ok(message, {
24
- // ...options || {},
25
- // ...options?.buttons ? {} : (options?.keyboards || { keyboards }),
26
- // });
23
+ // extend ctx
24
+ ctxExt(ctx);
27
25
  // handle callback query
28
26
  if (ctx._.type === 'callback_query') {
29
27
  const data = utilitas.parseJson(ctx.update.callback_query.data);
@@ -64,7 +62,7 @@ const action = async (ctx, next) => {
64
62
  = { args: ctx._.cmd.args, touchedAt: Date.now() };
65
63
  }
66
64
  // handle commands
67
- switch (ctx.cmd?.cmd) {
65
+ switch (ctx._.cmd?.cmd) {
68
66
  case 'clearkb':
69
67
  return await ctx.complete({ keyboards: [] });
70
68
  }
@@ -49,7 +49,7 @@ const action = async (ctx, next) => {
49
49
  }
50
50
  case 'reset':
51
51
  ctx._.session.config = ctx._.config = {};
52
- return await ctx.complete();
52
+ return await ctx.complete({ keyboards: ctx.getKeyboard() });
53
53
  }
54
54
  await next();
55
55
  };
@@ -8,11 +8,10 @@ const action = async (ctx, next) => {
8
8
  let [resp, extra, lock, sResp, lastMsg, lastSent] =
9
9
  [null, { buttons: [] }, 1000 * 3, null, null, 0];
10
10
  const ok = async options => {
11
- const curTime = Date.now();
12
11
  if (options?.processing && (
13
- curTime - lastSent < ctx._.limit || lastMsg === resp.text
12
+ Date.now() - lastSent < ctx._.limit || lastMsg === resp.text
14
13
  )) { return; }
15
- [lastSent, lastMsg] = [curTime + lock, resp.text];
14
+ [lastSent, lastMsg] = [Date.now() + lock, resp.text];
16
15
  if (!options?.processing) {
17
16
  (resp.annotations || []).map((x, i) => extra.buttons.push({
18
17
  label: `${i + 1}. ${x.title}`, url: x.url,
@@ -22,7 +21,7 @@ const action = async (ctx, next) => {
22
21
  ...ctx._.keyboards ? { keyboards: ctx._.keyboards } : {},
23
22
  ...extra, ...options || {},
24
23
  });
25
- lastSent = curTime;
24
+ lastSent = Date.now();
26
25
  return sResp;
27
26
  };
28
27
  resp = await alan.talk(ctx._.text, {
@@ -43,8 +42,8 @@ const action = async (ctx, next) => {
43
42
  }
44
43
  // print(resp);
45
44
  await ctx.timeout();
46
- await resp.text.trim() ? ok({ processing: false })
47
- : ctx.deleteMessage(ctx._.done[0].message_id);
45
+ await (resp.text.trim() ? ok({ processing: false })
46
+ : ctx.deleteMessage(ctx._.done[0].message_id));
48
47
  ctx._.request = resp.request;
49
48
  ctx._.response = resp.response;
50
49
  ctx.memorize && await ctx.memorize();