2021-04-25 17:10:37 +03:00
|
|
|
import { TelegramClient } from '../../client'
|
2022-06-30 16:32:56 +03:00
|
|
|
import { InputPeerLike, MtInvalidPeerTypeError, User } from '../../types'
|
2021-04-25 17:10:37 +03:00
|
|
|
import { normalizeToInputUser } from '../../utils/peer-utils'
|
2021-05-16 14:32:36 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
): Promise<User> {
|
2022-08-29 16:15:37 +03:00
|
|
|
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 ?? '',
|
2021-06-06 15:20:41 +03:00
|
|
|
addPhonePrivacyException: !!params.sharePhone,
|
2021-04-25 17:10:37 +03:00
|
|
|
})
|
|
|
|
|
2021-05-16 14:32:36 +03:00
|
|
|
assertIsUpdatesGroup('contacts.addContact', res)
|
2021-04-25 17:10:37 +03:00
|
|
|
|
|
|
|
this._handleUpdate(res)
|
|
|
|
|
2021-04-26 23:26:57 +03:00
|
|
|
return new User(this, res.users[0])
|
2021-04-25 17:10:37 +03:00
|
|
|
}
|