[英]How to send a whatsapp message from python
I am trying to create a program which on certain conditions will send whatsapp message as notification.我正在尝试创建一个程序,该程序在某些条件下会发送 whatsapp 消息作为通知。 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?
有没有办法可以使用任何 python 模块或框架来执行此操作?
we've done a home lab project with a Whatsapp gateway that allows you to send out notifications to your mobile only (to avoid spam use).我们已经使用 Whatsapp 网关完成了一个家庭实验室项目,该网关允许您仅向您的手机发送通知(以避免垃圾邮件的使用)。 Main project is in node-red, allowing 2-way communication and other features but there's a lite version for notifications that work over a simple http post.
主项目是红色节点,允许双向通信和其他功能,但有一个精简版的通知,可以通过简单的 http 帖子工作。
I've just created a new basic 'how-to' info repo in: https://github.com/inUtil-info/whin-use-cases我刚刚在: https://github.com/inUtil-info/whin-use-cases中创建了一个新的基本“操作方法”信息存储库
We'd be happy to hear your feedback on the issues/comments section of the repo.我们很高兴听到您对回购的问题/评论部分的反馈。
Create a new file called "wasend.py" and write the following code on it:创建一个名为“wasend.py”的新文件并在其上写入以下代码:
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".创建另一个名为“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()
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.该程序需要 11.5 秒来执行(您可以更改 sleep 以使其更快,但要给 WhatsApp 一些时间来加载。如果您已经加载了 WhatsApp,
wasend.py
中的第 7 行更改为sleep(1)
,因此程序加载仅需 2.5 秒。
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您可以在以下来源中找到更多详细信息https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#text-object
you can look this library: https://pypi.org/project/pywhatkit/ --pywhatkit你可以看看这个库: https://pypi.org/project/pywhatkit/ --pywhatkit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.