How to get contacts on Telegram
Asked Answered
N

2

6

I use node.js module for Telegram bot. I'm trying to get the user's contact on telegram using telegram API. Telegram API has 2 types: Bot API and Telegram API.

I think Bot API can not get the user's contacts. In Telegram API there is the method contact.getContacts. But I don't know how to use it.

How can I get the contacts on Telegram?

Neutron answered 2/5, 2017 at 9:13 Comment(0)
C
5
  1. this code will give you the contact, user shares his/her contact with your bot , user types command '/special' will be prompted with button to allow bot to get contact and on agreeing in your node server you may log contact info remember to declare Markup ---->

    //declare Markup 
    const {Extra,Markup}= Telegraf;
    
    bot.command('special', (ctx) => {
    return ctx.reply('Special buttons keyboard', Extra.markup((markup) => {
    return markup.resize()
    .keyboard([
    markup.contactRequestButton('contact')
    ])
    }))
    })
    //listens for the click on contact button
    bot.on('contact', (ctx) => {
    console.log(ctx.update.message.contact);
    //logs { phone_number: '254*******',
    //first_name: 'nelsonBlack',
    //user_id: 73***** }
    
    })
    
Chaussure answered 27/10, 2017 at 21:7 Comment(0)
E
3

Bot API can get contact info too; I think it is easier in this case.

You can try reply keyboard with request_contact. If the user clicks it, you will receive message update with Contact field.

Ellaelladine answered 2/5, 2017 at 9:49 Comment(1)
Great and one extra point. You can't get a user's contact without his permission. that's why the method is called request_contact not get_contact.Cowen

© 2022 - 2024 — McMap. All rights reserved.