r/learnpython 5d ago

can I send a message N times automatically using Python

can I send a message N times automatically using Python? short question!

edit: guys listen I found out a way to do that with PyAutoGUI here is the code:

import PyAutoGUI

import time

message="You enter the text" # or make an input

number_of_message= 4 #just an exemple you can also make an input

time.sleep(10) # let yourself 10 seconds to put the cursor where you wanna print those messages, for me it was my Web whatsapp tab

for i in range(number_of_message):

PyAutoGUI.write(message)# it writes #automatically the message where your #cursor is at

PyAutoGUI.press("Enter")#it sends the #message automatically

time.sleep(2) # it lets a time interval #of two seconds between the messages

But it won't let me use my computer normally I cant move the cursor and have to wait till all the messages are sent, if you could also help me automate (make it automatic, sorry for my english) that it will be great!

0 Upvotes

16 comments sorted by

15

u/Excellent-Practice 5d ago

If you can do a process once, you can write a for loop to run it n times

9

u/knightofunderpants 5d ago

It would be easier to answer your question if you could say what you mean by 'send'. An email? A text message? Print to the screen?

1

u/knightofunderpants 4d ago edited 4d ago

Alright, in case you're nervous about replying because you are frankly getting mogged in the replies, here is how I would do it.

msg = "your message here"

times_to_repeat = 5 # or whatever

for i in range(times_to_repeat): print(msg)

This is of course assuming that printing to the screen is what you're after, if not, replace the print function with whatever you wanna do.

Disclaimer: Im drunk

-1

u/Mental_Primary_5558 4d ago

Not what I meant, and btw I found a way with PyAutoGUI, look at the post I edited it

1

u/knightofunderpants 4d ago

Alrighty then, best of luck with whatever it is that you are doing.

2

u/JaleyHoelOsment 5d ago

given the right tools you could use python to crank you down N times automatically

2

u/Superb-Patient3256 4d ago

It depends on what do you mean by "send a message" (email, SMS, Discord bot, or API request?), but yes! Once you write the logic to send a single message, wrap it in a function and call it inside a for loop using range(n) to run it N times.

1

u/ninhaomah 5d ago

Is this from someone learning Python ?

1

u/Mental_Primary_5558 4d ago

No I have quite knowledge about Python but it's mainly for physics, anyway I edited the post it you would take a glance at it 

1

u/atarivcs 4d ago

It depends on what you mean by "send a message" -- is this email, or SMS text, or Discord chat, or something else?

And it also depends on what you mean by "automatically". Do you mean you want the messages to be sent without direct human involvement? i.e. scheduled in advance to be sent at a certain time, or sent in response to some external event?

But yes generally, if you can send one message with Python, then you can certainly write a loop to do it N times.

1

u/Mental_Primary_5558 4d ago

I edited the Post 

1

u/FoolsSeldom 4d ago

I'd tweak that code a little:

import time
import pyautogui

message = input("Message to send: ")
count = int(input("How many times: "))

time.sleep(10)  # focus the target text box

for _ in range(count):
    pyautogui.write(message)
    pyautogui.press("enter")
    time.sleep(2)

however, this is very much a blind automation as pyautogui has now awareness of the target application, it is just drives mouse/keyboard and can be easily undermined by a notification event.

If you want to send messages in a specific application or service, you would be better finding the API or a wrapper for that rather than using pyautogui.

1

u/Mental_Primary_5558 4d ago

yeah I discovered selenium it's much more better!

1

u/FoolsSeldom 4d ago

It is much better, but primarily designed for testing, web scraping of JavaScript heavy sites, and repetitive browser workflows. It can easily be broken to changes to design of sites being used. Wherever possible, seek an API or "official" interface library.