Skip to content

Commit

Permalink
Added sendAnimation and sendVoice methods
Browse files Browse the repository at this point in the history
  • Loading branch information
naseif committed Mar 1, 2022
1 parent ad600fc commit 7fcc381
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 42 deletions.
52 changes: 27 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

149 changes: 132 additions & 17 deletions src/structure/TelegramAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IMessage } from "../types/IMessage";
import { IUpdate } from "../types/IUpdate";
import { IUpdateOptions } from "../types/IUpdateOptions";
import { IUser } from "../types/IUser";
import { sendVoiceOptions } from "./methodsOptions";
import {
sendMessageOptions,
sendPollOptions,
Expand All @@ -14,6 +15,7 @@ import {
sendAudioOptions,
sendVideoOptions,
sendDocumentOptions,
sendAnimationOptions,
} from "./index";
import {
TCallbackQueryCallback,
Expand Down Expand Up @@ -112,14 +114,6 @@ export class TelegramAPI {
return result;
}

async getMe(callback?: (user: IUser) => void): Promise<IUser> {
let result: IUser;
const fetch = await this.sendRequest(this.endpoint + "getMe");
result = fetch;
if (callback) callback(result);
return result;
}

onMessage(callback: TMessageCallback) {
this.onMessageCallback = callback;
}
Expand Down Expand Up @@ -293,9 +287,18 @@ export class TelegramAPI {
clearTimeout(this.timeout);
}

/**
* A simple method for testing your bot's authentication token. Requires no parameters. Returns basic information about the bot in form of a User object.
* @returns IUser
*/
async getMe(): Promise<IUser> {
const fetch: IUser = await this.sendRequest(this.endpoint + "getMe");
return fetch;
}

/**
* Use this method to send text messages. On success, the sent Message is returned.
* @param chatId Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param chatId Unique identifier for the target chat or username of the target channel.
* @param text Text of the message to be sent, 1-4096 characters after entities parsing
* @param options sendMessageOptions
* @returns IMessage
Expand Down Expand Up @@ -335,7 +338,7 @@ export class TelegramAPI {

/**
* Use this method to send a native poll. On success, the sent Message is returned.
* @param chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param chat_id Unique identifier for the target chat or username of the target channel.
* @param question Poll question, 1-300 characters
* @param answer_options A JSON-serialized list of answer options, 2-10 strings 1-100 characters each
* @param options sendPollOptions
Expand Down Expand Up @@ -375,8 +378,8 @@ export class TelegramAPI {

/**
* Use this method to forward messages of any kind. Service messages can't be forwarded. On success, the sent Message is returned.
* @param chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param from_chat_id Unique identifier for the chat where the original message was sent (or channel username in the format @channelusername)
* @param chat_id Unique identifier for the target chat or username of the target channel.
* @param from_chat_id Unique identifier for the chat where the original message was sent or channel username.
* @param message_id Message identifier in the chat specified in from_chat_id
* @param options forwardMessageOptions
* @returns IMessage
Expand Down Expand Up @@ -419,7 +422,7 @@ export class TelegramAPI {

/**
* Use this method to copy messages of any kind. Service messages and invoice messages can't be copied. The method is analogous to the method forwardMessage, but the copied message doesn't have a link to the original message. Returns the MessageId of the sent message on success.
* @param chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param chat_id Unique identifier for the target chat or username of the target channel.
* @param from_chat_id Unique identifier for the chat where the original message was sent (or channel username in the format @channelusername)
* @param message_id Message identifier in the chat specified in from_chat_id
* @param options copyMessageOptions
Expand Down Expand Up @@ -462,7 +465,7 @@ export class TelegramAPI {

/**
* Use this method to send photos. On success, the sent Message is returned.
* @param chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param chat_id Unique identifier for the target chat or username of the target channel.
* @param photo Photo to send. Pass a file_id as String to send a photo that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a photo from the Internet, or upload a new photo using multipart/form-data. The photo must be at most 10 MB in size. The photo's width and height must not exceed 10000 in total. Width and height ratio must be at most 20. More info on Sending Files »
* @param options sendPhotoOptions
* @returns IMessage
Expand Down Expand Up @@ -518,7 +521,7 @@ export class TelegramAPI {

/**
* Use this method to send audio files, if you want Telegram clients to display them in the music player. Your audio must be in the .MP3 or .M4A format. On success, the sent Message is returned. Bots can currently send audio files of up to 50 MB in size
* @param chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param chat_id Unique identifier for the target chat or username of the target channel.
* @param audio Audio file to send. Pass a file_id as String to send an audio file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get an audio file from the Internet, or upload a new one using multipart/form-data.
* @param options sendAudioOptions
* @returns IMessage
Expand Down Expand Up @@ -574,7 +577,7 @@ export class TelegramAPI {

/**
* Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document). On success, the sent Message is returned. Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future.
* @param chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param chat_id Unique identifier for the target chat or username of the target channel.
* @param video Video to send. Pass a file_id as String to send a video that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a video from the Internet, or upload a new video using multipart/form-data.
* @param options sendVideoOptions
* @returns
Expand Down Expand Up @@ -631,7 +634,7 @@ export class TelegramAPI {

/**
* Use this method to send general files. On success, the sent Message is returned. Bots can currently send files of any type of up to 50 MB in size.
* @param chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param chat_id Unique identifier for the target chat or username of the target channel.
* @param document File to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data.
* @param options sendDocumentOptions
* @returns IMessage
Expand Down Expand Up @@ -686,6 +689,118 @@ export class TelegramAPI {
return send;
}

/**
* Use this method to send animation files (GIF or H.264/MPEG-4 AVC video without sound). On success, the sent Message is returned. Bots can currently send animation files of up to 50 MB in size.
* @param chat_id Unique identifier for the target chat or username of the target channel.
* @param animation Animation to send. Pass a file_id as String to send an animation that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get an animation from the Internet, or upload a new animation using multipart/form-data.
* @param options sendAnimationOptions
*/

async sendAnimation(
chat_id: string | number,
animation: Buffer | string,
options?: sendAnimationOptions
) {
let params = {};
let postOptions = {};
let qs;

if (this.isReadableStream(animation)) {
params = {
chat_id: chat_id,
animation: animation,
...options,
};
qs = this.qs(params);
postOptions = {
body: qs,
method: "POST",
headers: { "Content-Type": "multipart/form-data" },
};
} else {
if (options) {
params = {
chat_id: chat_id,
animation: animation,
...options,
};
} else {
params = {
chat_id: chat_id,
animation: animation,
};
}
qs = this.qs(params);
postOptions = {
body: qs,
method: "POST",
};
}

const send: IMessage = await this.sendRequest(
this.endpoint + "sendAnimation",
postOptions
);

return send;
}

/**
* Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .OGG file encoded with OPUS (other formats may be sent as Audio or Document). On success, the sent Message is returned. Bots can currently send voice messages of up to 50 MB in size.
* @param chat_id Unique identifier for the target chat or username of the target channel.
* @param voice Audio file to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data.
* @param options
* @returns
*/
async sendVoice(
chat_id: string | number,
voice: Buffer | string,
options?: sendVoiceOptions
) {
let params = {};
let postOptions = {};
let qs;

if (this.isReadableStream(voice)) {
params = {
chat_id: chat_id,
voice: voice,
...options,
};
qs = this.qs(params);
postOptions = {
body: qs,
method: "POST",
headers: { "Content-Type": "multipart/form-data" },
};
} else {
if (options) {
params = {
chat_id: chat_id,
voice: voice,
...options,
};
} else {
params = {
chat_id: chat_id,
voice: voice,
};
}
qs = this.qs(params);
postOptions = {
body: qs,
method: "POST",
};
}

const send: IMessage = await this.sendRequest(
this.endpoint + "sendVoice",
postOptions
);

return send;
}

private isReadableStream = (val: any) =>
val !== null &&
typeof val === "object" &&
Expand Down
2 changes: 2 additions & 0 deletions src/structure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ export {
sendAudioOptions,
sendVideoOptions,
sendDocumentOptions,
sendAnimationOptions,
sendVoiceOptions,
} from "./methodsOptions";
Loading

0 comments on commit 7fcc381

Please sign in to comment.