diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index fec87cb3..1e3a1034 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -15,6 +15,8 @@ import { signUp } from './methods/auth/sign-up' import { start } from './methods/auth/start' import { addChatMembers } from './methods/chats/add-chat-members' import { archiveChats } from './methods/chats/archive-chats' +import { createChannel } from './methods/chats/create-channel' +import { deleteChannel } from './methods/chats/delete-channel' import { getChatPreview } from './methods/chats/get-chat-preview' import { getChat } from './methods/chats/get-chat' import { getFullChat } from './methods/chats/get-full-chat' @@ -333,7 +335,8 @@ export class TelegramClient extends BaseTelegramClient { * * @param chatId ID of the chat or its username * @param users ID(s) of the users, their username(s) or phone(s). - * @param forwardCount(default: `100`) + * @param forwardCount + * (default: `100`) * Number of old messages to be forwarded (0-100). * Only applicable to legacy groups, ignored for supergroups and channels */ @@ -352,6 +355,34 @@ export class TelegramClient extends BaseTelegramClient { archiveChats(chats: MaybeArray): Promise { return archiveChats.apply(this, arguments) } + /** + * Create a new broadcast channel + * + * @param title Channel title + * @param description (default: `''`) Channel description + * @returns Newly created channel + */ + createChannel(title: string, description?: string): Promise { + return createChannel.apply(this, arguments) + } + + /** + * Delete a channel or a supergroup + * + * @param chatId Chat ID or username + */ + deleteChannel(chatId: InputPeerLike): Promise { + return deleteChannel.apply(this, arguments) + } + + /** + * Delete a channel or a supergroup + * + * @param chatId Chat ID or username + */ + deleteSupergroup(chatId: InputPeerLike): Promise { + return deleteChannel.apply(this, arguments) + } /** * Get preview information about a private chat. * diff --git a/packages/client/src/methods/chats/create-channel.ts b/packages/client/src/methods/chats/create-channel.ts new file mode 100644 index 00000000..43e7dc91 --- /dev/null +++ b/packages/client/src/methods/chats/create-channel.ts @@ -0,0 +1,33 @@ +import { TelegramClient } from '../../client' +import { Chat, MtCuteTypeAssertionError } from '../../types' + +/** + * Create a new broadcast channel + * + * @param title Channel title + * @param description Channel description + * @returns Newly created channel + * @internal + */ +export async function createChannel( + this: TelegramClient, + title: string, + description = '' +): Promise { + const res = await this.call({ + _: 'channels.createChannel', + title, + about: description, + broadcast: true, + }) + + if (!(res._ === 'updates' || res._ === 'updatesCombined')) { + throw new MtCuteTypeAssertionError( + 'createChannel (@ channels.createChannel)', + 'updates | updatesCombined', + res._ + ) + } + + return new Chat(this, res.chats[0]) +} diff --git a/packages/client/src/methods/chats/delete-channel.ts b/packages/client/src/methods/chats/delete-channel.ts new file mode 100644 index 00000000..bc96e773 --- /dev/null +++ b/packages/client/src/methods/chats/delete-channel.ts @@ -0,0 +1,20 @@ +import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types' +import { TelegramClient } from '../../client' +import { normalizeToInputChannel } from '../../utils/peer-utils' + +// @alias=deleteSupergroup +/** + * Delete a channel or a supergroup + * + * @param chatId Chat ID or username + * @internal + */ +export async function deleteChannel(this: TelegramClient, chatId: InputPeerLike): Promise { + const peer = normalizeToInputChannel(await this.resolvePeer(chatId)) + if (!peer) throw new MtCuteInvalidPeerTypeError(chatId, 'channel') + + await this.call({ + _: 'channels.deleteChannel', + channel: peer + }) +}