0

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).

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 2
    Please do not state that you want to pay for the work, here it is not charged and we are not a payment service. Please read [ask] and review the [tour] – eyllanesc Jun 06 '21 at 03:45

2 Answers2

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.

Andrew Hulterstrom
  • 1,563
  • 4
  • 18
  • 2
    I didn't expect community to be so active and help me solve an issue. Thank you so much for your reply. Let me go over the information and try to execute. Have a great rest of the weekend! – Bogdan Dimitrov Jun 06 '21 at 09:37
0

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 

Iris D
  • 82
  • 1
  • 7
  • 1
    Yeah, another proof you guys know what you are doing. Thanks for clarification and going into the code provided above! Let me go over your both comments and try to execute and verify which one can be in more use. Thank you so much for your reply. Have a good day ahead buddy! – Bogdan Dimitrov Jun 06 '21 at 09:39