skip to Main Content

hi i made a telegram auto-messaging bot using python but it only sends messages when i run it i need your help on how to loop it.

import sys
from telethon import TelegramClient
import time 
import datetime
starttime = time.time()

api_id = #api id
api_hash = '#api hash'

groups = ['#groups ']

failcount = 0;

while True:
    with TelegramClient('anon', api_id, api_hash) as client:
        for x in groups:
            try:
                client.loop.run_until_complete(client.send_message(x, '#message'))
            except:
                print(x, sys.exc_info()[0])
                failcount == 1
    print(datetime.datetime.now(), str(failcount/len(groups) * 100) + '%')
    time.sleep(10800 - ((time.time()- starttime) % 10800))  

3

Answers


  1. Well i dont know much about that but if the last print is the messsage you can always try putting it into the loop.

    Login or Signup to reply.
  2. that’s obvious lmao delete all this and read the docs.

    you start a file like this:

    import things you need

    client = TelegramClient("session name", api_id, api_hash)
    
    ////////// code
    
    
    client.run_until_disconnected()
    

    to send a message use

    @client.send_message('username or id', 'message')
    

    oh and failcount should be += 1 (failcount = failcount + 1)

    Login or Signup to reply.
  3. You actually only need to make login once. Here is an working example:

    # Telethon
    from telethon import TelegramClient, events, utils
    import asyncio
    import logging
    import sys
    logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s',
                        level=logging.WARNING)
    
    api_id = '#api id'
    api_hash = '#api hash'
    
    
    client = TelegramClient('my_app', api_id, api_hash)
    
    async def main():
        # Sending messages for myself
        i = 0;
        while True:
            await asyncio.sleep(1);
            await client.send_message("me", f'Message - {i}')
            i += 1;
           
    
    with client:
        client.loop.run_until_complete(main())
    
    

    The loop above will send messages forever, but, you can add if else to break the loop.

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