2023-10-09 05:59:48 +03:00
|
|
|
import { BaseTelegramClient } from '@mtcute/core'
|
|
|
|
|
2023-10-16 19:23:53 +03:00
|
|
|
import { InputPeerLike, User } from '../../types/index.js'
|
|
|
|
import { normalizeToInputUser } from '../../utils/peer-utils.js'
|
|
|
|
import { assertIsUpdatesGroup } from '../../utils/updates-utils.js'
|
|
|
|
import { resolvePeer } from '../users/resolve-peer.js'
|
2021-04-25 17:10:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an existing Telegram user as a contact
|
|
|
|
*/
|
|
|
|
export async function addContact(
|
2023-10-09 05:59:48 +03:00
|
|
|
client: BaseTelegramClient,
|
2021-04-25 17:10:37 +03:00
|
|
|
params: {
|
2023-10-07 00:22:08 +03:00
|
|
|
/** User ID, username or phone number */
|
|
|
|
userId: InputPeerLike
|
|
|
|
|
2021-04-25 17:10:37 +03:00
|
|
|
/**
|
|
|
|
* 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
|
2023-10-29 20:25:06 +03:00
|
|
|
* with the newly created contact
|
|
|
|
*
|
|
|
|
* @default false
|
2021-04-25 17:10:37 +03:00
|
|
|
*/
|
|
|
|
sharePhone?: boolean
|
2023-06-05 03:30:48 +03:00
|
|
|
},
|
2021-04-25 17:10:37 +03:00
|
|
|
): Promise<User> {
|
2023-10-07 00:22:08 +03:00
|
|
|
const { userId, firstName, lastName = '', phone = '', sharePhone = false } = params
|
2023-10-09 05:59:48 +03:00
|
|
|
const peer = normalizeToInputUser(await resolvePeer(client, userId), userId)
|
2021-04-25 17:10:37 +03:00
|
|
|
|
2023-10-09 05:59:48 +03:00
|
|
|
const res = await client.call({
|
2021-04-25 17:10:37 +03:00
|
|
|
_: 'contacts.addContact',
|
|
|
|
id: peer,
|
2023-10-07 00:22:08 +03:00
|
|
|
firstName,
|
|
|
|
lastName,
|
|
|
|
phone,
|
|
|
|
addPhonePrivacyException: 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
|
|
|
|
2023-10-09 05:59:48 +03:00
|
|
|
client.network.handleUpdate(res)
|
2021-04-25 17:10:37 +03:00
|
|
|
|
2023-10-09 05:59:48 +03:00
|
|
|
return new User(res.users[0])
|
2021-04-25 17:10:37 +03:00
|
|
|
}
|