halbot 1995.1.6 → 1995.1.8

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/lib/hal.mjs CHANGED
@@ -144,7 +144,7 @@ const parseArgs = async (args, ctx) => {
144
144
  };
145
145
 
146
146
  const packMessage = (messages) => messages.map(x => ({
147
- message_id: x.message_id, score: x.score,
147
+ message_id: x.message_id, score: x.score, created_at: x.created_at,
148
148
  request: x.received_text, response: x.response_text,
149
149
  }));
150
150
 
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.6",
4
+ "version": "1995.1.8",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/halbot",
7
7
  "type": "module",
@@ -1,5 +1,5 @@
1
1
  import { alan, bot, hal, uoid, utilitas } from '../index.mjs';
2
- import { paginate } from 'tellegram';
2
+ import { convert, paginate } from 'tellegram';
3
3
 
4
4
  const _name = 'Broca';
5
5
  const [PRIVATE_LIMIT, GROUP_LIMIT] = [60 / 60, 60 / 20].map(x => x * 1000);
@@ -44,26 +44,26 @@ const getExtra = (ctx, options) => {
44
44
  return resp;
45
45
  };
46
46
 
47
- const resp = async (ctx, text, md, extra) => {
47
+ const resp = async (ctx, text, extra) => {
48
48
  // if (ctx._.type === 'inline_query') {
49
49
  // return await ctx.answerInlineQuery([{}, {}]);
50
50
  // }
51
51
  let resp;
52
- if (md) {
53
- try {
54
- resp = await (extra?.reply_parameters?.message_id
55
- ? ctx.replyWithMarkdown(text, { parse_mode, ...extra })
56
- : ctx.sendMessage(text, { parse_mode, ...extra }));
57
- } catch (err) {
58
- log(err);
59
- await ctx.timeout();
60
- }
52
+ if (!extra?.noMd) {
53
+ const txt = extra?.parsed ? text : convert(text);
54
+ resp = await utilitas.ignoreErrFunc(async (
55
+ ) => await (extra?.reply_parameters?.message_id
56
+ ? ctx.sendMessage(txt, { parse_mode, ...extra })
57
+ : ctx.replyWithMarkdown(txt, { parse_mode, ...extra })), hal.logOptions);
58
+ }
59
+ if (!resp) {
60
+ await ctx.timeout();
61
+ resp = await utilitas.ignoreErrFunc(
62
+ async () => await (extra?.reply_parameters?.message_id
63
+ ? ctx.sendMessage(text, extra) : ctx.reply(text, extra)
64
+ ), hal.logOptions
65
+ );
61
66
  }
62
- resp || (resp = await utilitas.ignoreErrFunc(
63
- async () => await (extra?.reply_parameters?.message_id
64
- ? ctx.reply(text, extra) : ctx.sendMessage(text, extra)
65
- ), hal.logOptions
66
- ));
67
67
  resp && ctx._.done.push(resp);
68
68
  return resp;
69
69
  };
@@ -74,22 +74,22 @@ const replyWith = async (ctx, func, src, options) => ctx._.done.push(
74
74
  })) : { [getKey(src)]: src }, ctx.getExtra(options))
75
75
  );
76
76
 
77
- const edit = async (ctx, lastMsgId, text, md, extra) => {
77
+ const edit = async (ctx, lastMsgId, text, extra) => {
78
78
  let resp;
79
- if (md) {
80
- try {
81
- resp = await ctx.telegram.editMessageText(
82
- ctx._.chatId, lastMsgId, '', text, { parse_mode, ...extra }
83
- );
84
- } catch (err) {
85
- log(err);
86
- await ctx.timeout();
87
- }
79
+ if (!extra?.noMd) {
80
+ const txt = extra?.parsed ? text : convert(text);
81
+ resp = await utilitas.ignoreErrFunc(async (
82
+ ) => await ctx.telegram.editMessageText(
83
+ ctx._.chatId, lastMsgId, '', txt, { parse_mode, ...extra }
84
+ ), hal.logOptions);
85
+ }
86
+ if (!resp) {
87
+ await ctx.timeout();
88
+ resp = await utilitas.ignoreErrFunc(async (
89
+ ) => await ctx.telegram.editMessageText(
90
+ ctx._.chatId, lastMsgId, '', text, extra
91
+ ), hal.logOptions);
88
92
  }
89
- resp || (resp = await utilitas.ignoreErrFunc(async (
90
- ) => await ctx.telegram.editMessageText(
91
- ctx._.chatId, lastMsgId, '', text, extra
92
- ), hal.logOptions));
93
93
  resp && ctx._.done.push(resp);
94
94
  return resp;
95
95
  };
@@ -121,12 +121,12 @@ const ctxExt = ctx => {
121
121
  return str;
122
122
  };
123
123
  ctx.getExtra = (options) => getExtra(ctx, options);
124
- ctx.resp = async (text, md, extra) => await resp(ctx, text, md, extra);
125
- ctx.edit = async (lastMsgId, text, md, extra) =>
126
- await edit(ctx, lastMsgId, text, md, extra);
124
+ ctx.resp = async (text, extra) => await resp(ctx, text, extra);
125
+ ctx.edit = async (lastMsgId, text, extra) =>
126
+ await edit(ctx, lastMsgId, text, extra);
127
127
  ctx.ok = async (message, options) => {
128
128
  let pages = paginate(message, options);
129
- const extra = ctx.getExtra(options);
129
+ const extra = { ...ctx.getExtra(options), parsed: true };
130
130
  const [pageIds, pageMap] = [[], {}];
131
131
  options?.pageBreak || ctx._.done.filter(x => x).map(x => {
132
132
  pageMap[x?.message_id] || (pageIds.push(x?.message_id));
@@ -140,22 +140,25 @@ const ctxExt = ctx => {
140
140
  if (lastMsgId && pageMap[lastMsgId]?.text === pages[i]) {
141
141
  continue;
142
142
  }
143
- await (lastMsgId ? ctx.edit(lastMsgId, pages[i], options?.md, _extra) // ongoing, edit
144
- : ctx.resp(pages[i], options?.md, _extra)); // new page, reply
143
+ await (lastMsgId ? ctx.edit(lastMsgId, pages[i], _extra) // ongoing, edit
144
+ : ctx.resp(pages[i], _extra)); // new page, reply
145
145
  await ctx.timeout();
146
146
  }
147
147
  return ctx._.done;
148
148
  };
149
149
  ctx.err = async (m, opts) => {
150
150
  log(m);
151
- return await ctx.ok(`⚠️ ${m?.message || m} `, opts);
151
+ return await ctx.ok(`${bot.EMOJI_WARNING} ${m?.message || m} `, {
152
+ ...opts, pageBreak: true,
153
+ });
152
154
  };
153
- ctx.shouldReply = async text => {
155
+ ctx.shouldReply = async (text, options) => {
154
156
  const should = utilitas.insensitiveHas(hal._?.chatType, ctx._.chatType)
155
157
  || ctx._.session?.config?.chatty;
156
158
  text = utilitas.isSet(text, true) ? (text || '') : '';
157
159
  if (!should || !text) { return should; }
158
- return await ctx.ok(text);
160
+ return await (options?.error
161
+ ? ctx.err(text, options) : ctx.ok(text, options));
159
162
  };
160
163
  ctx.finish = () => ctx._.done.unshift(null);
161
164
  ctx.complete = async (options) => await ctx.ok(hal.CHECK, options);
@@ -241,10 +244,10 @@ const action = async (ctx, next) => {
241
244
  await sessionSet(ctx._.chatId, ctx._.session);
242
245
  // fallback response and log
243
246
  if (ctx._.done.length) { return; }
244
- const errStr = '⚠️ ' + (ctx._.cmd?.cmd
245
- ? `Command not found: /${ctx._.cmd.cmd}` : 'No suitable response.');
247
+ const errStr = ctx._.cmd?.cmd
248
+ ? `Command not found: /${ctx._.cmd.cmd}` : 'No suitable response.';
246
249
  log(`INFO: ${errStr}`);
247
- await ctx.shouldReply(errStr);
250
+ await ctx.shouldReply(errStr, { error: true });
248
251
  };
249
252
 
250
253
  export const { _NEED, name, run, priority, func } = {
@@ -68,6 +68,15 @@ const action = async (ctx, next) => {
68
68
  case 'clearkb':
69
69
  return await ctx.complete({ keyboards: [] });
70
70
  }
71
+ // update commands
72
+ await utilitas.ignoreErrFunc(async () =>
73
+ await hal._.bot.telegram.setMyCommands(hal._.cmds.sort((x, y) =>
74
+ (ctx._.session?.cmds?.[y.command.toLowerCase()]?.touchedAt || 0)
75
+ - (ctx._.session?.cmds?.[x.command.toLowerCase()]?.touchedAt || 0)
76
+ ).slice(0, hal.COMMAND_LIMIT), {
77
+ scope: { type: 'chat', chat_id: ctx._.chatId },
78
+ }), hal.logOptions
79
+ );
71
80
  // next middleware
72
81
  await next();
73
82
  };
@@ -3,7 +3,7 @@ import { bot, hal, utilitas } from '../index.mjs';
3
3
  let lorem;
4
4
 
5
5
  const action = async (ctx, next) => {
6
- let resp, md = true;
6
+ let resp;
7
7
  switch (ctx._.cmd.cmd) {
8
8
  case 'echo':
9
9
  const carry = { ...ctx._ };
@@ -41,17 +41,17 @@ const action = async (ctx, next) => {
41
41
  await ctx.ok(ipsum(), { ...extra, processing: true });
42
42
  }
43
43
  await ctx.timeout();
44
- await ctx.ok(ipsum(), { ...extra, md });
44
+ await ctx.ok(ipsum(), extra);
45
45
  // testing incomplete markdown reply {
46
- // await ctx.ok('_8964', { md });
46
+ // await ctx.ok('_8964', extra);
47
47
  // }
48
48
  // test pagebreak {
49
49
  // await ctx.timeout();
50
- // await ctx.ok(ipsum(), { md, pageBreak: true });
50
+ // await ctx.ok(ipsum(), { pageBreak: true });
51
51
  // }
52
52
  return;
53
53
  }
54
- await ctx.ok(resp, { md });
54
+ await ctx.ok(resp);
55
55
  };
56
56
 
57
57
  export const { _NEED, name, hidden, priority, func, help, cmds } = {
@@ -30,7 +30,7 @@ const action = async (ctx, next) => {
30
30
  }
31
31
  _help.length && help.push(bot.lines([`*${i.toUpperCase()}*`, ..._help]));
32
32
  }
33
- await ctx.ok(lines2(help), { md: true });
33
+ await ctx.ok(lines2(help));
34
34
  };
35
35
 
36
36
  export const { name, priority, func, help, cmds } = {
@@ -35,31 +35,21 @@ const memorize = async (ctx) => {
35
35
  }
36
36
  await hal._.storage?.client?.upsert?.(hal.table, event, { skipEcho: true });
37
37
  }, hal.logOptions);
38
- // TODO: 調整,如果命令執行過,應該更新菜單 !?
39
- await utilitas.ignoreErrFunc(async () => await hal._.bot.telegram.setMyCommands([
40
- ...hal._.cmds, ...Object.keys(ctx._.message.prompts || {}).map(
41
- command => hal.newCommand(command, ctx._.message.prompts[command])
42
- )
43
- ].sort((x, y) =>
44
- (ctx._.message?.cmds?.[y.command.toLowerCase()]?.touchedAt || 0)
45
- - (ctx._.message?.cmds?.[x.command.toLowerCase()]?.touchedAt || 0)
46
- ).slice(0, hal.COMMAND_LIMIT), {
47
- scope: { type: 'chat', chat_id: ctx._.chatId },
48
- }), hal.logOptions);
49
38
  };
50
39
 
51
40
  const ctxExt = ctx => {
52
41
  ctx.memorize = async () => await memorize(ctx);
53
42
  ctx.recall = async (keyword, offset = 0, limit = hal.SEARCH_LIMIT, options = {}) =>
54
- await recall(ctx._.chatId, keyword, offset, limit, options);
43
+ await hal.recall(ctx._.chatId, keyword, offset, limit, options);
55
44
  // ctx.getContext = async (offset = 0, limit = hal.SEARCH_LIMIT, options = {}) =>
56
- // await getContext(ctx._.chatId, offset, limit, options);
45
+ // await hal.getContext(ctx._.chatId, offset, limit, options);
57
46
  };
58
47
 
59
48
  const action = async (ctx, next) => {
60
49
  ctxExt(ctx);
61
50
  switch (ctx._.cmd?.cmd) {
62
51
  case 'search':
52
+ // print(ctx.update.callback_query?.message);
63
53
  (ctx._.type === 'callback_query')
64
54
  && await ctx.deleteMessage(ctx._.message.message_id);
65
55
  const regex = '[-—]+skip=[0-9]*';
@@ -71,27 +61,32 @@ const action = async (ctx, next) => {
71
61
  const result = await ctx.recall(keywords, offset);
72
62
  for (const i in result) {
73
63
  const content = bot.lines([
74
- '```↩️', compactLimit(result[i].response_text), '```',
64
+ '```↩️', compactLimit(result[i].response), '```',
75
65
  [`${utilitas.getTimeIcon(result[i].created_at)} ${result[i].created_at.toLocaleString()}`,
76
66
  `🏆 ${(Math.round(result[i].score * 100) / 100).toFixed(2)}`].join(' '),
77
67
  ]);
78
- await ctx.resp(content, true, {
68
+ await ctx.resp(content, {
79
69
  reply_parameters: {
80
70
  message_id: result[i].message_id,
81
71
  }, disable_notification: ~~i > 0,
82
72
  });
83
73
  await ctx.timeout();
84
74
  }
85
- // TODO: NEED MORE DEBUG
86
- result.length === hal.SEARCH_LIMIT && await ctx.resp(
87
- '___', true, ctx.getExtra({
75
+ const options = {
76
+ reply_parameters: {
77
+ message_id: ctx.update.callback_query?.message?.reply_to_message?.message_id
78
+ || ctx._.message.message_id,
79
+ }
80
+ };
81
+ result.length === hal.SEARCH_LIMIT ? await ctx.resp(
82
+ '___', ctx.getExtra({
88
83
  buttons: [{
89
84
  label: '🔍 More',
90
85
  text: `/search@${ctx.botInfo.username} ${keywords} `
91
86
  + `--skip=${offset + result.length}`,
92
- }]
93
- }));
94
- result.length || await ctx.err('No more records.');
87
+ }],
88
+ ...options,
89
+ })) : await ctx.err('No more records.', options);
95
90
  break;
96
91
  default:
97
92
  await next();
@@ -17,7 +17,7 @@ const listAIs = async ctx => {
17
17
  : ''}${x.label}`,
18
18
  text: `/set --ai=${x.id}`,
19
19
  }));
20
- return await ctx.ok(message, { lastMessageId, buttons, md: true });
20
+ return await ctx.ok(message, { lastMessageId, buttons });
21
21
  };
22
22
 
23
23
  const action = async (ctx, next) => {
@@ -20,7 +20,7 @@ const action = async (ctx, next) => {
20
20
  }
21
21
  sResp = await ctx.ok(resp.text, {
22
22
  ...ctx._.keyboards ? { keyboards: ctx._.keyboards } : {},
23
- md: true, ...extra, ...options || {},
23
+ ...extra, ...options || {},
24
24
  });
25
25
  lastSent = curTime;
26
26
  return sResp;