`I’m having problems with this script. It should initially load a spreadsheet, format the columns, format the phone numbers, and send messages via WhatsApp Web. It opens the WhatsApp Web page and starts the first conversation with the correct message, but it doesn’t send the message. Instead, it reloads the page to send to the second number. In the second conversation, the message is not selected, and it also doesn’t send the message. When running the script in the Anaconda CMD, it interprets the message as if they were commands.
Here is the code I’m using:
import pywhatkit as kit
import pandas as pd
import re
import time
import pyautogui
import pyperclip
import urllib.parse
import webbrowser # Para abrir a URL no navegador padrão
# Carregar os dados
df = pd.read_excel('Lista de pacientes Simone El-Bacha 2.xlsx')
def formatar_telefone(telefone):
if pd.isna(telefone):
return None
telefone = re.sub(r'D', '', str(telefone))
if len(telefone) == 11 and telefone.startswith('719'):
telefone = '55' + telefone
elif len(telefone) == 10 and telefone.startswith('71'):
telefone = '55' + telefone[:2] + '9' + telefone[2:]
elif len(telefone) == 9 e telefone.startswith('9'):
telefone = '5571' + telefone
elif len(telefone) == 8:
telefone = '55719' + telefone
else:
return None
if not telefone.startswith('55719'):
return None
return telefone
# Aplicar a função de formatação
df['TELEFONE'] = df['TELEFONE'].apply(formatar_telefone)
df = df.dropna(subset=['TELEFONE'])
# Função para criar a mensagem personalizada
def criar_mensagem(nome_completo):
primeiro_nome = nome_completo.split()[0] if pd.notna(nome_completo) else 'Paciente'
mensagem = (f"Olá {primeiro_nome},nn"
"Temos uma ótima notícia para você! A Dra. Simone El-Bachá está agora atendendo na clínica CFA. 🌟nn"
"Estamos muito felizes em continuar oferecendo cuidados de qualidade e sabemos que a Dra. Simone também está animada para atendê-la novamente. Na clínica CFA, você encontrará um ambiente acolhedor e todas as especialidades que você precisa, incluindo:nn"
"• Cardiologian"
"• Exames Laboratoriaisn"
"• Ultrassonografian"
"• Psicologian"
"• E muito mais!nn"
"Será um prazer recebê-la em nossa clínica. Caso precise de mais informações ou queira agendar uma consulta, estamos aqui para ajudar pelo WPP ou no telefone 71 3249-5497!nn"
"Equipe da Clínica CFA")
return mensagem
# Função para enviar mensagem pelo WhatsApp Web
def enviar_mensagem(numero, mensagem):
try:
if not numero.startswith('55'):
numero = '55' + numero
mensagem_codificada = urllib.parse.quote(mensagem)
url = f"https://wa.me/{numero}?text={mensagem_codificada}"
print(f"URL gerada: {url}")
webbrowser.open(url) # Abre a URL no navegador padrão
kit.sendwhatmsg_instantly(url, mensagem)
time.sleep(10) # Espera 10 segundos para garantir que a mensagem foi escrita
py
`
Gabriel Abiyê is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.