opencode-telegram-bot 1.0.0 → 1.0.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/dist/index.js +45 -46
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -18842,9 +18842,9 @@ async function startTelegram(options) {
18842
18842
  "Bot commands:\n" +
18843
18843
  "/new - Start a new conversation\n" +
18844
18844
  "/sessions - List your sessions\n" +
18845
- "/switch <id> - Switch to a different session\n" +
18845
+ "/switch <number> - Switch to a different session\n" +
18846
18846
  "/title <text> - Rename the current session\n" +
18847
- "/delete <id> - Delete a session\n" +
18847
+ "/delete <number> - Delete a session\n" +
18848
18848
  "/help - Show this help message\n";
18849
18849
  if (opencodeCommands.size > 0) {
18850
18850
  msg +=
@@ -18859,9 +18859,9 @@ async function startTelegram(options) {
18859
18859
  "Bot commands:\n" +
18860
18860
  "/new - Start a new conversation\n" +
18861
18861
  "/sessions - List your sessions\n" +
18862
- "/switch <id> - Switch to a different session\n" +
18862
+ "/switch <number> - Switch to a different session\n" +
18863
18863
  "/title <text> - Rename the current session\n" +
18864
- "/delete <id> - Delete a session\n" +
18864
+ "/delete <number> - Delete a session\n" +
18865
18865
  "/help - Show this help message\n";
18866
18866
  if (opencodeCommands.size > 0) {
18867
18867
  msg +=
@@ -18878,34 +18878,49 @@ async function startTelegram(options) {
18878
18878
  console.log(`[Telegram] Session cleared for chat ${chatId}`);
18879
18879
  ctx.reply("Conversation reset. Your next message will start a new session.");
18880
18880
  });
18881
+ /**
18882
+ * Get the list of known sessions, sorted by most recently updated.
18883
+ */
18884
+ async function getKnownSessions() {
18885
+ const list = await client.session.list();
18886
+ if (!list.data || list.data.length === 0)
18887
+ return [];
18888
+ return list.data
18889
+ .filter((s) => knownSessionIds.has(s.id))
18890
+ .sort((a, b) => b.time.updated - a.time.updated);
18891
+ }
18892
+ /**
18893
+ * Resolve a user argument to a session. Accepts either a numeric index
18894
+ * (1-based, as shown by /sessions) or a session ID / prefix.
18895
+ */
18896
+ function resolveSession(sessions, arg) {
18897
+ // Try numeric index first
18898
+ const num = parseInt(arg, 10);
18899
+ if (!isNaN(num) && String(num) === arg && num >= 1 && num <= sessions.length) {
18900
+ return sessions[num - 1];
18901
+ }
18902
+ // Fall back to ID / prefix match
18903
+ return sessions.find((s) => s.id === arg || s.id.startsWith(arg));
18904
+ }
18881
18905
  // Handle /sessions command - list Telegram bot sessions only
18882
18906
  bot.command("sessions", async (ctx) => {
18883
18907
  const chatId = ctx.chat.id.toString();
18884
18908
  const activeSessionId = chatSessions.get(chatId);
18885
18909
  try {
18886
- const list = await client.session.list();
18887
- if (!list.data || list.data.length === 0) {
18888
- await ctx.reply("No sessions found.");
18889
- return;
18890
- }
18891
- // Filter to only sessions created by this bot
18892
- const sessions = list.data
18893
- .filter((s) => knownSessionIds.has(s.id))
18894
- .sort((a, b) => b.time.updated - a.time.updated);
18910
+ const sessions = await getKnownSessions();
18895
18911
  if (sessions.length === 0) {
18896
18912
  await ctx.reply("No sessions found.");
18897
18913
  return;
18898
18914
  }
18899
18915
  let msg = "Your sessions:\n\n";
18900
- for (const session of sessions) {
18916
+ sessions.forEach((session, i) => {
18901
18917
  const isActive = session.id === activeSessionId;
18902
18918
  const age = formatAge(session.time.updated);
18903
- const shortId = session.id.substring(0, 8);
18904
- const marker = isActive ? "[active] " : "";
18905
- msg += `${marker}${session.title} - ${age} (${shortId})\n`;
18906
- }
18907
- msg += `\nUse /switch <id> to switch sessions.`;
18908
- msg += `\nUse /delete <id> to delete a session.`;
18919
+ const marker = isActive ? " [active]" : "";
18920
+ msg += `${i + 1}. ${session.title} - ${age}${marker}\n`;
18921
+ });
18922
+ msg += `\nUse /switch <number> to switch sessions.`;
18923
+ msg += `\nUse /delete <number> to delete a session.`;
18909
18924
  await ctx.reply(msg);
18910
18925
  }
18911
18926
  catch (err) {
@@ -18913,24 +18928,17 @@ async function startTelegram(options) {
18913
18928
  await ctx.reply("Failed to list sessions.");
18914
18929
  }
18915
18930
  });
18916
- // Handle /switch <id> command - switch to a different session
18931
+ // Handle /switch <number> command - switch to a different session
18917
18932
  bot.command("switch", async (ctx) => {
18918
18933
  const chatId = ctx.chat.id.toString();
18919
18934
  const args = ctx.message.text.replace(/^\/switch\s*/, "").trim();
18920
18935
  if (!args) {
18921
- await ctx.reply("Usage: /switch <session-id>\n\nUse /sessions to see available sessions.");
18936
+ await ctx.reply("Usage: /switch <number>\n\nUse /sessions to see available sessions.");
18922
18937
  return;
18923
18938
  }
18924
18939
  try {
18925
- // Fetch all sessions and find one matching the provided prefix
18926
- const list = await client.session.list();
18927
- if (!list.data) {
18928
- await ctx.reply("Failed to fetch sessions from the server.");
18929
- return;
18930
- }
18931
- const match = list.data
18932
- .filter((s) => knownSessionIds.has(s.id))
18933
- .find((s) => s.id === args || s.id.startsWith(args));
18940
+ const sessions = await getKnownSessions();
18941
+ const match = resolveSession(sessions, args);
18934
18942
  if (!match) {
18935
18943
  await ctx.reply(`No session found matching "${args}".\n\nUse /sessions to see available sessions.`);
18936
18944
  return;
@@ -18938,8 +18946,7 @@ async function startTelegram(options) {
18938
18946
  chatSessions.set(chatId, match.id);
18939
18947
  saveSessions();
18940
18948
  console.log(`[Telegram] Switched chat ${chatId} to session ${match.id}`);
18941
- const shortId = match.id.substring(0, 8);
18942
- await ctx.reply(`Switched to session: ${match.title} (${shortId})`);
18949
+ await ctx.reply(`Switched to session: ${match.title}`);
18943
18950
  }
18944
18951
  catch (err) {
18945
18952
  console.error("[Telegram] Error switching session:", err);
@@ -18975,24 +18982,17 @@ async function startTelegram(options) {
18975
18982
  await ctx.reply("Failed to rename session.");
18976
18983
  }
18977
18984
  });
18978
- // Handle /delete <id> command - delete a session
18985
+ // Handle /delete <number> command - delete a session
18979
18986
  bot.command("delete", async (ctx) => {
18980
18987
  const chatId = ctx.chat.id.toString();
18981
18988
  const args = ctx.message.text.replace(/^\/delete\s*/, "").trim();
18982
18989
  if (!args) {
18983
- await ctx.reply("Usage: /delete <session-id>\n\nUse /sessions to see available sessions.");
18990
+ await ctx.reply("Usage: /delete <number>\n\nUse /sessions to see available sessions.");
18984
18991
  return;
18985
18992
  }
18986
18993
  try {
18987
- // Fetch all sessions and find one matching the provided prefix
18988
- const list = await client.session.list();
18989
- if (!list.data) {
18990
- await ctx.reply("Failed to fetch sessions from the server.");
18991
- return;
18992
- }
18993
- const match = list.data
18994
- .filter((s) => knownSessionIds.has(s.id))
18995
- .find((s) => s.id === args || s.id.startsWith(args));
18994
+ const sessions = await getKnownSessions();
18995
+ const match = resolveSession(sessions, args);
18996
18996
  if (!match) {
18997
18997
  await ctx.reply(`No session found matching "${args}".\n\nUse /sessions to see available sessions.`);
18998
18998
  return;
@@ -19013,9 +19013,8 @@ async function startTelegram(options) {
19013
19013
  // Remove from known sessions
19014
19014
  knownSessionIds.delete(match.id);
19015
19015
  saveSessions();
19016
- const shortId = match.id.substring(0, 8);
19017
19016
  console.log(`[Telegram] Deleted session ${match.id}`);
19018
- await ctx.reply(`Deleted session: ${match.title} (${shortId})`);
19017
+ await ctx.reply(`Deleted session: ${match.title}`);
19019
19018
  }
19020
19019
  catch (err) {
19021
19020
  console.error("[Telegram] Error deleting session:", err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-telegram-bot",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "type": "module",
5
5
  "description": "Telegram bot that forwards messages to an OpenCode agent",
6
6
  "main": "./dist/index.js",