typescript-telegram-bot-api
Advanced tools
Comparing version 0.1.19 to 0.1.20
@@ -8,3 +8,3 @@ "use strict"; | ||
const events_1 = require("events"); | ||
const https_1 = __importDefault(require("https")); | ||
const axios_1 = __importDefault(require("axios")); | ||
const form_data_1 = __importDefault(require("form-data")); | ||
@@ -32,45 +32,27 @@ const util_1 = require("util"); | ||
async startPolling() { | ||
this.polling.start(); | ||
await this.polling.start(); | ||
} | ||
async stopPolling() { | ||
this.polling.stop(); | ||
await this.polling.stop(); | ||
} | ||
async request(url, options, abortController) { | ||
if (!abortController) { | ||
abortController = new AbortController(); | ||
const formData = this.createFormData(options); | ||
const response = await (0, axios_1.default)({ | ||
method: 'POST', | ||
url, | ||
data: formData, | ||
signal: abortController?.signal, | ||
responseType: 'text', | ||
transitional: { | ||
silentJSONParsing: false, | ||
forcedJSONParsing: false, | ||
}, | ||
validateStatus: () => true, | ||
}); | ||
try { | ||
return JSON.parse(response.data.toString()); | ||
} | ||
return new Promise((resolve, reject) => { | ||
const formData = this.createFormData(options); | ||
const request = https_1.default.request(url, { | ||
method: 'POST', | ||
headers: { | ||
...formData.getHeaders(), | ||
}, | ||
signal: abortController?.signal, | ||
}, (response) => { | ||
let responseData = ''; | ||
response.on('data', (chunk) => { | ||
responseData += chunk.toString(); | ||
}); | ||
response.on('end', () => { | ||
try { | ||
const response = JSON.parse(responseData.toString()); | ||
resolve(response); | ||
} | ||
catch (error) { | ||
reject(new Error(`Invalid response`)); | ||
} | ||
}); | ||
}); | ||
formData.on('error', (error) => { | ||
abortController.abort(); | ||
reject(error); | ||
}); | ||
request.on('error', (error) => { | ||
if (error.name !== 'AbortError') { | ||
reject(error); | ||
} | ||
}); | ||
formData.pipe(request); | ||
}); | ||
catch (error) { | ||
throw new Error(`Invalid response`); | ||
} | ||
} | ||
@@ -82,7 +64,7 @@ createFormData(options) { | ||
if (value !== undefined) { | ||
if (typeof options[key] === 'boolean') { | ||
formData.append(key, String(options[key])); | ||
if (typeof value === 'boolean') { | ||
formData.append(key, String(value)); | ||
} | ||
else if (options[key] instanceof Buffer) { | ||
formData.append(key, options[key], { | ||
else if (value instanceof Buffer) { | ||
formData.append(key, value, { | ||
filename: 'file', | ||
@@ -92,9 +74,10 @@ contentType: 'application/octet-stream', | ||
} | ||
else if (typeof options[key] === 'object' && | ||
!Array.isArray(options[key]) && | ||
!(options[key] instanceof stream_1.Readable)) { | ||
formData.append(key, JSON.stringify(options[key])); | ||
else if (value instanceof File || value instanceof stream_1.Readable) { | ||
formData.append(key, value); | ||
} | ||
else if (typeof value === 'object' && !Array.isArray(value)) { | ||
formData.append(key, JSON.stringify(value)); | ||
} | ||
else { | ||
formData.append(key, options[key]); | ||
formData.append(key, value); | ||
} | ||
@@ -104,2 +87,5 @@ } | ||
} | ||
else { | ||
formData.append('empty', 'empty'); | ||
} | ||
return formData; | ||
@@ -106,0 +92,0 @@ } |
@@ -53,5 +53,2 @@ "use strict"; | ||
} | ||
else { | ||
throw error; | ||
} | ||
} | ||
@@ -62,3 +59,3 @@ } | ||
async start() { | ||
return this.poll(); | ||
this.poll().catch(); | ||
} | ||
@@ -65,0 +62,0 @@ async stop() { |
import { ReadStream } from 'fs'; | ||
export type InputFile = ReadStream | Buffer; | ||
export type InputFile = ReadStream | Buffer | File; |
{ | ||
"type": "commonjs", | ||
"name": "typescript-telegram-bot-api", | ||
"version": "0.1.19", | ||
"version": "0.1.20", | ||
"description": "Telegram Bot API wrapper for Node.js written in TypeScript", | ||
@@ -37,2 +37,3 @@ "repository": "github:Borodin/typescript-telegram-bot-api", | ||
"@types/node": "^20.14.2", | ||
"axios-mock-adapter": "^1.22.0", | ||
"dotenv": "^16.4.5", | ||
@@ -48,2 +49,3 @@ "eslint": "^8.57.0", | ||
"dependencies": { | ||
"axios": "^1.7.2", | ||
"form-data": "^4.0.0" | ||
@@ -50,0 +52,0 @@ }, |
@@ -5,4 +5,4 @@ # 📦 typescript-telegram-bot-api | ||
[](https://www.npmjs.com/package/typescript-telegram-bot-api) | ||
[](https://codecov.io/github/Borodin/typescript-telegram-bot-api) | ||
[](https://core.telegram.org/bots/api#july-1-2024) | ||
[](https://codecov.io/github/Borodin/typescript-telegram-bot-api) | ||
@@ -83,2 +83,10 @@ | ||
}); | ||
// or in browser | ||
await bot.sendPhoto({ | ||
chat_id: chat_id, | ||
photo: input.files[0], | ||
caption: 'file', | ||
}); | ||
``` | ||
@@ -85,0 +93,0 @@ ## Events |
Sorry, the diff of this file is too big to display
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
114
0
255684
2
13
6123
+ Addedaxios@^1.7.2
+ Addedaxios@1.7.9(transitive)
+ Addedfollow-redirects@1.15.9(transitive)
+ Addedproxy-from-env@1.1.0(transitive)