Cómo enviar la API de WhatsApp usando Python | Inicio rápido

Introducción

En este tutorial, crearemos ejemplos simples para enviar mensajes a través de la API de WhatsApp usando Python.

Inicialmente, debe asegurarse de que la biblioteca http.client esté lista porque usaremos esta biblioteca en este artículo para enviar mensajes a Ultramsg Gateway.

Quicke Ejemplo de uso de la API de WhatsApp usando Python

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&body=WhatsApp API on UltraMsg.com works good&priority=10&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/chat", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Cuando se ejecuta el código anterior, y si el mensaje se envía con éxito, la respuesta será así:

{"sent":"true","message":"ok","id":44897}

Enviar imagen

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&image=https://file-example.s3-accelerate.amazonaws.com/images/test.jpg&caption=image Caption&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/image", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Enviar documento

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&filename=hello.pdf&document=https://file-example.s3-accelerate.amazonaws.com/documents/cv.pdf&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/document", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Enviar sonido

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&audio=https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/audio", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Enviar voz

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&audio=https://file-example.s3-accelerate.amazonaws.com/voice/oog_example.ogg&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/voice", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Enviar vídeo

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&video=https://file-example.s3-accelerate.amazonaws.com/video/test.mp4&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/video", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&link=https://en.wikipedia.org/wiki/COVID-19&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/link", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Enviar contacto

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&[email protected]&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/contact", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Enviar ubicación

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&address=ABC company \n Sixth floor , office 38&lat=25.197197&lng=55.2721877&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/location", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Enviar tarjeta virtual

import http.client
conn = http.client.HTTPSConnection("api.ultramsg.com")
payload = "token=1v941eyo9eqixrsi&to=14155552671&vcard= BEGIN:VCARD\nVERSION:3.0\nN:lastname;firstname\nFN:firstname lastname\nTEL;TYPE=CELL;waid=14000000001:14000000002\nNICKNAME:nickname\nBDAY:01.01.1987\nX-GENDER:M\nNOTE:note\nADR;TYPE=home:;;;;;;\nADR;TYPE=work_:;;;;;;\nEND:VCARD&referenceId="
headers = { 'content-type': "application/x-www-form-urlencoded" }
conn.request("POST", "/instance16/messages/vcard", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

usando solicitudes lib

También podemos usar la Biblioteca de solicitudes .

Ejemplo de uso de la API de WhatsApp usando Python y solicitudes lib

import requests
url = "https://api.ultramsg.com/instance16/messages/chat"
payload = "token=1v941eyo9eqixrsi&to=14155552671&body=test message;priority=1&referenceId="
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)

finalmente, puede ver la documentación completa de la API de Whatsapp y las preguntas frecuentes .