GramJS

⚠️ This project is archived and no longer maintained. Development continues in teleproto, an actively maintained fork. It's largely compatible with GramJS for most projects, migration means little more than swapping the package:
npm install teleproto
See the migration guide for details.

contacts.AddContact

Add an existing telegram user as contact.

Example

const { Api, TelegramClient } = require("telegram");
const { StringSession } = require("telegram/sessions");

const session = new StringSession(""); // You should put your string session here
const client = new TelegramClient(session, apiId, apiHash, {});

(async function run() {
  await client.connect(); // This assumes you have already authenticated with .start()

  const result = await client.invoke(
    new Api.contacts.AddContact({
      id: "username",
      firstName: "some string here",
      lastName: "some string here",
      phone: "some string here",
      addPhonePrivacyException: true,
    })
  );
  console.log(result); // prints the result
})();
import { Api, TelegramClient } from "telegram";
import { StringSession } from "telegram/sessions";

const session = new StringSession(""); // You should put your string session here
const client = new TelegramClient(session, apiId, apiHash, {});

(async function run() {
  await client.connect(); // This assumes you have already authenticated with .start()

  const result: Api.Updates = await client.invoke(
    new Api.contacts.AddContact({
      id: "username",
      firstName: "some string here",
      lastName: "some string here",
      phone: "some string here",
      addPhonePrivacyException: true,
    })
  );
  console.log(result); // prints the result
})();

Parameters

NameTypeDescription
flags#Flags, see TL conditional fields
addPhonePrivacyExceptionflags.0?trueAllow the other user to see our phone number?
idInputUserTelegram ID of the other user
firstNamestringFirst name
lastNamestringLast name
phonestringUser's phone number

Result

Updates

Possible errors

CodeTypeDescription
400CHANNEL_PRIVATEYou haven't joined this channel/supergroup.
400CONTACT_ID_INVALIDThe provided contact ID is invalid.
400CONTACT_NAME_EMPTYContact name empty.
400MSG_ID_INVALIDInvalid message ID provided.

Can bots use this method?

No

contacts.importContacts

Imports contacts: saves a full list on the server, adds already registered contacts to the contact list, returns added contacts and their info.