mtcute/packages/client/src/methods/contacts/add-contact.ts

56 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-04-25 17:10:37 +03:00
import { TelegramClient } from '../../client'
import { InputPeerLike, User } from '../../types'
2021-04-25 17:10:37 +03:00
import { normalizeToInputUser } from '../../utils/peer-utils'
import { assertIsUpdatesGroup } from '../../utils/updates-utils'
2021-04-25 17:10:37 +03:00
/**
* Add an existing Telegram user as a contact
*
* @param userId User ID, username or phone number
* @param params Contact details
* @internal
*/
export async function addContact(
this: TelegramClient,
userId: InputPeerLike,
params: {
/**
* 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
},
2021-04-25 17:10:37 +03:00
): Promise<User> {
const peer = normalizeToInputUser(await this.resolvePeer(userId), userId)
2021-04-25 17:10:37 +03:00
const res = await this.call({
_: 'contacts.addContact',
id: peer,
firstName: params.firstName,
lastName: params.lastName ?? '',
phone: params.phone ?? '',
addPhonePrivacyException: Boolean(params.sharePhone),
2021-04-25 17:10:37 +03:00
})
assertIsUpdatesGroup('contacts.addContact', res)
2021-04-25 17:10:37 +03:00
this._handleUpdate(res)
return new User(this, res.users[0])
2021-04-25 17:10:37 +03:00
}