teh-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/index.js +54 -44
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -19,10 +19,6 @@ const DEFAULT_OPTIONS = {
19
19
  baseApiUrl: "https://api.telegram.org",
20
20
  }
21
21
 
22
- /**
23
- * Modern, High-Performance Telegram Bot API Module
24
- * Inspired by Baileys simplified API and Telegraf's robust system
25
- */
26
22
  class TelegramBot extends EventEmitter {
27
23
  constructor(token, options = {}) {
28
24
  super()
@@ -121,50 +117,13 @@ class TelegramBot extends EventEmitter {
121
117
  if (!mediaType) return this.request("sendMessage", { chat_id: chatId, text: content.text, ...options })
122
118
 
123
119
  const method = `send${mediaType.charAt(0).toUpperCase() + mediaType.slice(1)}`
124
- const formData = { chat_id: chatId, caption, ...rest }
125
-
126
- if (
127
- mediaSource instanceof Stream ||
128
- Buffer.isBuffer(mediaSource) ||
129
- (typeof mediaSource === "string" && !mediaSource.startsWith("http"))
130
- ) {
131
- formData[mediaType] = await this._prepareFile(mediaSource, mediaType)
132
- return this.request(method, {}, formData)
133
- }
120
+ const formData = { chat_id: chatId, caption, [mediaType]: await this._prepareFile(mediaSource, mediaType), ...rest }
134
121
 
135
- formData[mediaType] = mediaSource
136
- return this.request(method, formData)
122
+ return this.request(method, {}, formData)
137
123
  }
138
124
 
139
125
  async sendPhoto(chatId, photo, options = {}) {
140
- if (typeof photo === "string" && (photo.startsWith("http") || photo.startsWith("/"))) {
141
- if (photo.startsWith("http")) {
142
- return this.request("sendPhoto", {
143
- chat_id: chatId,
144
- photo,
145
- ...options,
146
- })
147
- } else {
148
- const fileData = await this._readFileSync(photo)
149
- const formData = {
150
- chat_id: chatId,
151
- photo: {
152
- stream: true,
153
- filename: basename(photo),
154
- contentType: "image/jpeg",
155
- data: fileData,
156
- },
157
- ...this._flattenOptions(options),
158
- }
159
- return this.request("sendPhoto", {}, formData)
160
- }
161
- } else {
162
- return this.request("sendPhoto", {
163
- chat_id: chatId,
164
- photo,
165
- ...options,
166
- })
167
- }
126
+ return this._sendFile("sendPhoto", chatId, photo, "photo", options)
168
127
  }
169
128
 
170
129
  async sendAudio(chatId, audio, options = {}) {
@@ -758,7 +717,28 @@ class TelegramBot extends EventEmitter {
758
717
  async _prepareFile(source, type) {
759
718
  if (source instanceof Stream) return { data: source, contentType: this._getMime(type) }
760
719
  if (Buffer.isBuffer(source)) return { data: source, contentType: this._getMime(type) }
720
+
761
721
  if (typeof source === "string") {
722
+ if (source.startsWith("http")) {
723
+ // Fetch remote URL and return as stream
724
+ return new Promise((resolve, reject) => {
725
+ const client = source.startsWith("https") ? https : http
726
+ client
727
+ .get(source, (res) => {
728
+ if (res.statusCode !== 200) {
729
+ return reject(new Error(`Failed to fetch remote file: ${res.statusCode}`))
730
+ }
731
+ resolve({
732
+ data: res,
733
+ filename: basename(new URL(source).pathname) || `file_${Date.now()}`,
734
+ contentType: res.headers["content-type"] || this._getMime(type),
735
+ })
736
+ })
737
+ .on("error", reject)
738
+ })
739
+ }
740
+
741
+ // Local file
762
742
  return {
763
743
  data: createReadStream(source),
764
744
  filename: basename(source),
@@ -780,6 +760,18 @@ class TelegramBot extends EventEmitter {
780
760
  return mimes[ext] || "application/octet-stream"
781
761
  }
782
762
 
763
+ _flattenOptions(options) {
764
+ const flat = {}
765
+ for (const [key, value] of Object.entries(options)) {
766
+ if (typeof value === "object" && value !== null) {
767
+ flat[key] = JSON.stringify(value)
768
+ } else {
769
+ flat[key] = value
770
+ }
771
+ }
772
+ return flat
773
+ }
774
+
783
775
  _formatError(resp) {
784
776
  const err = new Error(resp.description || "Unknown Telegram Error")
785
777
  err.code = resp.error_code
@@ -803,6 +795,24 @@ class TelegramBot extends EventEmitter {
803
795
  async getWebhookInfo() {
804
796
  return this.request("getWebhookInfo")
805
797
  }
798
+
799
+ static InlineKeyboard() {
800
+ return new InlineKeyboardBuilder()
801
+ }
802
+
803
+ static ReplyKeyboard() {
804
+ return new ReplyKeyboardBuilder()
805
+ }
806
+
807
+ static RemoveKeyboard(selective = false) {
808
+ return { remove_keyboard: true, selective }
809
+ }
810
+
811
+ static ForceReply(selective = false, placeholder = "") {
812
+ const obj = { force_reply: true, selective }
813
+ if (placeholder) obj.input_field_placeholder = placeholder
814
+ return obj
815
+ }
806
816
  }
807
817
 
808
818
  class InlineKeyboardBuilder {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teh-bot",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Lightweight, high-performance Telegram Bot API module with zero dependencies",
5
5
  "keywords": [
6
6
  "telegram",