From 97cbfd48406aacbffc4a9426efdc8397803a66ce Mon Sep 17 00:00:00 2001 From: teidesu Date: Sat, 10 Apr 2021 15:23:27 +0300 Subject: [PATCH] feat(client): sendLocation method --- packages/client/src/client.ts | 44 ++++++++++- .../client/src/methods/messages/send-dice.ts | 5 +- .../src/methods/messages/send-location.ts | 73 +++++++++++++++++++ 3 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 packages/client/src/methods/messages/send-location.ts diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 9427bcfb..4022ed42 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -28,6 +28,7 @@ import { _parseEntities } from './methods/messages/parse-entities' import { searchGlobal } from './methods/messages/search-global' import { searchMessages } from './methods/messages/search-messages' import { sendDice } from './methods/messages/send-dice' +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' @@ -744,9 +745,12 @@ export class TelegramClient extends BaseTelegramClient { /** * Send an animated dice with a random value. * - * For convenience, supported dice emojis are available + * For convenience, known dice emojis are available * as static members of {@link Dice}. * + * Note that dice result value is generated randomly on the server, + * you can't influence it in any way! + * * @param chatId ID of the chat, its username, phone or `"me"` or `"self"` * @param emoji Emoji representing a dice * @param params Additional sending parameters @@ -781,6 +785,44 @@ export class TelegramClient extends BaseTelegramClient { ): Promise { return sendDice.apply(this, arguments) } + /** + * Send a static geo location. + * + * @param chatId ID of the chat, its username, phone or `"me"` or `"self"` + * @param latitude Latitude of the location + * @param longitude Longitude of the location + * @param params Additional sending parameters + */ + sendLocation( + chatId: InputPeerLike, + latitude: number, + longitude: number, + params?: { + /** + * Message to reply to. Either a message object or message ID. + */ + replyTo?: number | Message + + /** + * Whether to send this message silently. + */ + silent?: boolean + + /** + * If set, the message will be scheduled to this date. + * When passing a number, a UNIX time in ms is expected. + */ + schedule?: Date | number + + /** + * For bots: inline or reply markup or an instruction + * to hide a reply keyboard or to force a reply. + */ + replyMarkup?: ReplyMarkup + } + ): Promise { + return sendLocation.apply(this, arguments) + } /** * Send a single media. * diff --git a/packages/client/src/methods/messages/send-dice.ts b/packages/client/src/methods/messages/send-dice.ts index 189c9f23..7b87c04d 100644 --- a/packages/client/src/methods/messages/send-dice.ts +++ b/packages/client/src/methods/messages/send-dice.ts @@ -44,7 +44,6 @@ export async function sendDice( * to hide a reply keyboard or to force a reply. */ replyMarkup?: ReplyMarkup - } ): Promise { if (!params) params = {} @@ -57,7 +56,7 @@ export async function sendDice( peer, media: { _: 'inputMediaDice', - emoticon: emoji + emoticon: emoji, }, silent: params.silent, replyToMsgId: params.replyTo @@ -68,7 +67,7 @@ export async function sendDice( randomId: randomUlong(), scheduleDate: normalizeDate(params.schedule), replyMarkup, - message: '' + message: '', }) return this._findMessageInUpdate(res) diff --git a/packages/client/src/methods/messages/send-location.ts b/packages/client/src/methods/messages/send-location.ts new file mode 100644 index 00000000..35649613 --- /dev/null +++ b/packages/client/src/methods/messages/send-location.ts @@ -0,0 +1,73 @@ +import { BotKeyboard, InputPeerLike, Message, ReplyMarkup } from '../../types' +import { TelegramClient } from '../../client' +import { normalizeToInputPeer } from '../../utils/peer-utils' +import { normalizeDate, randomUlong } from '../../utils/misc-utils' + +/** + * Send a static geo location. + * + * @param chatId ID of the chat, its username, phone or `"me"` or `"self"` + * @param latitude Latitude of the location + * @param longitude Longitude of the location + * @param params Additional sending parameters + * @internal + */ +export async function sendLocation( + this: TelegramClient, + chatId: InputPeerLike, + latitude: number, + longitude: number, + params?: { + /** + * Message to reply to. Either a message object or message ID. + */ + replyTo?: number | Message + + /** + * Whether to send this message silently. + */ + silent?: boolean + + /** + * If set, the message will be scheduled to this date. + * When passing a number, a UNIX time in ms is expected. + */ + schedule?: Date | number + + /** + * For bots: inline or reply markup or an instruction + * to hide a reply keyboard or to force a reply. + */ + replyMarkup?: ReplyMarkup + } +): Promise { + if (!params) params = {} + + const peer = normalizeToInputPeer(await this.resolvePeer(chatId)) + const replyMarkup = BotKeyboard._convertToTl(params.replyMarkup) + + const res = await this.call({ + _: 'messages.sendMedia', + peer, + media: { + _: 'inputMediaGeoPoint', + geoPoint: { + _: 'inputGeoPoint', + lat: latitude, + long: longitude + } + }, + silent: params.silent, + replyToMsgId: params.replyTo + ? typeof params.replyTo === 'number' + ? params.replyTo + : params.replyTo.id + : undefined, + randomId: randomUlong(), + scheduleDate: normalizeDate(params.schedule), + replyMarkup, + message: '', + }) + + return this._findMessageInUpdate(res) +}