Python code to send sms only when the difference date of the query is equal to 7 days from the date of the current day.
# -*- coding: utf-8 -*-
import MySQLdb
import pycurl
import base64
import json
import datetime
date_now = datetime.datetime.now()
seven_days_ago = date_now + datetime.timedelta(days=7)
db = MySQLdb.connect(host="xxx.xxx.x.xx", user="xxxx", passwd="xxxxxxx", db="xxxxxx")
cursor = db.cursor()
cursor.execute("SELECT DataConsulta, Dias, HoraConsulta, HoraSaida, nome, Consulta, centrodb.LocalConsulta.Descricao, Contato FROM centrodb.RegistoConsultas LEFT OUTER JOIN centrodb.LocalConsulta ON centrodb.LocalConsulta.Id = centrodb.RegistoConsultas.'Local' LEFT OUTER JOIN centrodb.utentes ON centrodb.utentes.codigoutente = centrodb.RegistoConsultas.Utente LEFT OUTER JOIN centrodb.DiasSemana ON centrodb.DiasSemana.Id = centrodb.RegistoConsultas.DiaSemana")
myresult = cursor.fetchall()
for linha in myresult:
DataConsulta = linha[0]
Dias = linha[1]
HoraConsulta = linha[2]
HoraSaida = linha[3]
nome = linha[4]
Consulta = linha[5]
Descricao = linha[6]
Contato = linha[7]
if DataConsulta == seven_days_ago():
if __name__ == "__main__":
url ="https://dashboard.360nrs.com/api/rest/sms"
usrPass = "xxxxxx:xxxxxxx"
data = json.dumps({
"to":[Contato],
"from":"CPA",
"message":"Teste ",
})
b64Val = base64.b64encode(usrPass)
headers=["Accept:Application/json","Authorization:Basic %s"%b64Val]
c = pycurl.Curl()
c.setopt(pycurl.URL, url)
c.setopt(pycurl.HTTPHEADER,headers)
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, data)
c.setopt(pycurl.SSL_VERIFYHOST, 0)
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.perform()
http_code = c.getinfo(pycurl.HTTP_CODE)
print(http_code)
When I run the script on the terminal I get this error:
Traceback (most recent call last):
File "/var/www/html/wordpress/ensms.py", line 29, in <module>
if DataConsulta == seven_days_ago():
TypeError: 'datetime.datetime' object is not callable