Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

It's awesome how google something can be annoying when you can't find the right words. I found a million answers on how about to create a Telegram Bot to send and receive messages, and it's easy as write maybe five code lines.

But how about managing my own account? I want to know if it it's posible, using Python (telepot or other library), to retrieve my personal messages and send messages from my PERSONAL account, not using a bot.

If it's possible, where can I find more information about that

like image 246
Leonardo Rick Avatar asked Aug 05 '20 19:08

Leonardo Rick


1 Answers

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!

like image 157
Pac0 Avatar answered Oct 17 '22 04:10

Pac0