skip to Main Content

I am currently struggling with creating a spamming bot for several groups and the only way I found is the following:

import pyautogui
import time

msg = input("Enter the message: ")
n = input("How many times ?: ")

print ("t minus")

count = 5
while(count != 0):
    print(count)
    time.sleep(1)
    count -= 1

print("Fire in the hole!!!")

for i in range(0,int(n)):
    pyautogui.typewrite(msg)

But in this case its only allowing me to put a single line message. My complete desire is to read from a file, so I can prepare the file script to execute it (read from it).

But if this is not achievable can you advise me how to modify this code at least to allow me to add new line not send a single one (as from the case above).

2

Answers


  1. To get the variable msg to include a new line, you can type /n. More info here.

    You can go to a next line in Telegram using shift+enter. To get pyautogui to type this you can use:

    pyautogui.keyDown('shift')  # hold down the shift key
    pyautogui.press('enter')     # press the enter key
    pyautogui.keyUp('shift')    # release the shift key
    

    You will probably need to create a function that separates the msg into individual lines and then executes the above pyautogui code between each line.

    I will also point out though, that you may want to take a different approach than pyautogui. Telegram has an API that you can you to automatically send messages, and it would probably be much easier to use that.

    Login or Signup to reply.
  2. If you want to read text from a file and send that, you could do something like:

    file = 'file.txt'
    
    with open(file, 'r+'):
      
       content = f.readlines()
       
       msg = content 
    
       # execute from here 
    

    If you have to use the user input function, this answer suggests redirecting your standard input to a file and reading from a file, like so:

    input_here = sys.stdin 
    
    with open(file, 'r+') as f: 
    
        sys.stdin = f
        
        msg = input("Enter the message: ")
        
        sys.stdin = input_here
    

    You may not be able to use the context manager with input (the context manager handles opening and closing automatically, but you can’t keep working on a closed file), in which case it would look like:

    input_here = sys.stdin 
    
    f = open(file, 'r+') 
    
    msg = input("Enter the message: ")
    
    f.close()
    
    sys.stdin = input_here 
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search