Skip to content

Commit

Permalink
add FileOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Borodin committed Jul 24, 2024
1 parent 2f5eb19 commit 5a2987a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ If the API error includes a `retry_after` field, the library will retry the requ
If the error is not related to the API, the Promise will be rejected with a different error.

For sending files, you can use not only ```'file_id'``` or ```'url'```, but also ```stream.Readable``` or ```Buffer```.
To send files with additional parameters, such as a filename or specific contentType, use the ```FileOptions``` wrapper class.
```typescript
import {createReadStream} from 'fs';
import {readFile} from 'fs/promises';
import { TelegramBot, FileOptions } from 'typescript-telegram-bot-api';
import { createReadStream } from 'fs';
import { readFile } from 'fs/promises';


await bot.sendPhoto({
Expand Down Expand Up @@ -81,11 +83,24 @@ await bot.sendPhoto({
caption: 'buffer',
});

// or

await bot.sendPhoto({
chat_id: chat_id,
photo: new FileOptions(
await readFile('photo.jpg'), {
filename: 'custom_file_name.jpg',
contentType: 'image/jpeg',
}
),
caption: 'FileOptions',
});

// or in browser

await bot.sendPhoto({
chat_id: chat_id,
photo: input.files[0],
photo: input.files[0], // or new File(…)
caption: 'file',
});
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "commonjs",
"name": "typescript-telegram-bot-api",
"version": "0.1.26",
"version": "0.1.27",
"description": "Telegram Bot API wrapper for Node.js written in TypeScript",
"repository": "github:Borodin/typescript-telegram-bot-api",
"main": "dist/index.js",
Expand Down
25 changes: 20 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import axios from 'axios';
import FormData from 'form-data';
import { promisify } from 'util';
import { Readable } from 'stream';
import { TelegramError } from './errors';
import { Polling } from './pooling';
import { ReadStream } from 'fs';

import {
Update,
Expand Down Expand Up @@ -63,8 +66,6 @@ import {
Currencies,
} from './types/';
import * as TelegramTypes from './types/';
import { TelegramError } from './errors';
import { Polling } from './pooling';

const wait: (ms: number) => Promise<void> = promisify(setTimeout);

Expand All @@ -74,6 +75,13 @@ class JSONSerialized {
constructor(public value: object | undefined) {}
}

export class FileOptions {
constructor(
public file: ReadStream | Buffer | File,
public options?: FormData.AppendOptions | string,
) {}
}

export class TelegramBot extends EventEmitter {
private readonly polling: Polling;
botToken: string;
Expand Down Expand Up @@ -145,13 +153,18 @@ export class TelegramBot extends EventEmitter {
if (
value instanceof File ||
value instanceof Buffer ||
value instanceof Readable
value instanceof Readable ||
value instanceof FileOptions
) {
const name = Math.random().toString(36).substring(7);
formData.append(
name,
value,
value instanceof Buffer ? 'file' : undefined,
value instanceof FileOptions ? value.file : value,
value instanceof Buffer
? 'file'
: value instanceof FileOptions
? value.options
: undefined,
);
return `attach://${name}`;
} else if (Array.isArray(value)) {
Expand Down Expand Up @@ -180,6 +193,8 @@ export class TelegramBot extends EventEmitter {
} else if (value instanceof JSONSerialized) {
const json = JSON.stringify(this.serializeJSON(value.value, formData));
if (json !== undefined) formData.append(key, json);
} else if (value instanceof FileOptions) {
formData.append(key, value.file, value.options);
} else if (value instanceof Buffer) {
formData.append(key, value, 'file');
} else if (value instanceof File || value instanceof Readable) {
Expand Down
3 changes: 2 additions & 1 deletion src/types/InputFile.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ReadStream } from 'fs';
import { FileOptions } from '../index';

export type InputFile = ReadStream | Buffer | File;
export type InputFile = ReadStream | Buffer | File | FileOptions;
14 changes: 13 additions & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dotenv/config';
import { createReadStream } from 'fs';
import { readFile } from 'fs/promises';
import { TelegramBot } from '../src';
import { FileOptions, TelegramBot } from '../src';
import { TelegramError } from '../src/errors';
import { ForumTopic, File, User } from '../src/types';

Expand Down Expand Up @@ -386,6 +386,18 @@ describe('.sendDocument()', () => {
}),
).resolves.toHaveProperty('document.mime_type', 'application/pdf');
});

it('should send document from FileOptions', async () => {
await expect(
bot.sendDocument({
chat_id: USERID,
document: new FileOptions(await readFile('tests/data/photo.jpg'), {
filename: 'custom_file_name.jpg',
contentType: 'image/jpeg',
}),
}),
).resolves.toHaveProperty('document.file_name', 'custom_file_name.jpg');
});
});

describe('.sendVideo()', () => {
Expand Down

0 comments on commit 5a2987a

Please sign in to comment.