如何在Telegram上获取联系人

6
我使用Node.js模块来创建Telegram机器人,我想通过Telegram API获取用户的联系方式。 Telegram API有两种类型:Bot API 和 Telegram API
我认为Bot API无法获取用户联系方式。在Telegram API中,有一个叫做contact.getContacts的方法,但是我不知道如何使用它。
我该如何在Telegram上获取联系方式?
2个回答

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***** }
    
    })
    

3

Bot API也可以获取联系人信息;我认为在这种情况下更容易。

您可以尝试使用request_contact的回复键盘。如果用户点击它,您将收到带有Contact字段的消息更新。


1
很好,再加一点。未经用户许可,您无法获取用户的联系方式。这就是为什么该方法被称为“request_contact”而不是“get_contact”的原因。 - Ahmad Maleki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接