close

How can I send a message to someone with telegram API using my own account

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How can I send a message to someone with telegram API using my own account in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How can I send a message to someone with telegram API using my own account?

  1. How can I send a message to someone with telegram API using my own account?

    You'll find instructions to use the library, and in the examples folder you can find a script to send a message.

  2. I send a message to someone with telegram API using my own account

    You'll find instructions to use the library, and in the examples folder you can find a script to send a message.

Method 1

Telegram has a thorough and documented public API.

Following some links from there, here is the summary of the relevant parts:

  • the API is not restricted to bots, they are just a (special) kind of users;
  • the API has methods called getMessages and sendMessage, that should be what you need;
  • to call the API, Telegram recommends to use the dedicated library TDLib available for multiple programming languages.
  • There are several examples available on GitHub

Among the examples, if you go the the Python part, they recommend:

If you use modern Python >= 3.6, take a look at python-telegram.

You’ll find instructions to use the library, and in the examples folder you can find a script to send a message.

I’ll copy it here for the sake of completeness:

import logging
import argparse

from utils import setup_logging
from telegram.client import Telegram

"""
Sends a message to a chat
Usage:
    python examples/send_message.py api_id api_hash phone chat_id text
"""


if __name__ == '__main__':
    setup_logging(level=logging.INFO)

    parser = argparse.ArgumentParser()
    parser.add_argument('api_id', help='API id')  # https://my.telegram.org/apps
    parser.add_argument('api_hash', help='API hash')
    parser.add_argument('phone', help='Phone')
    parser.add_argument('chat_id', help='Chat id', type=int)
    parser.add_argument('text', help='Message text')
    args = parser.parse_args()

    tg = Telegram(
        api_id=args.api_id,
        api_hash=args.api_hash,
        phone=args.phone,
        database_encryption_key='changeme1234',
    )
    # you must call login method before others
    tg.login()

    # if this is the first run, library needs to preload all chats
    # otherwise the message will not be sent
    result = tg.get_chats()

    # `tdlib` is asynchronous, so `python-telegram` always returns you an `AsyncResult` object.
    # You can wait for a result with the blocking `wait` method.
    result.wait()

    if result.error:
        print(f'get chats error: {result.error_info}')
    else:
        print(f'chats: {result.update}')

    result = tg.send_message(
        chat_id=args.chat_id,
        text=args.text,
    )

    result.wait()
    if result.error:
        print(f'send message error: {result.error_info}')
    else:
        print(f'message has been sent: {result.update}')

Of course you’ll need to explore the documentation to get what are all those variables / ids in your case, but it will get you started!

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read