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

59 lines
1.5 KiB
TypeScript
Raw Normal View History

import { BaseTelegramClient } from '@mtcute/core'
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(
client: BaseTelegramClient,
2021-04-25 17:10:37 +03:00
params: {
/** 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
},
2021-04-25 17:10:37 +03:00
): Promise<User> {
const { userId, firstName, lastName = '', phone = '', sharePhone = false } = params
const peer = normalizeToInputUser(await resolvePeer(client, userId), userId)
2021-04-25 17:10:37 +03:00
const res = await client.call({
2021-04-25 17:10:37 +03:00
_: 'contacts.addContact',
id: peer,
firstName,
lastName,
phone,
addPhonePrivacyException: sharePhone,
2021-04-25 17:10:37 +03:00
})
assertIsUpdatesGroup('contacts.addContact', res)
2021-04-25 17:10:37 +03:00
client.network.handleUpdate(res)
2021-04-25 17:10:37 +03:00
return new User(res.users[0])
2021-04-25 17:10:37 +03:00
}