1

I have gone through many forums and API of telegram also. But could not get the proper answer.

Well here is my main question. I have list of users with their mobile numbers in my database and from all those, I want to send them messages on their telegram. So for that I will need their chat ID. How can I retrieve that by their MobileNumber? If I will have chat id, I am able to send message to them by using the following reference.

Telegram php example send message

I have visited the following link to get the chat id by manually from BOT by user itself. But I want automatic from script.

How to obtain Telegram chat_id for a specific user?

Thanks.

Community
  • 1
  • 1
  • 1
    Please, read [this (how to ask)](http://stackoverflow.com/help/how-to-ask) and [this (mcve)](http://stackoverflow.com/help/mcve) before asking, as those will help you get more and better answers from the community. – Bonatti Jun 03 '16 at 12:04
  • thanks for your response. I will go through of it. – Durvesh Parmar Jun 03 '16 at 12:35

3 Answers3

5

I'm not familiar with PHP. but there is a way to do this:

As you know you can send a contact in telegram bot using its phoneNumber and a firsName (Doesn't need to be the real first name of the contact who owns that number).

After sending the contact to a chatID(no matter what chatID you choose, can be your own personal chat ID), you can look for its UserID.

Now if the person exists in Telegram you will get a long number that stands for his/her UserID or chatID but if not the long will be 0.

In C# I used this piece of code to see whether a phone number exists in telegram or not and it worked very well.

string s = "+44....";    //the phone number
var req2 = await bot.MakeRequestAsync(new SendContact(update.Message.Chat.Id, s, "Name"));
if(req2.Contact.UserId == 0)
{
  Console.WriteLine("The contact does not exist in Telegram");
}else
{
  Console.WriteLine("The contact exists in telegram with UserID:{0}",req2.Contact.UserId.ToString());
}

I believe this can be done in PHP too.

Naser.Sadeghi
  • 1,341
  • 1
  • 12
  • 35
  • After doing so much research - I almost tried something similar to your answer. Thanks for your answer btw. – Durvesh Parmar Jan 27 '18 at 13:42
  • 3
    There is a limitation on the number of user_ids you can get with this method. You can only use "sendContact" 20 times in a day. After that any attempt to use sendContact to any chat_id, will result in "Error:429 Too Many Requests: retry after XXXX". But all other functionalities of the bot remain intact. This is not well documented. – Mehrdad Nazmdar Apr 02 '18 at 08:56
  • @MehrdadNazmdar I haven't tested that, but for this question it works because he just wanted to know the chat id of a mobile number. – Naser.Sadeghi Apr 06 '18 at 20:02
2

By Telegram bot you can send messages only to users that started chat with your bot. you can obtain their chat_id (not phone number) from received query.

So you can not send message to another people even if you have their phone numbers.

Alireza Zojaji
  • 802
  • 2
  • 13
  • 33
1

You can't use bots to find a user on Telegram using their phone number.

ariaby
  • 852
  • 7
  • 12