2

I am making a fairly simple code to send WhatsApp messages through Python and I need to use line breaks.

For example, if the message is "Dear Student, Please send your report Thank you for your attention"

The message on WhatsApp should look like

Dear Student,

Please send your report

Thank you for your attention

Attempts with \ n have not worked. The text must be in a single message. Below the code that I have advanced, thanks in advance for the help.

# Necessary libraries 

import pyautogui as pg
import webbrowser as web
import time    

message = "Dear Student, \n Please send your report\n Thank you for your attention"

number = 'XX_XXXXXXXXX'

web.open("https://web.whatsapp.com/send?phone="+number+"&text="+message)

time.sleep(12) # wait for the page to load

pg.click(1335, 690) # click on the submit button location
4jano20
  • 25
  • 1
  • 5

2 Answers2

2

Use urlencodedtext for the message, because it is a http request, as so, your message would be

Dear%20Student%2C%0APlease%20send%20your%20report%0AThank%20you%20for%20your%20attention

Check this question first

2
  • I also had same issue,
  • In the WhatsApp web "\n" works as a key.
  • For breaking a line in WhatsApp web you have to use its short keys to break the line.
  • Short keys for line break in WhatsApp web are : SHIFT+ENTER
  • But if we use it with code then it will hold the 'SHIFT' key during the whole execution. So the text capitalization is going to be change.
  • For prevention from capitalization changes you have to send 'SHIFT' key once again to release the 'SHIFT' key. So the keys which are going to send by code will be : SHIFT+ENTER+SHIFT

You can use any suitable way which will work for you better.

# Necessary libraries 

import pyautogui as pg
import webbrowser as web
import time
from selenium.webdriver.common.keys import Keys   
#You have to install selenium for keys or can choose any other library.

message = "Dear Student," + (Keys.SHIFT)+(Keys.ENTER)+(Keys.SHIFT) + "Please send your report" + (Keys.SHIFT)+(Keys.ENTER)+(Keys.SHIFT) + "Thank you for your attention"

number = 'XX_XXXXXXXXX'

web.open("https://web.whatsapp.com/send?phone="+number+"&text="+message)

time.sleep(12) # wait for the page to load

pg.click(1335, 690) # click on the submit button location

Maybe following method will also work for you.

# Necessary libraries 

import pyautogui as pg
import webbrowser as web
import time    

message = """Dear Student,                \
Please send your report          \
Thank you for your attention"""

# In the above just use '\' which is used for concatenation.
# But you have to give extra spaces as I have mentioned.
# because '\' will add only 2 tab spaces in the message during writing.

number = 'XX_XXXXXXXXX'

web.open("https://web.whatsapp.com/send?phone="+number+"&text="+message)

time.sleep(12) # wait for the page to load

pg.click(1335, 690) # click on the submit button location

Update : Must use this following way

  • The above messages looks too complicated to type.
  • So you can use that in following simple way by assigning the keys in a variable to use simply

import pyautogui as pg
import webbrowser as web
import time
from selenium.webdriver.common.keys import Keys   
#You have to install selenium for keys or can choose any other library which be suitable for you.

br = (Keys.SHIFT)+(Keys.ENTER)+(Keys.SHIFT)
message = "Dear Student," + br + "Please send your report" + br + "Thank you for your attention"

number = 'XX_XXXXXXXXX'

web.open("https://web.whatsapp.com/send?phone="+number+"&text="+message)

time.sleep(12) # wait for the page to load

pg.click(1335, 690) # click on the submit button location


Or can try using f or .format string to make it simple.


import pyautogui as pg
import webbrowser as web
import time
from selenium.webdriver.common.keys import Keys   
#You have to install selenium for keys or can choose any other library which be suitable for you.

br = (Keys.SHIFT)+(Keys.ENTER)+(Keys.SHIFT)
message = f"Dear Student,{br}Please send your report{br}Thank you for your attention"
####################### Or #########################
# message = "Dear Student,{0}Please send your report{0}Thank you for your attention".format(br)

number = 'XX_XXXXXXXXX'

web.open("https://web.whatsapp.com/send?phone="+number+"&text="+message)

time.sleep(12) # wait for the page to load

pg.click(1335, 690) # click on the submit button location