diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 05ca21c2..152fe074 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -46,6 +46,7 @@ import { getHistory } from './methods/messages/get-history' import { getMessages } from './methods/messages/get-messages' import { iterHistory } from './methods/messages/iter-history' import { _parseEntities } from './methods/messages/parse-entities' +import { pinMessage } from './methods/messages/pin-message' import { searchGlobal } from './methods/messages/search-global' import { searchMessages } from './methods/messages/search-messages' import { sendDice } from './methods/messages/send-dice' @@ -53,6 +54,7 @@ import { sendLocation } from './methods/messages/send-location' import { sendMedia } from './methods/messages/send-media' import { sendPhoto } from './methods/messages/send-photo' import { sendText } from './methods/messages/send-text' +import { unpinMessage } from './methods/messages/unpin-message' import { getParseMode, registerParseMode, @@ -947,6 +949,25 @@ export class TelegramClient extends BaseTelegramClient { ): Promise<[string, tl.TypeMessageEntity[] | undefined]> { return _parseEntities.apply(this, arguments) } + /** + * Pin a message in a group, supergroup, channel or PM. + * + * For supergroups/channels, you must have appropriate permissions, + * either as an admin, or as default permissions + * + * @param chatId Chat ID, username, phone number, `"self"` or `"me"` + * @param messageId Message ID + * @param notify (default: `false`) Whether to send a notification (only for legacy groups and supergroups) + * @param bothSides (default: `false`) Whether to pin for both sides (only for private chats) + */ + pinMessage( + chatId: InputPeerLike, + messageId: number, + notify?: boolean, + bothSides?: boolean + ): Promise { + return pinMessage.apply(this, arguments) + } /** * Search for messages globally from all of your chats * @@ -1309,6 +1330,18 @@ export class TelegramClient extends BaseTelegramClient { ): Promise { return sendText.apply(this, arguments) } + /** + * Unpin a message in a group, supergroup, channel or PM. + * + * For supergroups/channels, you must have appropriate permissions, + * either as an admin, or as default permissions + * + * @param chatId Chat ID, username, phone number, `"self"` or `"me"` + * @param messageId Message ID + */ + unpinMessage(chatId: InputPeerLike, messageId: number): Promise { + return unpinMessage.apply(this, arguments) + } /** * Register a given {@link IMessageEntityParser} as a parse mode * for messages. When this method is first called, given parse diff --git a/packages/client/src/methods/messages/pin-message.ts b/packages/client/src/methods/messages/pin-message.ts new file mode 100644 index 00000000..5141fdea --- /dev/null +++ b/packages/client/src/methods/messages/pin-message.ts @@ -0,0 +1,31 @@ +import { TelegramClient } from '../../client' +import { InputPeerLike } from '../../types' +import { normalizeToInputPeer } from '../../utils/peer-utils' + +/** + * Pin a message in a group, supergroup, channel or PM. + * + * For supergroups/channels, you must have appropriate permissions, + * either as an admin, or as default permissions + * + * @param chatId Chat ID, username, phone number, `"self"` or `"me"` + * @param messageId Message ID + * @param notify Whether to send a notification (only for legacy groups and supergroups) + * @param bothSides Whether to pin for both sides (only for private chats) + * @internal + */ +export async function pinMessage( + this: TelegramClient, + chatId: InputPeerLike, + messageId: number, + notify = false, + bothSides = false +): Promise { + await this.call({ + _: 'messages.updatePinnedMessage', + peer: normalizeToInputPeer(await this.resolvePeer(chatId)), + id: messageId, + silent: !notify, + pmOneside: !bothSides + }) +} diff --git a/packages/client/src/methods/messages/unpin-message.ts b/packages/client/src/methods/messages/unpin-message.ts new file mode 100644 index 00000000..867cf2c0 --- /dev/null +++ b/packages/client/src/methods/messages/unpin-message.ts @@ -0,0 +1,26 @@ +import { TelegramClient } from '../../client' +import { InputPeerLike } from '../../types' +import { normalizeToInputPeer } from '../../utils/peer-utils' + +/** + * Unpin a message in a group, supergroup, channel or PM. + * + * For supergroups/channels, you must have appropriate permissions, + * either as an admin, or as default permissions + * + * @param chatId Chat ID, username, phone number, `"self"` or `"me"` + * @param messageId Message ID + * @internal + */ +export async function unpinMessage( + this: TelegramClient, + chatId: InputPeerLike, + messageId: number, +): Promise { + await this.call({ + _: 'messages.updatePinnedMessage', + peer: normalizeToInputPeer(await this.resolvePeer(chatId)), + id: messageId, + unpin: true + }) +} diff --git a/packages/client/src/types/messages/message.ts b/packages/client/src/types/messages/message.ts index 0730f1fa..a94738b9 100644 --- a/packages/client/src/types/messages/message.ts +++ b/packages/client/src/types/messages/message.ts @@ -913,6 +913,23 @@ export class Message { return this.client.deleteMessages(this.chat.inputPeer, this.id, revoke) } + /** + * Pin this message. + * + * @param notify Whether to send a notification (only for legacy groups and supergroups) + * @param bothSides Whether to pin for both sides (only for private chats) + */ + pin(notify = false, bothSides = false): Promise { + return this.client.pinMessage(this.chat.inputPeer, this.id, notify, bothSides) + } + + /** + * Unpin this message. + */ + unpin(): Promise { + return this.client.pinMessage(this.chat.inputPeer, this.id) + } + /** * Edit this message's text and/or reply markup *