Search code examples
telegram-botphp-telegram-bot

how can i get userchat id in telegram bot using phone number in php


I need to get user id of a specific phonenumber group and for this i user SendContact Telegram bot api method to share my phone numbers with my own chat id , but when i get telegram server response i don't get any chat id however my phone numbers are registered in telegram but telegram server send no response that include chat id of that phone number , just server return the phonenumber that i shared with the name that i choose for sharing that. please help me thanks. you can see telegram response to my request in next lines :

stdClass Object
(
    [ok] => 1
    [result] => stdClass Object
        (
            [message_id] => 98
            [from] => stdClass Object
                (
                    [id] => 12334567
                    [is_bot] => 1
                    [first_name] => botname
                    [username] => botusername
                )

            [chat] => stdClass Object
                (
                    [id] => myid
                    [first_name] => myfirstname
                    [username] => myusername
                    [type] => private
                )

            [date] => 1532279079
            [contact] => stdClass Object
                (
                    [phone_number] => 9891911111111
                    [first_name] => qqqq
                )
        )
)

Solution

  • You can have a look at SendContact method

    For response you will get Message object, that contains field from and field contact.

    Both of them contains user_id/chat_id field.

    In your stdClass Object chat_id is in result.from.id