mtcute/packages/client/src/methods/contacts/add-contact.ts
Alina Sireneva a03d73503a refactor: initial support for esm
also fixed all circular imports and added checking for them via dpdm
2023-10-11 08:42:37 +03:00

56 lines
1.4 KiB
TypeScript

import { BaseTelegramClient } from '@mtcute/core'
import { InputPeerLike, User } from '../../types'
import { normalizeToInputUser } from '../../utils/peer-utils'
import { assertIsUpdatesGroup } from '../../utils/updates-utils'
import { resolvePeer } from '../users/resolve-peer'
/**
* Add an existing Telegram user as a contact
*/
export async function addContact(
client: BaseTelegramClient,
params: {
/** User ID, username or phone number */
userId: InputPeerLike
/**
* First name of the contact
*/
firstName: string
/**
* Last name of the contact
*/
lastName?: string
/**
* Phone number of the contact, if available
*/
phone?: string
/**
* Whether to share your own phone number
* with the newly created contact (defaults to `false`)
*/
sharePhone?: boolean
},
): Promise<User> {
const { userId, firstName, lastName = '', phone = '', sharePhone = false } = params
const peer = normalizeToInputUser(await resolvePeer(client, userId), userId)
const res = await client.call({
_: 'contacts.addContact',
id: peer,
firstName,
lastName,
phone,
addPhonePrivacyException: sharePhone,
})
assertIsUpdatesGroup('contacts.addContact', res)
client.network.handleUpdate(res)
return new User(res.users[0])
}