Skip to content

Commit

Permalink
feat(API): Added the following methods:
Browse files Browse the repository at this point in the history
- unpinAllChatMessages
- leaveChat
- getChat
- getChatAdministrators
- getChatMemberCount
- getChatMember
  • Loading branch information
naseif committed Mar 13, 2022
1 parent 55f4d88 commit a15455c
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 1 deletion.
135 changes: 134 additions & 1 deletion src/structure/TelegramAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import EventEmitter from "eventemitter3";
import needle, { NeedleHttpVerbs, NeedleOptions } from "needle";
import mime from "mime-types";
import { URLSearchParams } from "node:url";
import { IChatInviteLink, IChatPermissions, IMessage, IUpdate, IUpdateOptions, IUser } from "../types";
import { IChat, IChatInviteLink, IChatMember, IChatPermissions, IMessage, IUpdate, IUpdateOptions, IUser } from "../types";
import {
sendMessageOptions,
sendPollOptions,
Expand Down Expand Up @@ -1779,5 +1779,138 @@ export class TelegramAPI {
return send;
}

/**
* Use this method to clear the list of pinned messages in a chat. If the chat is not a private chat, the bot must be an administrator in the chat for this to work and must have the 'can_pin_messages' administrator right in a supergroup or 'can_edit_messages' administrator right in a channel. Returns True on success.
* @param chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @returns boolean
*/

async unpinAllChatMessages(chat_id: string | number) {
let params = {
chat_id: chat_id,
}

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

return send;
}

/**
* Use this method for your bot to leave a group, supergroup or channel. Returns True on success.
* @param chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @returns boolean
*/

async leaveChat(chat_id: string | number) {
let params = {
chat_id: chat_id,
}

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

return send;
}

/**
* Use this method to get up to date information about the chat (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.). Returns a Chat object on success.
* @param chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @returns IChat
*/

async getChat(chat_id: string | number) {
let params = {
chat_id: chat_id,
}

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

return send;
}

/**
* Use this method to get a list of administrators in a chat. On success, returns an Array of ChatMember objects that contains information about all chat administrators except other bots. If the chat is a group or a supergroup and no administrators were appointed, only the creator will be returned.
* @param chat_id Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername)
* @returns IChatMember[]
*/

async getChatAdministrators(chat_id: string | number) {
let params = {
chat_id: chat_id,
}

const send: IChatMember[] = await (
await this.sendRequest(
"post",
this.endpoint + "getChatAdministrators",
params
)
).result;

return send;
}

/**
* Use this method to get the number of members in a chat. Returns Int on success.
* @param chat_id Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername)
* @returns number
*/

async getChatMemberCount(chat_id: string | number) {
let params = {
chat_id: chat_id,
}

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

return send;
}

/**
* Use this method to get information about a member of a chat. Returns a ChatMember object on success.
* @param chat_id Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername)
* @param user_id Unique identifier of the target user
* @returns IChatMember
*/

async getChatMember(chat_id: string | number, user_id: number) {
let params = {
chat_id: chat_id,
user_id: user_id,
}

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

return send;
}

}
10 changes: 10 additions & 0 deletions src/types/IChatMember.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IChatMemberAdministrator } from "./IChatMemberAdministrator";
import { IChatMemberBanned } from "./IChatMemberBanned";
import { IChatMemberLeft } from "./IChatMemberLeft";
import { IChatMemberMember } from "./IChatMemberMember";
import { IChatMemberOwner } from "./IChatMemberOwner";
import { IChatMemberRestricted } from "./IChatMemberRestricted";

export interface IChatMember {
member: IChatMemberAdministrator | IChatMemberBanned | IChatMemberLeft | IChatMemberMember | IChatMemberOwner | IChatMemberRestricted
}
67 changes: 67 additions & 0 deletions src/types/IChatMemberAdministrator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { IUser } from ".";

/**
* Represents a chat member that has some additional privileges.
*/
export interface IChatMemberAdministrator {
/**
* The member's status in the chat, always "administrator"
*/
status: string;
/**
* Information about the user
*/
user: IUser;
/**
* True, if the bot is allowed to edit administrator privileges of that user
*/
can_be_edited: boolean;
/**
* True, if the user's presence in the chat is hidden
*/
is_anonymous: boolean;
/**
* True, if the administrator can access the chat event log, chat statistics, message statistics in channels, see channel members, see anonymous administrators in supergroups and ignore slow mode. Implied by any other administrator privilege
*/
can_manage_chat: boolean;
/**
* True, if the administrator can delete messages of other users
*/
can_delete_messages: boolean;
/**
* True, if the administrator can manage voice chats
*/
can_manage_voice_chats: boolean;
/**
* True, if the administrator can restrict, ban or unban chat members
*/
can_restrict_members: boolean;
/**
* True, if the administrator can add new administrators with a subset of their own privileges or demote administrators that he has promoted, directly or indirectly (promoted by administrators that were appointed by the user)
*/
can_promote_members: boolean;
/**
* True, if the user is allowed to change the chat title, photo and other settings
*/
can_change_info: boolean;
/**
* True, if the user is allowed to invite new users to the chat
*/
can_invite_users: boolean;
/**
* Optional. True, if the administrator can post in the channel; channels only
*/
can_post_messages: boolean;
/**
* Optional. True, if the administrator can edit messages of other users and can pin messages; channels only
*/
can_edit_messages: boolean;
/**
* Optional. True, if the user is allowed to pin messages; groups and supergroups only
*/
can_pin_messages: boolean;
/**
* Optional. Custom title for this user
*/
custom_title: string;
}
20 changes: 20 additions & 0 deletions src/types/IChatMemberBanned.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { IUser } from ".";

/**
* Represents a chat member that was banned in the chat and can't return to the chat or view chat messages.
*/

export interface IChatMemberBanned {
/**
* The member's status in the chat, always "kicked"
*/
status: string;
/**
* Information about the user
*/
user: IUser;
/**
* Date when restrictions will be lifted for this user; unix time. If 0, then the user is banned forever
*/
until_date: number
}
16 changes: 16 additions & 0 deletions src/types/IChatMemberLeft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IUser } from ".";

/**
* Represents a chat member that isn't currently a member of the chat, but may join it themselves.
*/

export interface IChatMemberLeft {
/**
* The member's status in the chat, always "left"
*/
status: string;
/**
* Information about the user
*/
user: IUser;
}
16 changes: 16 additions & 0 deletions src/types/IChatMemberMember.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IUser } from ".";

/**
* Represents a chat member that has no additional privileges or restrictions.
*/

export interface IChatMemberMember {
/**
* The member's status in the chat, always "member"
*/
status: string;
/**
* Information about the user
*/
user: IUser;
}
24 changes: 24 additions & 0 deletions src/types/IChatMemberOwner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { IUser } from ".";

/**
* Represents a chat member that owns the chat and has all administrator privileges.
*/

export interface IChatMemberOwner {
/**
* The member's status in the chat, always "creator"
*/
status: string;
/**
* Information about the user
*/
user: IUser;
/**
* True, if the user's presence in the chat is hidden
*/
is_anonymous: boolean;
/**
* Optional. Custom title for this user
*/
custom_title: string
}
56 changes: 56 additions & 0 deletions src/types/IChatMemberRestricted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { IUser } from ".";

/**
* Represents a chat member that is under certain restrictions in the chat. Supergroups only.
*/
export interface IChatMemberRestricted {
/**
* The member's status in the chat, always "restricted"
*/
status: string;
/**
* Information about the user
*/
user: IUser;
/**
* True, if the user is a member of the chat at the moment of the request
*/
is_member: boolean;
/**
* True, if the user is allowed to change the chat title, photo and other settings
*/
can_change_info: boolean;
/**
* True, if the user is allowed to invite new users to the chat
*/
can_invite_users: boolean;
/**
* True, if the user is allowed to pin messages
*/
can_pin_messages: boolean;
/**
* True, if the user is allowed to send text messages, contacts, locations and venues
*/
can_send_messages: boolean;
/**
* True, if the user is allowed to send audios, documents, photos, videos, video notes and voice notes
*/
can_send_media_messages: boolean;
/**
* True, if the user is allowed to send polls
*/
can_send_polls: boolean;
/**
* True, if the user is allowed to send animations, games, stickers and use inline bots
*/
can_send_other_messages: boolean;
/**
* True, if the user is allowed to add web page previews to their messages
*/
can_add_web_page_previews: boolean;
/**
* Date when restrictions will be lifted for this user; unix time. If 0, then the user is restricted forever
*/
until_date: number;

}
7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,10 @@ export {
IBotCommandScopeChatAdministrators,
IBotCommandScopeChatMember,
} from "./IBotCommandScope";
export { IChatMemberAdministrator } from "./IChatMemberAdministrator";
export { IChatMemberBanned } from "./IChatMemberBanned";
export { IChatMemberLeft } from "./IChatMemberLeft";
export { IChatMemberMember } from "./IChatMemberMember";
export { IChatMemberOwner } from "./IChatMemberOwner";
export { IChatMemberRestricted } from "./IChatMemberRestricted";
export { IChatMember } from "./IChatMember";

0 comments on commit a15455c

Please sign in to comment.