Skip to content

Commit

Permalink
Fixed setMyCommands and deleteMyCommands methods
Browse files Browse the repository at this point in the history
  • Loading branch information
naseif committed Mar 9, 2022
1 parent 1d24dc3 commit ecb4ccf
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 7 deletions.
53 changes: 47 additions & 6 deletions src/structure/TelegramAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
sendVoiceOptions,
stopMessageLiveLocationOptions,
sendVideoNoteOptions,
setMyCommandsOptions,
} from "./index";
import {
TCallbackQueryCallback,
Expand Down Expand Up @@ -1171,11 +1172,26 @@ export class TelegramAPI {
* @returns boolean
*/

async setMyCommands(commands: IBotCommand[]): Promise<boolean> {
const send: boolean = await (
await this.sendRequest("post", this.endpoint + "setMyCommands", {
async setMyCommands(
commands: IBotCommand[],
options?: setMyCommandsOptions
): Promise<boolean> {
let params = {};

if (options && options.scope) {
params = {
commands: JSON.stringify(commands),
})
scope: JSON.stringify(options.scope),
language_code: options.language_code ? options.language_code : "en",
};
} else {
params = {
commands: JSON.stringify(commands),
};
}

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

return send;
Expand All @@ -1185,9 +1201,34 @@ export class TelegramAPI {
* Use this method to delete the list of the bot's commands for the given scope and user language. After deletion, higher level commands will be shown to affected users. Returns True on success.
* @returns boolean
*/
async deleteMyCommands() {
async deleteMyCommands(options?: setMyCommandsOptions) {
let params = {};

switch (options) {
case options?.scope:
params = {
scope: JSON.stringify(options?.scope),
};
break;

case options?.language_code:
params = {
language_code: options?.language_code,
};
break;

case options?.language_code && options.scope:
params = {
scope: JSON.stringify(options?.scope),
language_code: options?.language_code,
};
break;
default:
params = {};
}

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

return send;
Expand Down
1 change: 1 addition & 0 deletions src/structure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export {
sendContactOptions,
sendDiceOptions,
getUserProfilePhotosOptions,
setMyCommandsOptions,
} from "./methodsOptions";
23 changes: 22 additions & 1 deletion src/structure/methodsOptions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import {
IBotCommandScopeBase,
IBotCommandScopeChat,
IBotCommandScopeChatAdministrators,
IBotCommandScopeChatMember,
} from "../types";
import {
IInlineKeyboardMarkup,
IMessageEntity,
Expand Down Expand Up @@ -419,4 +425,19 @@ export interface getUserProfilePhotosOptions {
limit?: number;
}

export interface setMyCommandsOptions {}
export interface setMyCommandsOptions {
/**
* A JSON-serialized object, describing scope of users for which the commands are relevant. Defaults to BotCommandScopeDefault.
*/
scope?:
| IBotCommandScopeBase
| IBotCommandScopeChat
| IBotCommandScopeChatAdministrators
| IBotCommandScopeChatMember;

/**
* A two-letter ISO 639-1 language code. If empty, commands will be applied to all users from the given scope, for whose language there are no dedicated commands
*/
language_code?: string;
}
38 changes: 38 additions & 0 deletions src/types/IBotCommandScope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export interface IBotCommandScopeBase {
/**
* Scope type
*/
type:
| "default"
| "all_private_chats"
| "all_group_chats"
| "all_chat_administrators"
| "chat"
| "chat_administrators";
}

export interface IBotCommandScopeChat extends IBotCommandScopeBase {
/**
* Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername)
*/
chat_id: string | number;
}

export interface IBotCommandScopeChatAdministrators
extends IBotCommandScopeBase {
/**
* Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername)
*/
chat_id: string | number;
}

export interface IBotCommandScopeChatMember extends IBotCommandScopeBase {
/**
* Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername)
*/
chat_id: string | number;
/**
* Unique identifier of the target user
*/
user_id: number;
}
6 changes: 6 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,9 @@ export { IInputMediaVideo } from "./IInputMediaVideo";
export { IUserProfilePhotos } from "./IUserProfilePhotos";
export { IFile } from "./IFile";
export { IBotCommand } from "./IBotCommand";
export {
IBotCommandScopeBase,
IBotCommandScopeChat,
IBotCommandScopeChatAdministrators,
IBotCommandScopeChatMember,
} from "./IBotCommandScope";

0 comments on commit ecb4ccf

Please sign in to comment.