我正在寻找一种无需重新打开浏览器即可向多个用户(客户编号)发送 WhatsApp 消息的方法,我遇到了 Pywhatkit,其内部实现需要在每次要发送消息或媒体时重新打开浏览器。
任何关于我可以使用哪个 python 工具或库来完成此任务的建议或推荐,我将非常感激。
谢谢你
创建 Excel 文件“messages_schedule.xlsx”
import openpyxl
from datetime import datetime, timedelta
# Function to create Excel sheet with sample data
def create_excel_sheet(filename):
# Create a new Excel workbook
wb = openpyxl.Workbook()
# Select the active sheet
sheet = wb.active
# Set column headers
sheet['A1'] = 'Numbers'
sheet['B1'] = 'Notes'
sheet['C1'] = 'Time for Sending'
# Sample data
sample_data = [
('+923000000000', 'Meeting tomorrow at 2 PM', datetime.now() + timedelta(seconds=20)),
('+923000000000', 'Don\'t forget to buy groceries', datetime.now() + timedelta(seconds=40)),
]
# Populate the sheet with sample data
for row_num, (number, note, send_time) in enumerate(sample_data, start=2):
sheet.cell(row=row_num, column=1, value=number)
sheet.cell(row=row_num, column=2, value=note)
sheet.cell(row=row_num, column=3, value=send_time)
# Save the workbook to a file
wb.save(filename)
print(f"Excel sheet '{filename}' created with sample data.")
# # Define the filename for the Excel sheet
# excel_filename = 'messages_schedule.xlsx'
#
# # Create Excel sheet with sample data
# create_excel_sheet(excel_filename)
在不同时间发送消息
# Importing the Required Library
import pywhatkit
import openpyxl
from datetime import datetime, timedelta
import time
一个号码只能发送一条消息
# Defining the Phone Number and Message
phone_number = "+92300000000"
message = "message"
# Sending the WhatsApp Message
pywhatkit.sendwhatmsg_instantly(phone_number, message)
# Displaying a Success Message
print("WhatsApp message sent!")
对于多个号码
import create_numbers_xls_sheet
# Define the filename for the Excel sheet
excel_filename = 'messages_schedule.xlsx'
create_numbers_xls_sheet.create_excel_sheet(excel_filename)
# Function to read Excel sheet and schedule messages using pywhatkit
def read_excel_and_schedule_messages(filename):
# Load the workbook
wb = openpyxl.load_workbook(filename)
sheet = wb.active
# Iterate through rows in the sheet
for row in sheet.iter_rows(min_row=2, values_only=True):
contact, message, send_time = row
# Calculate the time difference
current_time = datetime.now()
time_difference = (send_time - current_time).total_seconds()
# Wait until the scheduled time
if time_difference > 0:
print(f"Waiting for {time_difference} seconds until {send_time}")
time.sleep(time_difference)
# Send the message using pywhatkit
try:
pywhatkit.sendwhatmsg_instantly(contact, message)
print(f"WhatsApp message sent to {contact}: {message}")
except Exception as e:
print(f"Error sending message to {contact}: {e}")
# Read Excel sheet and schedule messages
read_excel_and_schedule_messages(excel_filename)