@apidog/multibot-sdk-ts
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -97,2 +97,3 @@ /// <reference types="node" /> | ||
reply_markup?: Markup; | ||
__filename?: string; | ||
}) => Promise<Message>; | ||
@@ -113,2 +114,3 @@ sendAudio: (props: { | ||
reply_markup?: Markup; | ||
__filename?: string; | ||
}) => Promise<Message>; | ||
@@ -127,2 +129,3 @@ sendDocument: (props: { | ||
allow_sending_without_reply?: boolean; | ||
__filename?: string; | ||
}) => Promise<Message>; | ||
@@ -144,2 +147,3 @@ sendVideo: (props: { | ||
reply_markup?: Markup; | ||
__filename?: string; | ||
}) => Promise<Message>; | ||
@@ -160,2 +164,3 @@ sendAnimation: (props: { | ||
reply_markup?: Markup; | ||
__filename?: string; | ||
}) => Promise<Message>; | ||
@@ -173,2 +178,3 @@ sendVoice: (props: { | ||
reply_markup?: Markup; | ||
__filename?: string; | ||
}) => Promise<Message>; | ||
@@ -185,2 +191,3 @@ sendVideoNote: (props: { | ||
reply_markup?: Markup; | ||
__filename?: string; | ||
}) => Promise<Message>; | ||
@@ -187,0 +194,0 @@ sendMediaGroup: (props: { |
@@ -25,2 +25,4 @@ "use strict"; | ||
const FormData = require("form-data"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const abstract_bot_1 = require("../abstract-bot"); | ||
@@ -32,21 +34,34 @@ const matcher_1 = require("./matcher"); | ||
super(); | ||
this.getApiEndpoint = (method) => { | ||
return `${this.config.apiUrl}/bot${this.config.secret}/${method}`; | ||
}; | ||
this.getApiEndpoint = (method) => `${this.config.apiUrl}/bot${this.config.secret}/${method}`; | ||
this.createFormDataFromParams = (params) => { | ||
return Object.entries(params).reduce((form, [key, value]) => { | ||
if (value !== undefined) { | ||
switch (typeof value) { | ||
case 'number': | ||
case 'boolean': { | ||
value = String(value); | ||
break; | ||
} | ||
case 'object': { | ||
value = JSON.stringify(value); | ||
break; | ||
} | ||
if (value === undefined) { | ||
return form; | ||
} | ||
if (typeof value === 'string' && ['photo', 'video', 'audio', 'document'].includes(key) && !value.startsWith('http') && fs.existsSync(value)) { | ||
value = fs.createReadStream(value); | ||
} | ||
if (value instanceof Buffer || value instanceof fs.ReadStream) { | ||
let filename = 'filename'; // fallback | ||
if ('__filename' in params) { | ||
filename = params.__filename; // user-specified name | ||
} | ||
form.append(key, value); | ||
else if (value instanceof fs.ReadStream && typeof value.path === 'string') { | ||
filename = path.basename(value.path); // file stream path | ||
} | ||
form.append(key, value, { filename }); | ||
return form; | ||
} | ||
switch (typeof value) { | ||
case 'number': | ||
case 'boolean': { | ||
value = String(value); | ||
break; | ||
} | ||
case 'object': { | ||
value = JSON.stringify(value); | ||
break; | ||
} | ||
} | ||
form.append(key, value); | ||
return form; | ||
@@ -56,10 +71,11 @@ }, new FormData()); | ||
this.request = (apiMethod, params = {}) => __awaiter(this, void 0, void 0, function* () { | ||
var _a; | ||
const url = this.getApiEndpoint(apiMethod); | ||
const form = this.createFormDataFromParams(params); | ||
const { data, status, statusText } = yield axios_1.default.post(this.getApiEndpoint(apiMethod), form, { | ||
headers: form.getHeaders(), | ||
}); | ||
if (status !== 200) { | ||
throw new Error(`Error HTTP ${statusText}`); | ||
const headers = form.getHeaders(); | ||
const { data, status, statusText } = yield axios_1.default.post(url, form, { headers }); | ||
if (data === null || data === void 0 ? void 0 : data.ok) { | ||
return data.result; | ||
} | ||
return data.result; | ||
throw new Error(`Error HTTP ${status} ${statusText}: ${(_a = data) === null || _a === void 0 ? void 0 : _a.description}`); | ||
}); | ||
@@ -145,5 +161,5 @@ /** | ||
if (!this.events[event]) { | ||
this.events[event] = []; | ||
this.events[event] = new Set(); | ||
} | ||
this.events[event].push(listener); | ||
this.events[event].add(listener); | ||
return this; | ||
@@ -150,0 +166,0 @@ }; |
{ | ||
"name": "@apidog/multibot-sdk-ts", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Telegram and VK bot SDK for TypeScript", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
165478
2504
3