Skip to content

Commit

Permalink
Added sendContact, sendDice, sendChatAction, `getUserProfilePho…
Browse files Browse the repository at this point in the history
…tos` and `getFile` methoods.
  • Loading branch information
naseif committed Mar 8, 2022
1 parent 7a791c1 commit 04b42d7
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 18 deletions.
165 changes: 147 additions & 18 deletions src/structure/TelegramAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { IUpdateOptions } from "../types/IUpdateOptions";
import { IUser } from "../types/IUser";
import {
editMessageLiveLocationOptions,
getUserProfilePhotosOptions,
sendContactOptions,
sendDiceOptions,
sendLocationOptions,
sendMediaGroupOptions,
sendVenueOptions,
Expand Down Expand Up @@ -38,12 +41,15 @@ import {
TOnError,
IMessageId,
LocalFile,
ActionType,
} from "./types";
import {
IFile,
IInputMediaAudio,
IInputMediaDocument,
IInputMediaPhoto,
IInputMediaVideo,
IUserProfilePhotos,
} from "../types";

export class TelegramAPI {
Expand Down Expand Up @@ -259,7 +265,11 @@ export class TelegramAPI {
async getUpdates(options?: IUpdateOptions) {
if (!options) {
options = {};
return this.sendRequest("get", this.endpoint + "getUpdates", options);
return await this.sendRequest(
"get",
this.endpoint + "getUpdates",
options
);
}

const qs = new URLSearchParams();
Expand Down Expand Up @@ -306,7 +316,7 @@ export class TelegramAPI {
* @returns IUser
*/
async getMe(): Promise<IUser> {
const fetch: IUser = (
const fetch: IUser = await (
await this.sendRequest("get", this.endpoint + "getMe", {})
).result;
return fetch;
Expand Down Expand Up @@ -338,7 +348,7 @@ export class TelegramAPI {
...options,
};

const send: IMessage = (
const send: IMessage = await (
await this.sendRequest("post", this.endpoint + "sendMessage", params)
).result;

Expand Down Expand Up @@ -376,7 +386,7 @@ export class TelegramAPI {
...options,
};

const send: IMessage = (
const send: IMessage = await (
await this.sendRequest("post", this.endpoint + "sendPoll", params)
).result;

Expand Down Expand Up @@ -410,7 +420,7 @@ export class TelegramAPI {
...options,
};

const send: IMessage = (
const send: IMessage = await (
await this.sendRequest("post", this.endpoint + "forwardMessage", params)
).result;

Expand Down Expand Up @@ -445,7 +455,7 @@ export class TelegramAPI {
...options,
};

const send: IMessageId = (
const send: IMessageId = await (
await this.sendRequest("post", this.endpoint + "copyMessage", params)
).result;

Expand Down Expand Up @@ -499,7 +509,7 @@ export class TelegramAPI {

let result: IMessage;

result = (
result = await (
await this.sendRequest("post", this.endpoint + "sendPhoto", params, {
multipart: true,
})
Expand Down Expand Up @@ -553,7 +563,7 @@ export class TelegramAPI {
}

let result: IMessage;
result = (
result = await (
await this.sendRequest("post", this.endpoint + "sendAudio", params, {
multipart: true,
})
Expand Down Expand Up @@ -609,7 +619,7 @@ export class TelegramAPI {

let result: IMessage;

result = (
result = await (
await this.sendRequest("post", this.endpoint + "sendVideo", params, {
multipart: true,
})
Expand Down Expand Up @@ -665,7 +675,7 @@ export class TelegramAPI {

let result: IMessage;

result = (
result = await (
await this.sendRequest("post", this.endpoint + "sendDocument", params, {
multipart: true,
})
Expand Down Expand Up @@ -721,7 +731,7 @@ export class TelegramAPI {

let result: IMessage;

result = (
result = await (
await this.sendRequest("post", this.endpoint + "sendAnimation", params, {
multipart: true,
})
Expand Down Expand Up @@ -777,7 +787,7 @@ export class TelegramAPI {

let result: IMessage;

result = (
result = await (
await this.sendRequest("post", this.endpoint + "sendVoice", params, {
multipart: true,
})
Expand Down Expand Up @@ -830,7 +840,7 @@ export class TelegramAPI {

let result: IMessage;

result = (
result = await (
await this.sendRequest("post", this.endpoint + "sendVideoNote", params, {
multipart: true,
})
Expand Down Expand Up @@ -881,7 +891,7 @@ export class TelegramAPI {
...options,
};

const send: IMessage = (
const send: IMessage = await (
await this.sendRequest("post", this.endpoint + "sendMediaGroup", params, {
multipart: true,
})
Expand Down Expand Up @@ -916,7 +926,7 @@ export class TelegramAPI {
...options,
};

const send: IMessage = (
const send: IMessage = await (
await this.sendRequest("post", this.endpoint + "sendLocation", params)
).result;

Expand All @@ -942,7 +952,7 @@ export class TelegramAPI {
...options,
};

const send: IMessage = (
const send: IMessage = await (
await this.sendRequest(
"post",
this.endpoint + "editMessageLiveLocation",
Expand All @@ -966,7 +976,7 @@ export class TelegramAPI {
...options,
};

const send: IMessage = (
const send: IMessage = await (
await this.sendRequest(
"post",
this.endpoint + "stopMessageLiveLocation",
Expand Down Expand Up @@ -1008,10 +1018,129 @@ export class TelegramAPI {
...options,
};

const send: IMessage = (
const send: IMessage = await (
await this.sendRequest("post", this.endpoint + "sendVenue", params)
).result;

return send;
}

/**
* Use this method to send phone contacts. On success, the sent Message is returned.
* @param chat_id Unique identifier for the target chat or username of the target channel
* @param phone_number Contact's phone number
* @param first_name Contact's first name
* @param options sendContactOptions
* @returns IMessage
* ```ts
* // this will send a phone contact in the specified chat.
* await TelegramAPI.sendContact(message.chat.id, "3216513215", "Bob")
* ```
*/

async sendContact(
chat_id: string | number,
phone_number: string,
first_name: string,
options?: sendContactOptions
): Promise<IMessage> {
let params = {
chat_id: chat_id,
phone_number: phone_number,
first_name: first_name,
...options,
};

const send: IMessage = await (
await this.sendRequest("post", this.endpoint + "sendContact", params)
).result;

return send;
}

/**
* Use this method to send an animated emoji that will display a random value. On success, the sent Message is returned.
* @param chat_id Unique identifier for the target chat or username of the target channel
* @param options sendDiceOptions
* @returns IMessage
*/

async sendDice(
chat_id: string | number,
options?: sendDiceOptions
): Promise<IMessage> {
let params = {
chat_id: chat_id,
...options,
};

const send: IMessage = await (
await this.sendRequest("post", this.endpoint + "sendDice", params)
).result;

return send;
}

/**
* Use this method when you need to tell the user that something is happening on the bot's side. The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing status). Returns True on success.
* @param chat_id Unique identifier for the target chat or username of the target channel
* @param action ActionType
* @returns boolean
*/

async sendChatAction(chat_id: number | string, action: ActionType) {
let params = {
chat_id: chat_id,
action: action,
};

const send: boolean = await (
await this.sendRequest("post", this.endpoint + "sendChatAction", params)
).result;

return send;
}

/**
* Use this method to get a list of profile pictures for a user. Returns a IUserProfilePhotos object.
* @param user_id Unique identifier of the target user
* @param options getUserProfilePhotosOptions
* @returns IUserProfilePhotos
*/

async getUserProfilePhotos(
user_id: number,
options?: getUserProfilePhotosOptions
): Promise<IUserProfilePhotos> {
let params = {
user_id: user_id,
...options,
};

const send: IUserProfilePhotos = await (
await this.sendRequest(
"post",
this.endpoint + "getUserProfilePhotos",
params
)
).result;

return send;
}

/**
* Use this method to get basic info about a file and prepare it for downloading. For the moment, bots can download files of up to 20MB in size. On success, a File object is returned. The file can then be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>, where <file_path> is taken from the response. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling getFile
* @param file_id File identifier to get info about
* @returns IFile
*/

async getFile(file_id: string): Promise<IFile> {
const send: IFile = await (
await this.sendRequest("post", this.endpoint + "getFile", {
file_id: file_id,
})
).result;

return send;
}
}
3 changes: 3 additions & 0 deletions src/structure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ export {
editMessageLiveLocationOptions,
stopMessageLiveLocationOptions,
sendVenueOptions,
sendContactOptions,
sendDiceOptions,
getUserProfilePhotosOptions,
} from "./methodsOptions";
29 changes: 29 additions & 0 deletions src/structure/methodsOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,32 @@ export interface sendVenueOptions extends OptionsBase {
*/
google_place_type?: string;
}
export interface sendContactOptions extends OptionsBase {
/**
* Contact's last name
*/
last_name?: string;
/**
* Additional data about the contact in the form of a vCard, 0-2048 bytes
* @see https://en.wikipedia.org/wiki/VCard
*/
vcard?: string;
}

export interface sendDiceOptions extends OptionsBase {
/**
* Emoji on which the dice throw animation is based. Currently, must be one of “🎲”, “🎯”, “🏀”, “⚽”, “🎳”, or “🎰”. Dice can have values 1-6 for “🎲”, “🎯” and “🎳”, values 1-5 for “🏀” and “⚽”, and values 1-64 for “🎰”. Defaults to “🎲”
*/
emoji?: string;
}

export interface getUserProfilePhotosOptions {
/**
* Sequential number of the first photo to be returned. By default, all photos are returned.
*/
offset?: number;
/**
* Limits the number of photos to be retrieved. Values between 1-100 are accepted. Defaults to 100.
*/
limit?: number;
}
13 changes: 13 additions & 0 deletions src/structure/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ export type TPollCallback = (poll: IPoll) => void;
export type TPollAnswerCallback = (poll_answer: IPollAnswer) => void;
export type TOnError = (error: any) => void;

export type ActionType =
| "typing"
| "upload_photo"
| "record_video"
| "upload_video"
| "record_voice"
| "upload_voice"
| "upload_document"
| "choose_sticker"
| "find_location"
| "record_video_note"
| "upload_video_note";

export interface LocalFile {
/**
* Path string to the local file
Expand Down
22 changes: 22 additions & 0 deletions src/types/IFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* This object represents a file ready to be downloaded. The file can be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling getFile.
*/

export interface IFile {
/**
* Identifier for this file, which can be used to download or reuse the file
*/
file_id: string;
/**
* Unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file.
*/
file_unique_id: string;
/**
* Optional. File size in bytes, if known
*/
file_size?: number;
/**
* Optional. File path. Use https://api.telegram.org/file/bot<token>/<file_path> to get the file.
*/
file_path?: string;
}
Loading

0 comments on commit 04b42d7

Please sign in to comment.