teh-bot 1.0.2 → 1.0.3

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/index.js +25 -13
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -384,6 +384,11 @@ class TelegramBot extends EventEmitter {
384
384
  }
385
385
 
386
386
  command(cmd, handler) {
387
+ if (handler === undefined) {
388
+ const commandName = typeof cmd === "string" ? (cmd.startsWith("/") ? cmd : `/${cmd}`) : ""
389
+ return this.commands.get(commandName)
390
+ }
391
+
387
392
  if (typeof handler !== "function") {
388
393
  throw new Error("Command handler must be a function")
389
394
  }
@@ -597,16 +602,17 @@ class TelegramBot extends EventEmitter {
597
602
  const message = update.message || update.edited_message || update.channel_post || update.callback_query?.message
598
603
 
599
604
  // Fallback logic for chat extraction from various update types
600
- let chat =
601
- message?.chat || update.my_chat_member?.chat || update.chat_member?.chat || update.chat_join_request?.chat
602
-
603
- // Explicit extraction for callback_query which might not have a message (inline buttons)
604
- if (!chat && update.callback_query) {
605
- chat = update.callback_query.message?.chat
606
- }
605
+ const chat =
606
+ update.message?.chat ||
607
+ update.callback_query?.message?.chat ||
608
+ update.my_chat_member?.chat ||
609
+ update.chat_member?.chat ||
610
+ update.chat_join_request?.chat ||
611
+ update.edited_message?.chat ||
612
+ update.channel_post?.chat
607
613
 
608
614
  const from =
609
- message?.from ||
615
+ update.message?.from ||
610
616
  update.callback_query?.from ||
611
617
  update.inline_query?.from ||
612
618
  update.chosen_inline_result?.from ||
@@ -632,18 +638,24 @@ class TelegramBot extends EventEmitter {
632
638
 
633
639
  // Baileys-style simplified response
634
640
  ctx.send = (content, opts) => {
635
- const chatId = ctx.chat?.id || update.callback_query?.from?.id || update.inline_query?.from?.id
641
+ const chatId =
642
+ ctx.chat?.id ||
643
+ update.callback_query?.from?.id ||
644
+ update.inline_query?.from?.id ||
645
+ update.message?.from?.id ||
646
+ from?.id
647
+
636
648
  if (!chatId) {
637
- console.error("[v0] Context update without chat_id:", JSON.stringify(update))
638
- throw new Error("[Teh] Cannot send message: chat_id is not available in this context")
649
+ console.error("[v0] Context update without valid chatId destination:", JSON.stringify(update))
650
+ throw new Error("[Teh] Cannot send message: chat_id could not be resolved from this update context")
639
651
  }
640
652
  return this.sendMessage(chatId, content, opts)
641
653
  }
642
654
 
643
655
  ctx.reply = (text, opts) => {
644
- const chatId = ctx.chat?.id || update.callback_query?.from?.id
656
+ const chatId = ctx.chat?.id || update.callback_query?.from?.id || from?.id
645
657
  if (!chatId) {
646
- throw new Error("[Teh] Cannot reply: chat_id is not available in this context")
658
+ throw new Error("[Teh] Cannot reply: chat_id could not be resolved from this update context")
647
659
  }
648
660
  return this.sendMessage(chatId, text, {
649
661
  reply_to_message_id: ctx.message?.message_id,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teh-bot",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Lightweight, high-performance Telegram Bot API module with zero dependencies",
5
5
  "keywords": [
6
6
  "telegram",