feat(client): support more service messages
This commit is contained in:
parent
977527b78a
commit
e28ec79110
2 changed files with 434 additions and 144 deletions
42
packages/client/src/types/calls/discard-reason.ts
Normal file
42
packages/client/src/types/calls/discard-reason.ts
Normal file
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
* Phone call discard reason. Can be:
|
||||
* - `missed`: The call was missed
|
||||
* - `disconnect`: The connection has interrupted
|
||||
* - `hangup`: The call was ended normally
|
||||
* - `busy`: The call was discarded because the user is in another call
|
||||
*/
|
||||
import { tl } from '@mtcute/tl'
|
||||
|
||||
export type CallDiscardReason = 'missed' | 'disconnect' | 'hangup' | 'busy'
|
||||
|
||||
/** @internal */
|
||||
export function _callDiscardReasonFromTl(
|
||||
raw: tl.TypePhoneCallDiscardReason
|
||||
): CallDiscardReason {
|
||||
switch (raw._) {
|
||||
case 'phoneCallDiscardReasonMissed':
|
||||
return 'missed'
|
||||
case 'phoneCallDiscardReasonDisconnect':
|
||||
return 'disconnect'
|
||||
case 'phoneCallDiscardReasonHangup':
|
||||
return 'hangup'
|
||||
case 'phoneCallDiscardReasonBusy':
|
||||
return 'busy'
|
||||
}
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function _callDiscardReasonToTl(
|
||||
r: CallDiscardReason
|
||||
): tl.TypePhoneCallDiscardReason {
|
||||
switch (r) {
|
||||
case 'missed':
|
||||
return { _: 'phoneCallDiscardReasonMissed' }
|
||||
case 'disconnect':
|
||||
return { _: 'phoneCallDiscardReasonDisconnect' }
|
||||
case 'hangup':
|
||||
return { _: 'phoneCallDiscardReasonHangup' }
|
||||
case 'busy':
|
||||
return { _: 'phoneCallDiscardReasonBusy' }
|
||||
}
|
||||
}
|
|
@ -29,6 +29,10 @@ import {
|
|||
WebPage,
|
||||
} from '../media'
|
||||
import { parseDocument } from '../media/document-utils'
|
||||
import {
|
||||
_callDiscardReasonFromTl,
|
||||
CallDiscardReason,
|
||||
} from '../calls/discard-reason'
|
||||
|
||||
/**
|
||||
* A message or a service message
|
||||
|
@ -152,6 +156,134 @@ export namespace Message {
|
|||
readonly inviter: number
|
||||
}
|
||||
|
||||
/** A payment was received from a user (bot) */
|
||||
export interface ActionPaymentReceived {
|
||||
readonly type: 'payment_received'
|
||||
|
||||
/** Three-letter ISO 4217 currency code */
|
||||
readonly currency: string
|
||||
|
||||
/**
|
||||
* Price of the product in the smallest units of the currency
|
||||
* (integer, not float/double). For example, for a price of
|
||||
* `US$ 1.45`, `amount = 145`
|
||||
*/
|
||||
readonly amount: tl.Long
|
||||
|
||||
/** Bot specified invoice payload */
|
||||
readonly payload: Buffer
|
||||
|
||||
/** Order information provided by the user */
|
||||
readonly info?: tl.TypePaymentRequestedInfo
|
||||
|
||||
/** ID of the shipping option chosen by the user */
|
||||
readonly shippingOptionId?: string
|
||||
|
||||
/** Payment provider ID */
|
||||
readonly charge?: tl.TypePaymentCharge
|
||||
}
|
||||
|
||||
/** A payment was sent to a user */
|
||||
export interface ActionPaymentSent {
|
||||
readonly type: 'payment_sent'
|
||||
|
||||
/** Three-letter ISO 4217 currency code */
|
||||
readonly currency: string
|
||||
|
||||
/**
|
||||
* Price of the product in the smallest units of the currency
|
||||
* (integer, not float/double). For example, for a price of
|
||||
* `US$ 1.45`, `amount = 145`
|
||||
*/
|
||||
readonly amount: tl.Long
|
||||
}
|
||||
|
||||
/** A phone call */
|
||||
export interface ActionCall {
|
||||
readonly type: 'call'
|
||||
|
||||
/** Call ID */
|
||||
readonly id: tl.Long
|
||||
|
||||
/** Whether this is a video call */
|
||||
readonly isVideo: boolean
|
||||
|
||||
/** Duration if the call in seconds (0 if not available) */
|
||||
readonly duration: number
|
||||
|
||||
/** Call discard reason, if available */
|
||||
readonly reason?: CallDiscardReason
|
||||
}
|
||||
|
||||
/** A screenshot was taken */
|
||||
export interface ActionScreenshotTaken {
|
||||
readonly type: 'screenshot_taken'
|
||||
}
|
||||
|
||||
/** User has authorized via the bot */
|
||||
export interface ActionBotAllowed {
|
||||
readonly type: 'bot_allowed'
|
||||
|
||||
/** Domain where the user has logged in */
|
||||
readonly domain: string
|
||||
}
|
||||
|
||||
/**
|
||||
* A user is in proximity of another user
|
||||
* (see [Proximity alerts]{https://core.telegram.org/api/live-location#proximity-alert})
|
||||
*/
|
||||
export interface ActionGeoProximity {
|
||||
readonly type: 'geo_proximity'
|
||||
|
||||
/** ID of the user who sent the geolocation with proximity alerts */
|
||||
readonly targetId: number
|
||||
|
||||
/** ID of the user who has approached `targetId` */
|
||||
readonly userId: number
|
||||
|
||||
/** Distance between them in meters */
|
||||
readonly distance: number
|
||||
}
|
||||
|
||||
/** Group call has started */
|
||||
export interface ActionGroupCallStarted {
|
||||
readonly type: 'group_call_started'
|
||||
|
||||
/** TL object representing the call */
|
||||
readonly call: tl.TypeInputGroupCall
|
||||
}
|
||||
|
||||
/** Group call has ended */
|
||||
export interface ActionGroupCallEnded {
|
||||
readonly type: 'group_call_ended'
|
||||
|
||||
/** TL object representing the call */
|
||||
readonly call: tl.TypeInputGroupCall
|
||||
|
||||
/** Duration of the call */
|
||||
readonly duration: number
|
||||
}
|
||||
|
||||
/** Group call has ended */
|
||||
export interface ActionGroupInvite {
|
||||
readonly type: 'group_call_invite'
|
||||
|
||||
/** TL object representing the call */
|
||||
readonly call: tl.TypeInputGroupCall
|
||||
|
||||
/** IDs of the users invited to the call */
|
||||
readonly userIds: number[]
|
||||
}
|
||||
|
||||
/** Messages TTL changed */
|
||||
export interface ActionSetTtl {
|
||||
readonly type: 'set_ttl'
|
||||
|
||||
/** New TTL period */
|
||||
readonly period: number
|
||||
}
|
||||
|
||||
/** Information about a forward */
|
||||
export interface MessageForwardInfo {
|
||||
/**
|
||||
* Date the original message was sent
|
||||
|
@ -193,6 +325,16 @@ export namespace Message {
|
|||
| ActionUserLeft
|
||||
| ActionUserRemoved
|
||||
| ActionUserJoinedLink
|
||||
| ActionPaymentReceived
|
||||
| ActionPaymentSent
|
||||
| ActionCall
|
||||
| ActionScreenshotTaken
|
||||
| ActionBotAllowed
|
||||
| ActionGeoProximity
|
||||
| ActionGroupCallStarted
|
||||
| ActionGroupCallEnded
|
||||
| ActionGroupInvite
|
||||
| ActionSetTtl
|
||||
| null
|
||||
|
||||
// todo: venue, poll, invoice, successful_payment,
|
||||
|
@ -250,7 +392,7 @@ export class Message {
|
|||
raw: tl.TypeMessage,
|
||||
users: Record<number, tl.TypeUser>,
|
||||
chats: Record<number, tl.TypeChat>,
|
||||
isScheduled = false,
|
||||
isScheduled = false
|
||||
) {
|
||||
this.client = client
|
||||
this._users = users
|
||||
|
@ -352,18 +494,15 @@ export class Message {
|
|||
// forwarded channel post
|
||||
this._sender = new Chat(
|
||||
this.client,
|
||||
this._chats[from.channelId],
|
||||
this._chats[from.channelId]
|
||||
)
|
||||
} else if (from._ === 'peerUser') {
|
||||
this._sender = new User(
|
||||
this.client,
|
||||
this._users[from.userId],
|
||||
)
|
||||
this._sender = new User(this.client, this._users[from.userId])
|
||||
} else
|
||||
throw new MtCuteTypeAssertionError(
|
||||
'Message#sender (@ raw.fromId)',
|
||||
'peerUser | peerChannel',
|
||||
from._,
|
||||
from._
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -383,7 +522,7 @@ export class Message {
|
|||
this.client,
|
||||
this.raw,
|
||||
this._users,
|
||||
this._chats,
|
||||
this._chats
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -420,18 +559,18 @@ export class Message {
|
|||
if (fwd.fromId._ === 'peerChannel') {
|
||||
sender = new Chat(
|
||||
this.client,
|
||||
this._chats[fwd.fromId.channelId],
|
||||
this._chats[fwd.fromId.channelId]
|
||||
)
|
||||
} else if (fwd.fromId._ === 'peerUser') {
|
||||
sender = new User(
|
||||
this.client,
|
||||
this._users[fwd.fromId.userId],
|
||||
this._users[fwd.fromId.userId]
|
||||
)
|
||||
} else
|
||||
throw new MtCuteTypeAssertionError(
|
||||
'Message#forward (@ raw.fwdFrom.fromId)',
|
||||
'peerUser | peerChannel',
|
||||
fwd.fromId._,
|
||||
fwd.fromId._
|
||||
)
|
||||
} else {
|
||||
this._forward = null
|
||||
|
@ -483,7 +622,7 @@ export class Message {
|
|||
} else {
|
||||
this._viaBot = new User(
|
||||
this.client,
|
||||
this._users[this.raw.viaBotId],
|
||||
this._users[this.raw.viaBotId]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -538,66 +677,82 @@ export class Message {
|
|||
const act = this.raw.action
|
||||
let action: Message.MessageAction
|
||||
|
||||
if (act._ === 'messageActionChatCreate') {
|
||||
switch (act._) {
|
||||
case 'messageActionChatCreate':
|
||||
action = {
|
||||
type: 'chat_created',
|
||||
title: act.title,
|
||||
users: act.users,
|
||||
}
|
||||
} else if (act._ === 'messageActionChannelCreate') {
|
||||
break
|
||||
case 'messageActionChannelCreate':
|
||||
action = {
|
||||
type: 'channel_created',
|
||||
title: act.title,
|
||||
}
|
||||
} else if (act._ === 'messageActionChatMigrateTo') {
|
||||
break
|
||||
case 'messageActionChatMigrateTo':
|
||||
action = {
|
||||
type: 'chat_migrate_to',
|
||||
id: act.channelId,
|
||||
}
|
||||
} else if (act._ === 'messageActionChannelMigrateFrom') {
|
||||
break
|
||||
case 'messageActionChannelMigrateFrom':
|
||||
action = {
|
||||
type: 'channel_migrate_from',
|
||||
id: act.chatId,
|
||||
title: act.title,
|
||||
}
|
||||
} else if (act._ === 'messageActionPinMessage') {
|
||||
break
|
||||
case 'messageActionPinMessage':
|
||||
action = {
|
||||
type: 'message_pinned',
|
||||
}
|
||||
} else if (act._ === 'messageActionHistoryClear') {
|
||||
break
|
||||
case 'messageActionHistoryClear':
|
||||
action = {
|
||||
type: 'history_cleared',
|
||||
}
|
||||
} else if (act._ === 'messageActionGameScore') {
|
||||
break
|
||||
case 'messageActionGameScore':
|
||||
action = {
|
||||
type: 'game_score',
|
||||
gameId: act.gameId,
|
||||
score: act.score,
|
||||
}
|
||||
} else if (act._ === 'messageActionContactSignUp') {
|
||||
break
|
||||
case 'messageActionContactSignUp':
|
||||
action = {
|
||||
type: 'contact_joined',
|
||||
}
|
||||
} else if (act._ === 'messageActionChatEditTitle') {
|
||||
break
|
||||
case 'messageActionChatEditTitle':
|
||||
action = {
|
||||
type: 'title_changed',
|
||||
title: act.title,
|
||||
}
|
||||
} else if (act._ === 'messageActionChatEditPhoto') {
|
||||
break
|
||||
case 'messageActionChatEditPhoto':
|
||||
action = {
|
||||
type: 'photo_changed',
|
||||
photo: new Photo(this.client, act.photo as tl.RawPhoto),
|
||||
photo: new Photo(
|
||||
this.client,
|
||||
act.photo as tl.RawPhoto
|
||||
),
|
||||
}
|
||||
} else if (act._ === 'messageActionChatDeletePhoto') {
|
||||
break
|
||||
case 'messageActionChatDeletePhoto':
|
||||
action = {
|
||||
type: 'photo_deleted',
|
||||
}
|
||||
} else if (act._ === 'messageActionChatAddUser') {
|
||||
break
|
||||
case 'messageActionChatAddUser':
|
||||
action = {
|
||||
type: 'users_added',
|
||||
users: act.users,
|
||||
}
|
||||
} else if (act._ === 'messageActionChatDeleteUser') {
|
||||
break
|
||||
case 'messageActionChatDeleteUser':
|
||||
if (
|
||||
this.raw.fromId?._ === 'peerUser' &&
|
||||
act.userId === this.raw.fromId.userId
|
||||
|
@ -611,13 +766,95 @@ export class Message {
|
|||
user: act.userId,
|
||||
}
|
||||
}
|
||||
} else if (act._ === 'messageActionChatJoinedByLink') {
|
||||
break
|
||||
case 'messageActionChatJoinedByLink':
|
||||
action = {
|
||||
type: 'user_joined_link',
|
||||
inviter: act.inviterId,
|
||||
}
|
||||
} else {
|
||||
break
|
||||
case 'messageActionPaymentSentMe':
|
||||
action = {
|
||||
type: 'payment_received',
|
||||
currency: act.currency,
|
||||
amount: act.totalAmount,
|
||||
payload: act.payload,
|
||||
info: act.info,
|
||||
shippingOptionId: act.shippingOptionId,
|
||||
charge: act.charge,
|
||||
}
|
||||
break
|
||||
case 'messageActionPaymentSent':
|
||||
action = {
|
||||
type: 'payment_sent',
|
||||
currency: act.currency,
|
||||
amount: act.totalAmount,
|
||||
}
|
||||
break
|
||||
case 'messageActionPhoneCall':
|
||||
action = {
|
||||
type: 'call',
|
||||
id: act.callId,
|
||||
isVideo: !!act.video,
|
||||
reason: act.reason
|
||||
? _callDiscardReasonFromTl(act.reason)
|
||||
: undefined,
|
||||
duration: act.duration ?? 0,
|
||||
}
|
||||
break
|
||||
case 'messageActionScreenshotTaken':
|
||||
action = {
|
||||
type: 'screenshot_taken'
|
||||
}
|
||||
break
|
||||
case 'messageActionBotAllowed':
|
||||
action = {
|
||||
type: 'bot_allowed',
|
||||
domain: act.domain
|
||||
}
|
||||
break
|
||||
case 'messageActionGeoProximityReached':
|
||||
if (act.fromId._ !== 'peerUser' || act.toId._ !== 'peerUser') {
|
||||
action = null
|
||||
} else {
|
||||
action = {
|
||||
type: 'geo_proximity',
|
||||
targetId: act.toId.userId,
|
||||
userId: act.fromId.userId,
|
||||
distance: act.distance
|
||||
}
|
||||
}
|
||||
break
|
||||
case 'messageActionGroupCall':
|
||||
if (act.duration) {
|
||||
action = {
|
||||
type: 'group_call_ended',
|
||||
call: act.call,
|
||||
duration: act.duration
|
||||
}
|
||||
} else {
|
||||
action = {
|
||||
type: 'group_call_started',
|
||||
call: act.call
|
||||
}
|
||||
}
|
||||
break
|
||||
case 'messageActionInviteToGroupCall':
|
||||
action = {
|
||||
type: 'group_call_invite',
|
||||
call: act.call,
|
||||
userIds: act.users
|
||||
}
|
||||
break
|
||||
case 'messageActionSetMessagesTTL':
|
||||
action = {
|
||||
type: 'set_ttl',
|
||||
period: act.period
|
||||
}
|
||||
break
|
||||
default:
|
||||
action = null
|
||||
break
|
||||
}
|
||||
|
||||
this._action = action
|
||||
|
@ -676,7 +913,12 @@ export class Message {
|
|||
} else if (m._ === 'messageMediaVenue') {
|
||||
media = new Venue(this.client, m)
|
||||
} else if (m._ === 'messageMediaPoll') {
|
||||
media = new Poll(this.client, m.poll, this._users, m.results)
|
||||
media = new Poll(
|
||||
this.client,
|
||||
m.poll,
|
||||
this._users,
|
||||
m.results
|
||||
)
|
||||
} else if (m._ === 'messageMediaInvoice') {
|
||||
media = new Invoice(this.client, m)
|
||||
} else {
|
||||
|
@ -751,7 +993,7 @@ export class Message {
|
|||
}
|
||||
|
||||
throw new MtCuteArgumentError(
|
||||
`Cannot generate message link for ${this.chat.type}`,
|
||||
`Cannot generate message link for ${this.chat.type}`
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -793,13 +1035,11 @@ export class Message {
|
|||
replyText(
|
||||
text: string,
|
||||
visible = false,
|
||||
params?: Parameters<TelegramClient['sendText']>[2],
|
||||
params?: Parameters<TelegramClient['sendText']>[2]
|
||||
): ReturnType<TelegramClient['sendText']> {
|
||||
if (visible) {
|
||||
return this.client.sendText(this.chat.inputPeer, text, {
|
||||
...(
|
||||
params || {}
|
||||
),
|
||||
...(params || {}),
|
||||
replyTo: this.id,
|
||||
})
|
||||
}
|
||||
|
@ -819,13 +1059,11 @@ export class Message {
|
|||
replyMedia(
|
||||
media: InputMediaLike,
|
||||
visible = false,
|
||||
params?: Parameters<TelegramClient['sendMedia']>[2],
|
||||
params?: Parameters<TelegramClient['sendMedia']>[2]
|
||||
): ReturnType<TelegramClient['sendMedia']> {
|
||||
if (visible) {
|
||||
return this.client.sendMedia(this.chat.inputPeer, media, {
|
||||
...(
|
||||
params || {}
|
||||
),
|
||||
...(params || {}),
|
||||
replyTo: this.id,
|
||||
})
|
||||
}
|
||||
|
@ -848,7 +1086,12 @@ export class Message {
|
|||
* @param bothSides Whether to pin for both sides (only for private chats)
|
||||
*/
|
||||
pin(notify = false, bothSides = false): Promise<void> {
|
||||
return this.client.pinMessage(this.chat.inputPeer, this.id, notify, bothSides)
|
||||
return this.client.pinMessage(
|
||||
this.chat.inputPeer,
|
||||
this.id,
|
||||
notify,
|
||||
bothSides
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -864,7 +1107,7 @@ export class Message {
|
|||
* @link TelegramClient.editMessage
|
||||
*/
|
||||
edit(
|
||||
params: Parameters<TelegramClient['editMessage']>[2],
|
||||
params: Parameters<TelegramClient['editMessage']>[2]
|
||||
): Promise<Message> {
|
||||
return this.client.editMessage(this.chat.inputPeer, this.id, params)
|
||||
}
|
||||
|
@ -881,13 +1124,11 @@ export class Message {
|
|||
*/
|
||||
editText(
|
||||
text: string,
|
||||
params?: Omit<Parameters<TelegramClient['editMessage']>[2], 'text'>,
|
||||
params?: Omit<Parameters<TelegramClient['editMessage']>[2], 'text'>
|
||||
): Promise<Message> {
|
||||
return this.edit({
|
||||
text,
|
||||
...(
|
||||
params || {}
|
||||
),
|
||||
...(params || {}),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -903,23 +1144,31 @@ export class Message {
|
|||
* @param toChatId Target chat ID
|
||||
* @param params Copy parameters
|
||||
*/
|
||||
sendCopy (toChatId: InputPeerLike, params: Parameters<TelegramClient['sendCopy']>[3]): Promise<Message> {
|
||||
sendCopy(
|
||||
toChatId: InputPeerLike,
|
||||
params: Parameters<TelegramClient['sendCopy']>[3]
|
||||
): Promise<Message> {
|
||||
if (!params) params = {}
|
||||
|
||||
if (this.raw._ === 'messageService') {
|
||||
throw new MtCuteArgumentError('Service messages can\'t be copied')
|
||||
throw new MtCuteArgumentError("Service messages can't be copied")
|
||||
}
|
||||
|
||||
if (this.media && !(
|
||||
this.media instanceof WebPage
|
||||
)) {
|
||||
return this.client.sendMedia(toChatId, {
|
||||
if (this.media && !(this.media instanceof WebPage)) {
|
||||
return this.client.sendMedia(
|
||||
toChatId,
|
||||
{
|
||||
type: 'auto',
|
||||
file: this.media.inputMedia,
|
||||
caption: params.caption ?? this.raw.message,
|
||||
// we shouldn't use original entities if the user wants custom text
|
||||
entities: params.entities ?? params.caption ? undefined : this.raw.entities,
|
||||
}, params)
|
||||
entities:
|
||||
params.entities ?? params.caption
|
||||
? undefined
|
||||
: this.raw.entities,
|
||||
},
|
||||
params
|
||||
)
|
||||
}
|
||||
|
||||
return this.client.sendText(toChatId, this.raw.message, params)
|
||||
|
@ -940,5 +1189,4 @@ export class Message {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
makeInspectable(Message, ['empty', 'isScheduled'], ['link'])
|
||||
|
|
Loading…
Reference in a new issue