How to be always online in Telegram? -Python/Telethon

Previously, I have already developed bots for Telegram with Aiogram. But those bots worked exactly like telegram bots. I wanted to automate telegram actions on behalf of my user. It’s like I’m doing the action. To do this, I found and used the wonderful Telethon framework. It is essentially a telegram client written in Python.
The first client bot appeared in my head as a simulation of my continuous activity in the messenger. I am already an active telegram user and I do not think that this will affect my status much. The bot is useful for those who want to hide their regular visits by adding fake visits. At the same time, it remains possible to see someone else’s activity. If you completely hide your status, you also do not see about others

See also  Work Accounting System Python/Django

Before working with Telethon, you need get your API ID and hash secret:

  1. Login to Telegram dev website with your own account with the phone number of the developer account to use.
  2. Click under API Development tools.
  3. A Create new application window will appear. Fill in your application details. There is no need to enter any URL, and only the first two fields (App title and Short name) can currently be changed later.
  4. Click on Create application at the end. Remember that your API hash is secret and Telegram won’t let you revoke it. Don’t post it anywhere

Now let’s move on to programming. I will provide the source code, the environment setup is already known to you even if you are a beginner Python Dev.

from telethon import TelegramClient
import threading #Threading need for creating continuously loop
from threading import *
import asyncio
import random
from datetime import datetime
import os

logfilename = 'log.csv' #Logging events into this file

async def main():
    loop = asyncio.new_event_loop()
    api_id = 0000000 #Your API ID from dev site
    api_hash = '6587575ytytytytytytytyty6565657' #Your API HASH from dev site
    client = TelegramClient('evgdev', api_id, api_hash, loop=loop)
    async with client:
        me = await client.get_me()
        await client.send_message('me', 'Ass!') #Sending single message to Saved messages chat for wakeup activity
        async for message in client.iter_messages('me'): #checking for last message in 'me' chat
            timenow =
            with open(logfilename, 'a', encoding="utf-8") as file_object:
                file_object.write(f'"{timenow}"\n') # wtiting to log
            await client.delete_messages('me', # deleting message for preventing garbage collection
            break # we need only one FOR iteration

def runAll():
    global state
    threading.Timer(int(random.choice(range(120, 300))), runAll).start()


At first time, program will request authorization by phone number.

See also  How I Landed My Chief on a Separate View Rule in Django



Author: admin

Leave a Reply

Your email address will not be published. Required fields are marked *