Reputation: 35
I am trying to create a program which on certain conditions will send whatsapp message as notification. I want to perform this task without any third-party registration. Is there a way I can perform this using any python module or framework ?
Upvotes: 1
Views: 6421
Reputation: 109
You could do it in the following way, I hope it helps.
import requests
import json
PHONE_ID = "<whatsapp-phone-id>"
TOKEN = "<whatsapp-token>"
NUMBER = "<number>"
MESSAGE = "<message>"
URL = "https://graph.facebook.com/v13.0/"+PHONE_ID+"/messages"
headers = {
"Authorization": "Bearer "+TOKEN,
"Content-Type": "application/json"
}
data = {
"messaging_product": "whatsapp",
"to": NUMBER,
"type": "text",
"text": json.dumps({ "preview_url": False, "body": MESSAGE})
}
response = requests.post(URL, headers=headers, data=data)
response_json = response.json()
print(response_json)
You can find more details in the following source https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#text-object
Upvotes: 1
Reputation: 1328
Create a new file called "wasend.py" and write the following code on it:
import webbrowser
import pyautogui
from time import sleep
def send(text, phone):
webbrowser.open("whatsapp://send?text=" + text.replace('\n', '%0A') + "&phone=" + phone.replace('+', ''))
sleep(10)
pyautogui.click(x=1787, y=978)
sleep(0.2)
pyautogui.hotkey('enter')
sleep(1)
pyautogui.hotkey('alt', "f4")
Then, execute the following commands:
$ pip install pyautogui
$ pip install webbrowser
Create another file called "send.py". On it, write the following code:
import wasend
wasend.send("message", "phone")
I built this program. These are the params of the wasend.send()
function:
wasend.send(message, number)
> message
The message in plain text. (Use * for bold, _ for italic, and plain Whatsapp formatting)
Write "\n" to make a line break.
> number
The phone number in the format: IIINNNNNNNNN (i = indicative, n = number, without the plus sign)
The program takes 11.5 seconds to execute (you can change the sleep to make it fast, but give some time to WhatsApp to load. If you already have WhatsApp loaded, change line 7 in wasend.py
to sleep(1)
, so the program takes only 2.5 seconds to load.
Upvotes: 1