halbot 1995.1.7 → 1995.1.9

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.7",
4
+ "version": "1995.1.9",
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,41 +44,45 @@ 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
+ extra = getExtra(ctx, extra);
51
52
  let resp;
52
- if (md) {
53
+ if (!extra?.noMd) {
54
+ const txt = extra?.parsed ? text : convert(text);
53
55
  resp = await utilitas.ignoreErrFunc(async (
54
56
  ) => await (extra?.reply_parameters?.message_id
55
- ? ctx.replyWithMarkdown(text, { parse_mode, ...extra })
56
- : ctx.sendMessage(text, { parse_mode, ...extra })), hal.logOptions);
57
+ ? ctx.sendMessage(txt, { parse_mode, ...extra })
58
+ : ctx.replyWithMarkdown(txt, { parse_mode, ...extra })), hal.logOptions);
57
59
  }
58
60
  if (!resp) {
59
61
  await ctx.timeout();
60
62
  resp = await utilitas.ignoreErrFunc(
61
63
  async () => await (extra?.reply_parameters?.message_id
62
- ? ctx.reply(text, extra) : ctx.sendMessage(text, extra)
64
+ ? ctx.sendMessage(text, extra) : ctx.reply(text, extra)
63
65
  ), hal.logOptions
64
66
  );
65
67
  }
66
- resp && ctx._.done.push(resp);
68
+ resp && ctx._.done.push({ ...resp, raw: text });
67
69
  return resp;
68
70
  };
69
71
 
70
72
  const replyWith = async (ctx, func, src, options) => ctx._.done.push(
71
73
  await ctx[func](Array.isArray(src) ? src.map(x => ({
72
74
  type: x.type || 'photo', media: { [getKey(x.src)]: x.src },
73
- })) : { [getKey(src)]: src }, ctx.getExtra(options))
75
+ })) : { [getKey(src)]: src }, getExtra(ctx, options))
74
76
  );
75
77
 
76
- const edit = async (ctx, lastMsgId, text, md, extra) => {
78
+ const edit = async (ctx, lastMsgId, text, extra) => {
79
+ extra = getExtra(ctx, extra);
77
80
  let resp;
78
- if (md) {
81
+ if (!extra?.noMd) {
82
+ const txt = extra?.parsed ? text : convert(text);
79
83
  resp = await utilitas.ignoreErrFunc(async (
80
84
  ) => await ctx.telegram.editMessageText(
81
- ctx._.chatId, lastMsgId, '', text, { parse_mode, ...extra }
85
+ ctx._.chatId, lastMsgId, '', txt, { parse_mode, ...extra }
82
86
  ), hal.logOptions);
83
87
  }
84
88
  if (!resp) {
@@ -88,7 +92,7 @@ const edit = async (ctx, lastMsgId, text, md, extra) => {
88
92
  ctx._.chatId, lastMsgId, '', text, extra
89
93
  ), hal.logOptions);
90
94
  }
91
- resp && ctx._.done.push(resp);
95
+ resp && ctx._.done.push({ ...resp, raw: text });
92
96
  return resp;
93
97
  };
94
98
 
@@ -118,16 +122,15 @@ const ctxExt = ctx => {
118
122
  ctx.collect(str, null, { refresh: true });
119
123
  return str;
120
124
  };
121
- ctx.getExtra = (options) => getExtra(ctx, options);
122
- ctx.resp = async (text, md, extra) => await resp(ctx, text, md, extra);
123
- ctx.edit = async (lastMsgId, text, md, extra) =>
124
- await edit(ctx, lastMsgId, text, md, extra);
125
+ ctx.resp = async (text, extra) => await resp(ctx, text, extra);
126
+ ctx.edit = async (lastMsgId, text, extra) =>
127
+ await edit(ctx, lastMsgId, text, extra);
125
128
  ctx.ok = async (message, options) => {
126
129
  let pages = paginate(message, options);
127
- const extra = ctx.getExtra(options);
130
+ const extra = { ...getExtra(ctx, options), parsed: true };
128
131
  const [pageIds, pageMap] = [[], {}];
129
132
  options?.pageBreak || ctx._.done.filter(x => x).map(x => {
130
- pageMap[x?.message_id] || (pageIds.push(x?.message_id));
133
+ pageMap[x?.message_id] || pageIds.push(x?.message_id);
131
134
  pageMap[x?.message_id] = x;
132
135
  });
133
136
  for (let i in pages) {
@@ -135,18 +138,20 @@ const ctxExt = ctx => {
135
138
  const shouldExtra = options?.lastMessageId || lastPage;
136
139
  const _extra = shouldExtra ? extra : {};
137
140
  const lastMsgId = ~~(options?.lastMessageId || pageIds[~~i]);
138
- if (lastMsgId && pageMap[lastMsgId]?.text === pages[i]) {
141
+ if (lastMsgId && pageMap[lastMsgId]?.raw === pages[i]) {
139
142
  continue;
140
143
  }
141
- await (lastMsgId ? ctx.edit(lastMsgId, pages[i], options?.md, _extra) // ongoing, edit
142
- : ctx.resp(pages[i], options?.md, _extra)); // new page, reply
144
+ await (lastMsgId ? ctx.edit(lastMsgId, pages[i], _extra) // ongoing, edit
145
+ : ctx.resp(pages[i], _extra)); // new page, reply
143
146
  await ctx.timeout();
144
147
  }
145
148
  return ctx._.done;
146
149
  };
147
150
  ctx.err = async (m, opts) => {
148
151
  log(m);
149
- return await ctx.ok(`${bot.EMOJI_WARNING} ${m?.message || m} `, opts);
152
+ return await ctx.ok(`${bot.EMOJI_WARNING} ${m?.message || m} `, {
153
+ ...opts, pageBreak: true,
154
+ });
150
155
  };
151
156
  ctx.shouldReply = async (text, options) => {
152
157
  const should = utilitas.insensitiveHas(hal._?.chatType, ctx._.chatType)
@@ -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 } = {
@@ -40,15 +40,16 @@ const memorize = async (ctx) => {
40
40
  const ctxExt = ctx => {
41
41
  ctx.memorize = async () => await memorize(ctx);
42
42
  ctx.recall = async (keyword, offset = 0, limit = hal.SEARCH_LIMIT, options = {}) =>
43
- await recall(ctx._.chatId, keyword, offset, limit, options);
43
+ await hal.recall(ctx._.chatId, keyword, offset, limit, options);
44
44
  // ctx.getContext = async (offset = 0, limit = hal.SEARCH_LIMIT, options = {}) =>
45
- // await getContext(ctx._.chatId, offset, limit, options);
45
+ // await hal.getContext(ctx._.chatId, offset, limit, options);
46
46
  };
47
47
 
48
48
  const action = async (ctx, next) => {
49
49
  ctxExt(ctx);
50
50
  switch (ctx._.cmd?.cmd) {
51
51
  case 'search':
52
+ // print(ctx.update.callback_query?.message);
52
53
  (ctx._.type === 'callback_query')
53
54
  && await ctx.deleteMessage(ctx._.message.message_id);
54
55
  const regex = '[-—]+skip=[0-9]*';
@@ -60,26 +61,30 @@ const action = async (ctx, next) => {
60
61
  const result = await ctx.recall(keywords, offset);
61
62
  for (const i in result) {
62
63
  const content = bot.lines([
63
- '```↩️', compactLimit(result[i].response_text), '```',
64
+ '```↩️', compactLimit(result[i].response), '```',
64
65
  [`${utilitas.getTimeIcon(result[i].created_at)} ${result[i].created_at.toLocaleString()}`,
65
66
  `🏆 ${(Math.round(result[i].score * 100) / 100).toFixed(2)}`].join(' '),
66
67
  ]);
67
- await ctx.resp(content, true, {
68
+ await ctx.resp(content, {
68
69
  reply_parameters: {
69
70
  message_id: result[i].message_id,
70
71
  }, disable_notification: ~~i > 0,
71
72
  });
72
73
  await ctx.timeout();
73
74
  }
74
- result.length === hal.SEARCH_LIMIT && await ctx.resp(
75
- '___', true, ctx.getExtra({
76
- buttons: [{
77
- label: '🔍 More',
78
- text: `/search@${ctx.botInfo.username} ${keywords} `
79
- + `--skip=${offset + result.length}`,
80
- }]
81
- }));
82
- result.length || await ctx.err('No more records.');
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
+ buttons: [{
83
+ label: '🔍 More',
84
+ text: `/search@${ctx.botInfo.username} ${keywords} `
85
+ + `--skip=${offset + result.length}`,
86
+ }], ...options,
87
+ }) : await ctx.err('No more records.', options);
83
88
  break;
84
89
  default:
85
90
  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) => {
@@ -6,7 +6,7 @@ const log = (c, o) => utilitas.log(c, _name, { time: 1, ...o || {} });
6
6
  const action = async (ctx, next) => {
7
7
  if (!ctx._.text && !ctx._.collected.length) { return await next(); }
8
8
  let [resp, extra, lock, sResp, lastMsg, lastSent] =
9
- [null, { buttons: [] }, 1000 * 5, null, null, 0];
9
+ [null, { buttons: [] }, 1000 * 3, null, null, 0];
10
10
  const ok = async options => {
11
11
  const curTime = Date.now();
12
12
  if (options?.processing && (
@@ -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;
@@ -42,6 +42,7 @@ const action = async (ctx, next) => {
42
42
  await ctx.audio(audio.data, { caption: audio.caption });
43
43
  }
44
44
  // print(resp);
45
+ await ctx.timeout();
45
46
  await resp.text.trim() ? ok({ processing: false })
46
47
  : ctx.deleteMessage(ctx._.done[0].message_id);
47
48
  ctx._.request = resp.request;