skip to Main Content

I’m using Telethon’s send_message function to send messages to various numbers.
Problem that I’m facing is I can send messages to numbers that are in my contact list, but when I’m sending message to unsaved number I’m getting an error "ValueError: Cannot find any entity corresponding to "+XXXXXXXXXXXX"

from telethon import TelegramClient

api_id = xxxxx
api_hash = 'xxxxx'
client = TelegramClient('anon', api_id, api_hash)

async def main():
    await client.send_message('+XYZXYZXYZXYZ', 'Hello, friend!')

with client:
    client.loop.run_until_complete(main())

3

Answers


  1. Chosen as BEST ANSWER

    None of the above answer works for me so after lot of effort I got the solution of the problem. It is not possible to send message to a telegram user without saving their number. To overcome this problem first we have to save the number by these lines of code:

    Note: You have to downgrade your telethon to 0.19 for this just run pip install telethon==0.19

    contact = InputPhoneContact(client_id=0, phone=guest_phone_number,
    first_name="first_name", last_name="last_name")
    result = client.invoke(ImportContactsRequest([contact]))
    

    Here is full working code:

    from telethon import TelegramClient
    from telethon.tl.functions.messages import AddChatUserRequest
    from telethon.tl.types import InputPhoneContact
    from telethon.tl.functions.contacts import ImportContactsRequest
    
    api_id = XXXXX
    api_hash = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
    phone_number = '+XXXXXXXXXXXX' #sender's phone number
    guest_phone_number='+XXXXXXXXXXXX' #recievers phone number
    
    client = TelegramClient('session_name',
                    api_id,
                    api_hash)
    
    assert client.connect()
    if not client.is_user_authorized():
        client.send_code_request(phone_number)
        me = client.sign_in(phone_number, input('Enter code: '))
    
    # ---------------------------------------
    # add user to contact
    contact = InputPhoneContact(client_id=0, phone=guest_phone_number, 
    first_name="user", last_name=" ")
    result = client.invoke(ImportContactsRequest([contact]))
    # ---------------------------------------
    # send message to reciever
    client.send_message(result.users[0], 'Hello, friend!')
    

  2. it’s the new telegram limit about get numbers of accounts

    privacy setting

    if it’s Nobody you can’t find it by number if you have not this in your contacts before

    Login or Signup to reply.
  3. First you need to make telethon aware of all the entities at least once:
    For this use:

    dialogs = await client.get_dialogs()
    

    After this get the entity represented by the mobile number using the command:

    entity = await client.get_entity(<mobile_num>)
    

    Now you can use the entity to send message

    await client.send_message(entity, 'Hello, friend!')
    

    For more info have a look at this : Telethon Entities

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search